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 | |
| 7 | /** |
| 8 | * Abstraction of a buffer id in OpenFlow. Immutable. |
| 9 | * |
| 10 | * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com> |
| 11 | */ |
| 12 | @Immutable |
| 13 | public class OFBufferId { |
| 14 | |
| 15 | public static final OFBufferId NO_BUFFER = new OFBufferId(0xFFFFFFFF); |
| 16 | |
| 17 | private final int rawValue; |
| 18 | |
| 19 | private OFBufferId(int rawValue) { |
| 20 | this.rawValue = rawValue; |
| 21 | } |
| 22 | |
| 23 | public static OFBufferId of(final int rawValue) { |
| 24 | if (rawValue == NO_BUFFER.getInt()) |
| 25 | return NO_BUFFER; |
| 26 | return new OFBufferId(rawValue); |
| 27 | } |
| 28 | |
| 29 | public int getInt() { |
| 30 | return rawValue; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public String toString() { |
| 35 | return Long.toString(U32.f(rawValue)); |
| 36 | } |
| 37 | |
| 38 | public void write4Bytes(ChannelBuffer c) { |
| 39 | c.writeInt(this.rawValue); |
| 40 | } |
| 41 | |
| 42 | public static OFBufferId read4Bytes(ChannelBuffer c) throws OFParseError { |
| 43 | return OFBufferId.of((int)(c.readUnsignedInt() & 0xFFFFFFFF)); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public int hashCode() { |
| 48 | final int prime = 31; |
| 49 | int result = 1; |
| 50 | result = prime * result + rawValue; |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean equals(Object obj) { |
| 56 | if (this == obj) |
| 57 | return true; |
| 58 | if (obj == null) |
| 59 | return false; |
| 60 | if (getClass() != obj.getClass()) |
| 61 | return false; |
| 62 | OFBufferId other = (OFBufferId) obj; |
| 63 | if (rawValue != other.rawValue) |
| 64 | return false; |
| 65 | return true; |
| 66 | } |
| 67 | } |