blob: e537c20989bd211451001cb112943553c2d255ed [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 UDF implements OFValueType<UDF> {
12 static final int LENGTH = 4;
13 private final int rawValue;
14
15 public static final UDF ZERO = UDF.of(0x0);
16 public static final UDF NO_MASK = UDF.of(0xFFFFFFFF);
17 public static final UDF FULL_MASK = UDF.of(0x00000000);
18
19 private UDF(final int rawValue) {
20 this.rawValue = rawValue;
21 }
22
23 public static UDF of(final int raw) {
24 return new UDF(raw);
25 }
26
27 public int getInt() {
28 return rawValue;
29 }
30
31 @Override
32 public int getLength() {
33 return LENGTH;
34 }
35
36 @Override
37 public int hashCode() {
38 final int prime = 31;
39 int result = 1;
40 result = prime * result + rawValue;
41 return result;
42 }
43
44 @Override
45 public boolean equals(Object obj) {
46 if (this == obj)
47 return true;
48 if (obj == null)
49 return false;
50 if (getClass() != obj.getClass())
51 return false;
52 UDF other = (UDF) obj;
53 if (rawValue != other.rawValue)
54 return false;
55 return true;
56 }
57
58 @Override
59 public String toString() {
60 return Integer.toString(rawValue);
61 }
62
63 public void write4Bytes(ChannelBuffer c) {
64 c.writeInt(rawValue);
65 }
66
67 public static UDF read4Bytes(ChannelBuffer c) {
68 return UDF.of(c.readInt());
69 }
70
71 @Override
72 public UDF applyMask(UDF mask) {
73 return UDF.of(this.rawValue & mask.rawValue);
74 }
75
76 @Override
77 public int compareTo(UDF o) {
78 return UnsignedInts.compare(rawValue, o.rawValue);
79 }
80
81 @Override
82 public void putTo(PrimitiveSink sink) {
83 sink.putInt(rawValue);
84 }
85}