Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.projectfloodlight.openflow.annotations.Immutable; |
| 5 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 6 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 7 | import com.google.common.primitives.UnsignedInts; |
| 8 | |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 9 | /** |
| 10 | * Abstraction of a buffer id in OpenFlow. Immutable. |
| 11 | * |
| 12 | * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com> |
| 13 | */ |
| 14 | @Immutable |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 15 | public class OFBufferId implements Comparable<OFBufferId> { |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 16 | 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 Vaterlaus | 54f4b7e | 2013-09-24 13:33:02 -0700 | [diff] [blame] | 44 | return OFBufferId.of(c.readInt()); |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 45 | } |
| 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 Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | public int compareTo(OFBufferId o) { |
| 71 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 72 | } |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 73 | } |