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