blob: 856eff0af6e9911fea74b9eb3dae29227cd4896b [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 Wundsam85c961f2013-09-29 21:22:12 -07007import com.google.common.primitives.UnsignedInts;
8
Rob Vaterlausb10ae552013-09-23 14:39:39 -07009/**
10 * Abstraction of a buffer id in OpenFlow. Immutable.
11 *
12 * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
13 */
14@Immutable
Andreas Wundsam85c961f2013-09-29 21:22:12 -070015public class OFBufferId implements Comparable<OFBufferId> {
Rob Vaterlausb10ae552013-09-23 14:39:39 -070016 public static final OFBufferId NO_BUFFER = new OFBufferId(0xFFFFFFFF);
17
18 private final int rawValue;
19
20 private OFBufferId(int rawValue) {
21 this.rawValue = rawValue;
22 }
23
24 public static OFBufferId of(final int rawValue) {
25 if (rawValue == NO_BUFFER.getInt())
26 return NO_BUFFER;
27 return new OFBufferId(rawValue);
28 }
29
30 public int getInt() {
31 return rawValue;
32 }
33
34 @Override
35 public String toString() {
36 return Long.toString(U32.f(rawValue));
37 }
38
39 public void write4Bytes(ChannelBuffer c) {
40 c.writeInt(this.rawValue);
41 }
42
43 public static OFBufferId read4Bytes(ChannelBuffer c) throws OFParseError {
Rob Vaterlaus54f4b7e2013-09-24 13:33:02 -070044 return OFBufferId.of(c.readInt());
Rob Vaterlausb10ae552013-09-23 14:39:39 -070045 }
46
47 @Override
48 public int hashCode() {
49 final int prime = 31;
50 int result = 1;
51 result = prime * result + rawValue;
52 return result;
53 }
54
55 @Override
56 public boolean equals(Object obj) {
57 if (this == obj)
58 return true;
59 if (obj == null)
60 return false;
61 if (getClass() != obj.getClass())
62 return false;
63 OFBufferId other = (OFBufferId) obj;
64 if (rawValue != other.rawValue)
65 return false;
66 return true;
67 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070068
69 @Override
70 public int compareTo(OFBufferId o) {
71 return UnsignedInts.compare(rawValue, o.rawValue);
72 }
Rob Vaterlausb10ae552013-09-23 14:39:39 -070073}