blob: 1684d8d62cd0b30b8650ccc7887c2d6beed738f7 [file] [log] [blame]
Rob Vaterlausb10ae552013-09-23 14:39:39 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.annotations.Immutable;
5import org.projectfloodlight.openflow.exceptions.OFParseError;
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
Rob Vaterlausb10ae552013-09-23 14:39:39 -070010/**
11 * Abstraction of a buffer id in OpenFlow. Immutable.
12 *
13 * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
14 */
15@Immutable
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070016public class OFBufferId implements Comparable<OFBufferId>, PrimitiveSinkable {
Rob Vaterlausb10ae552013-09-23 14:39:39 -070017 public static final OFBufferId NO_BUFFER = new OFBufferId(0xFFFFFFFF);
18
19 private final int rawValue;
20
21 private OFBufferId(int rawValue) {
22 this.rawValue = rawValue;
23 }
24
25 public static OFBufferId of(final int rawValue) {
26 if (rawValue == NO_BUFFER.getInt())
27 return NO_BUFFER;
28 return new OFBufferId(rawValue);
29 }
30
31 public int getInt() {
32 return rawValue;
33 }
34
35 @Override
36 public String toString() {
37 return Long.toString(U32.f(rawValue));
38 }
39
40 public void write4Bytes(ChannelBuffer c) {
41 c.writeInt(this.rawValue);
42 }
43
44 public static OFBufferId read4Bytes(ChannelBuffer c) throws OFParseError {
Rob Vaterlaus54f4b7e2013-09-24 13:33:02 -070045 return OFBufferId.of(c.readInt());
Rob Vaterlausb10ae552013-09-23 14:39:39 -070046 }
47
48 @Override
49 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;
64 OFBufferId other = (OFBufferId) obj;
65 if (rawValue != other.rawValue)
66 return false;
67 return true;
68 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070069
70 @Override
71 public int compareTo(OFBufferId o) {
72 return UnsignedInts.compare(rawValue, o.rawValue);
73 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070074
75 @Override
76 public void putTo(PrimitiveSink sink) {
77 sink.putInt(rawValue);
78 }
Rob Vaterlausb10ae552013-09-23 14:39:39 -070079}