blob: b082f8be3e1429b5add50554771ee2a993e47455 [file] [log] [blame]
xinwued8d5582013-09-18 17:19:31 -07001package org.projectfloodlight.openflow.types;
2
3import javax.annotation.concurrent.Immutable;
4
Rich Laneeb21c4f2013-10-28 17:34:41 -07005import org.jboss.netty.buffer.ChannelBuffer;
6
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07007import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07008import com.google.common.primitives.UnsignedInts;
9
xinwued8d5582013-09-18 17:19:31 -070010@Immutable
Andreas Wundsam299f3622013-09-30 13:19:56 -070011public class ClassId implements OFValueType<ClassId> {
xinwued8d5582013-09-18 17:19:31 -070012 static final int LENGTH = 4;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070013
14 private final static int NONE_VAL = 0;
Andreas Wundsam299f3622013-09-30 13:19:56 -070015 public final static ClassId NONE = new ClassId(NONE_VAL);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070016
xinwued8d5582013-09-18 17:19:31 -070017 private final int rawValue;
18
Andreas Wundsam299f3622013-09-30 13:19:56 -070019 private ClassId(final int rawValue) {
xinwued8d5582013-09-18 17:19:31 -070020 this.rawValue = rawValue;
21 }
22
Andreas Wundsam299f3622013-09-30 13:19:56 -070023 public static ClassId of(final int raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070024 if(raw == NONE_VAL)
25 return NONE;
26
Andreas Wundsam299f3622013-09-30 13:19:56 -070027 return new ClassId(raw);
xinwued8d5582013-09-18 17:19:31 -070028 }
29
30 public int getInt() {
31 return rawValue;
32 }
33
Andreas Wundsam85c961f2013-09-29 21:22:12 -070034 @Override
xinwued8d5582013-09-18 17:19:31 -070035 public int getLength() {
36 return LENGTH;
37 }
38
39 @Override
Andreas Wundsam85c961f2013-09-29 21:22:12 -070040 public String toString() {
41 return Integer.toString(rawValue);
42 }
43
44 @Override
Andreas Wundsam299f3622013-09-30 13:19:56 -070045 public ClassId applyMask(ClassId mask) {
46 return ClassId.of(rawValue & mask.rawValue); }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070047
48 @Override
xinwued8d5582013-09-18 17:19:31 -070049 public int hashCode() {
50 final int prime = 31;
51 int result = 1;
52 result = prime * result + rawValue;
53 return result;
54 }
55
56 @Override
57 public boolean equals(Object obj) {
58 if (this == obj)
59 return true;
60 if (obj == null)
61 return false;
62 if (getClass() != obj.getClass())
63 return false;
Andreas Wundsam299f3622013-09-30 13:19:56 -070064 ClassId other = (ClassId) obj;
xinwued8d5582013-09-18 17:19:31 -070065 if (rawValue != other.rawValue)
66 return false;
67 return true;
68 }
69
Rich Laneeb21c4f2013-10-28 17:34:41 -070070 public void write4Bytes(ChannelBuffer c) {
71 c.writeInt(rawValue);
72 }
73
74 public static ClassId read4Bytes(ChannelBuffer c) {
75 return ClassId.of(c.readInt());
76 }
77
xinwued8d5582013-09-18 17:19:31 -070078 @Override
Andreas Wundsam299f3622013-09-30 13:19:56 -070079 public int compareTo(ClassId o) {
Andreas Wundsam85c961f2013-09-29 21:22:12 -070080 return UnsignedInts.compare(rawValue, rawValue);
xinwued8d5582013-09-18 17:19:31 -070081 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070082
83 @Override
84 public void putTo(PrimitiveSink sink) {
85 sink.putInt(rawValue);
86 }
xinwued8d5582013-09-18 17:19:31 -070087}