Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 3 | import org.projectfloodlight.openflow.annotations.Immutable; |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 4 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 5 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 6 | import com.google.common.primitives.UnsignedInts; |
| 7 | |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 8 | /** |
| 9 | * Abstraction of a buffer id in OpenFlow. Immutable. |
| 10 | * |
| 11 | * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com> |
| 12 | */ |
| 13 | @Immutable |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 14 | public class OFBufferId implements Comparable<OFBufferId>, PrimitiveSinkable { |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 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 | |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 38 | @Override |
| 39 | public int hashCode() { |
| 40 | final int prime = 31; |
| 41 | int result = 1; |
| 42 | result = prime * result + rawValue; |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean equals(Object obj) { |
| 48 | if (this == obj) |
| 49 | return true; |
| 50 | if (obj == null) |
| 51 | return false; |
| 52 | if (getClass() != obj.getClass()) |
| 53 | return false; |
| 54 | OFBufferId other = (OFBufferId) obj; |
| 55 | if (rawValue != other.rawValue) |
| 56 | return false; |
| 57 | return true; |
| 58 | } |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public int compareTo(OFBufferId o) { |
| 62 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 63 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 64 | |
| 65 | @Override |
| 66 | public void putTo(PrimitiveSink sink) { |
| 67 | sink.putInt(rawValue); |
| 68 | } |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 69 | } |