Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 5 | |
| 6 | import com.google.common.primitives.Shorts; |
| 7 | |
| 8 | public class AuxId implements Comparable<AuxId> { |
| 9 | |
| 10 | private static final short VALIDATION_MASK = 0xFF; |
| 11 | |
| 12 | private static final short MAIN_VAL = 0x0000; |
| 13 | |
| 14 | public static final AuxId MAIN = new AuxId(MAIN_VAL); |
| 15 | |
| 16 | private final short id; |
| 17 | |
| 18 | private AuxId(short id) { |
| 19 | this.id = id; |
| 20 | } |
| 21 | |
| 22 | public static AuxId of(short id) { |
| 23 | switch(id) { |
| 24 | case MAIN_VAL: |
| 25 | return MAIN; |
| 26 | default: |
| 27 | if ((id & VALIDATION_MASK) != id) |
| 28 | throw new IllegalArgumentException("Illegal Aux id value: " + id); |
| 29 | return new AuxId(id); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public static AuxId of(int id) { |
| 34 | if((id & VALIDATION_MASK) != id) |
| 35 | throw new IllegalArgumentException("Illegal Aux id value: "+id); |
| 36 | return of((short) id); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String toString() { |
| 41 | return "0x" + Integer.toHexString(id); |
| 42 | } |
| 43 | |
| 44 | public short getValue() { |
| 45 | return id; |
| 46 | } |
| 47 | |
| 48 | public void writeByte(ChannelBuffer c) { |
| 49 | c.writeByte(this.id); |
| 50 | } |
| 51 | |
| 52 | public static AuxId readByte(ChannelBuffer c) throws OFParseError { |
| 53 | return AuxId.of(c.readUnsignedByte()); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean equals(Object obj) { |
| 58 | if (!(obj instanceof TableId)) |
| 59 | return false; |
| 60 | AuxId other = (AuxId)obj; |
| 61 | if (other.id != this.id) |
| 62 | return false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int hashCode() { |
| 68 | int prime = 13873; |
| 69 | return this.id * prime; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int compareTo(AuxId other) { |
| 74 | return Shorts.compare(this.id, other.id); |
| 75 | } |
| 76 | |
| 77 | } |