blob: 98c12532a43bcb0eb07a89198a06d6b4f917e2f8 [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
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080017 private final static int NO_MASK_VAL = 0xFFFFFFFF;
18 public final static ClassId NO_MASK = new ClassId(NO_MASK_VAL);
19 public final static ClassId FULL_MASK = NONE;
20
xinwued8d5582013-09-18 17:19:31 -070021 private final int rawValue;
22
Andreas Wundsam299f3622013-09-30 13:19:56 -070023 private ClassId(final int rawValue) {
xinwued8d5582013-09-18 17:19:31 -070024 this.rawValue = rawValue;
25 }
26
Andreas Wundsam299f3622013-09-30 13:19:56 -070027 public static ClassId of(final int raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070028 if(raw == NONE_VAL)
29 return NONE;
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080030 else if(raw == NO_MASK_VAL)
31 return NO_MASK;
Andreas Wundsam299f3622013-09-30 13:19:56 -070032 return new ClassId(raw);
xinwued8d5582013-09-18 17:19:31 -070033 }
34
35 public int getInt() {
36 return rawValue;
37 }
38
Andreas Wundsam85c961f2013-09-29 21:22:12 -070039 @Override
xinwued8d5582013-09-18 17:19:31 -070040 public int getLength() {
41 return LENGTH;
42 }
43
44 @Override
Andreas Wundsam85c961f2013-09-29 21:22:12 -070045 public String toString() {
Andreas Wundsamb3ecc222014-11-12 17:10:01 -080046 return UnsignedInts.toString(rawValue);
Andreas Wundsam85c961f2013-09-29 21:22:12 -070047 }
48
49 @Override
Andreas Wundsam299f3622013-09-30 13:19:56 -070050 public ClassId applyMask(ClassId mask) {
51 return ClassId.of(rawValue & mask.rawValue); }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070052
53 @Override
xinwued8d5582013-09-18 17:19:31 -070054 public int hashCode() {
55 final int prime = 31;
56 int result = 1;
57 result = prime * result + rawValue;
58 return result;
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj)
64 return true;
65 if (obj == null)
66 return false;
67 if (getClass() != obj.getClass())
68 return false;
Andreas Wundsam299f3622013-09-30 13:19:56 -070069 ClassId other = (ClassId) obj;
xinwued8d5582013-09-18 17:19:31 -070070 if (rawValue != other.rawValue)
71 return false;
72 return true;
73 }
74
Rich Laneeb21c4f2013-10-28 17:34:41 -070075 public void write4Bytes(ChannelBuffer c) {
76 c.writeInt(rawValue);
77 }
78
79 public static ClassId read4Bytes(ChannelBuffer c) {
80 return ClassId.of(c.readInt());
81 }
82
xinwued8d5582013-09-18 17:19:31 -070083 @Override
Andreas Wundsam299f3622013-09-30 13:19:56 -070084 public int compareTo(ClassId o) {
Andreas Wundsam85c961f2013-09-29 21:22:12 -070085 return UnsignedInts.compare(rawValue, rawValue);
xinwued8d5582013-09-18 17:19:31 -070086 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070087
88 @Override
89 public void putTo(PrimitiveSink sink) {
90 sink.putInt(rawValue);
91 }
xinwued8d5582013-09-18 17:19:31 -070092}