blob: d2a09fa45f403a94a54098892ed0a402d0e7e5c6 [file] [log] [blame]
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -07001package org.onlab.packet;
2
3/**
4 * Representation of a VLAN ID.
5 */
6public class VLANID {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -07007
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -07008 private final short value;
Ayaka Koshibe1a100982014-09-13 19:32:19 -07009 // Based on convention used elsewhere? Check and change if needed
Ayaka Koshibe16698a32014-09-13 22:19:02 -070010 public static final short UNTAGGED = (short) 0xffff;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070011 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070012 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070013
Ayaka Koshibe1a100982014-09-13 19:32:19 -070014 protected VLANID() {
15 this.value = UNTAGGED;
16 }
17
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070018 protected VLANID(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070019 this.value = value;
20 }
21
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070022 public static VLANID vlanId() {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070023 return new VLANID(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070024 }
25
26 public static VLANID vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070027 if (value == UNTAGGED) {
28 return new VLANID();
29 }
30
31 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070032 throw new IllegalArgumentException(
33 "value exceeds allowed maximum VLAN ID value (4095)");
34 }
35 return new VLANID(value);
36 }
37
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070038 public short toShort() {
39 return this.value;
40 }
41
42 @Override
43 public boolean equals(Object obj) {
44 if (this == obj) {
45 return true;
46 }
47
48 if (obj instanceof VLANID) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070049
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070050 VLANID other = (VLANID) obj;
51
52 if (this.value == other.value) {
53 return true;
54 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070055 }
56
57 return false;
58 }
59
60 @Override
61 public int hashCode() {
62 return this.value;
63 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070064
65 @Override
66 public String toString() {
67 return String.valueOf(this.value);
68 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070069}
70