blob: 51364e1ee64da305a096a7dc1bb9418cbd63c0d3 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import javax.annotation.concurrent.Immutable;
4
5import org.jboss.netty.buffer.ChannelBuffer;
6
7import com.google.common.hash.PrimitiveSink;
8import com.google.common.primitives.UnsignedInts;
9
10@Immutable
11public class LagId implements OFValueType<LagId> {
12 static final int LENGTH = 4;
13 private final int rawValue;
14
15 private final static int NONE_VAL = 0;
16 public final static LagId NONE = new LagId(NONE_VAL);
17
18 private final static int NO_MASK_VAL = 0xFFFFFFFF;
19 public final static LagId NO_MASK = new LagId(NO_MASK_VAL);
20 public final static LagId FULL_MASK = NONE;
21
22 private LagId(final int rawValue) {
23 this.rawValue = rawValue;
24 }
25
26 public static LagId of(final int raw) {
27 if(raw == NONE_VAL)
28 return NONE;
29 else if (raw == NO_MASK_VAL)
30 return NO_MASK;
31 return new LagId(raw);
32 }
33
34 public int getInt() {
35 return rawValue;
36 }
37
38 @Override
39 public int getLength() {
40 return LENGTH;
41 }
42
43 @Override
44 public int hashCode() {
45 final int prime = 31;
46 int result = 1;
47 result = prime * result + rawValue;
48 return result;
49 }
50
51 @Override
52 public String toString() {
53 return Integer.toString(rawValue);
54 }
55
56 @Override
57 public boolean equals(final Object obj) {
58 if (this == obj)
59 return true;
60 if (obj == null)
61 return false;
62 if (getClass() != obj.getClass())
63 return false;
64 LagId other = (LagId) obj;
65 if (rawValue != other.rawValue)
66 return false;
67 return true;
68 }
69
70 public void write4Bytes(ChannelBuffer c) {
71 c.writeInt(rawValue);
72 }
73
74 public static LagId read4Bytes(ChannelBuffer c) {
75 return LagId.of(c.readInt());
76 }
77
78 @Override
79 public int compareTo(LagId o) {
80 return UnsignedInts.compare(rawValue, o.rawValue);
81 }
82
83 @Override
84 public LagId applyMask(LagId mask) {
85 return LagId.of(rawValue & mask.rawValue);
86 }
87
88 @Override
89 public void putTo(PrimitiveSink sink) {
90 sink.putInt(rawValue);
91 }
92}