blob: 5b26d049c985277431e12c2fd526d16fe10fcd06 [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;
9 private static final short NONE = 0;
10 // A VLAN ID is actually 12 bits of a VLAN tag.
11 private static final short MAX_VLAN = 4095;
12
13 protected VLANID(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070014 this.value = value;
15 }
16
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070017 public static VLANID vlanId() {
18 return new VLANID(NONE);
19 }
20
21 public static VLANID vlanId(short value) {
22 if (value >= MAX_VLAN) {
23 throw new IllegalArgumentException(
24 "value exceeds allowed maximum VLAN ID value (4095)");
25 }
26 return new VLANID(value);
27 }
28
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070029 public short toShort() {
30 return this.value;
31 }
32
33 @Override
34 public boolean equals(Object obj) {
35 if (this == obj) {
36 return true;
37 }
38
39 if (obj instanceof VLANID) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070040
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070041 VLANID other = (VLANID) obj;
42
43 if (this.value == other.value) {
44 return true;
45 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070046 }
47
48 return false;
49 }
50
51 @Override
52 public int hashCode() {
53 return this.value;
54 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070055
56 @Override
57 public String toString() {
58 return String.valueOf(this.value);
59 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070060}
61