Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 1 | package org.onlab.packet; |
| 2 | |
| 3 | /** |
| 4 | * Representation of a VLAN ID. |
| 5 | */ |
| 6 | public class VLANID { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 7 | |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 8 | 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 Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 14 | this.value = value; |
| 15 | } |
| 16 | |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 17 | 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 Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 29 | 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 Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 40 | |
Ayaka Koshibe | 50ee924 | 2014-09-12 16:37:46 -0700 | [diff] [blame^] | 41 | VLANID other = (VLANID) obj; |
| 42 | |
| 43 | if (this.value == other.value) { |
| 44 | return true; |
| 45 | } |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public int hashCode() { |
| 53 | return this.value; |
| 54 | } |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public String toString() { |
| 58 | return String.valueOf(this.value); |
| 59 | } |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 60 | } |
| 61 | |