xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
xinwu | 3203443 | 2013-09-18 17:17:50 -0700 | [diff] [blame] | 3 | import javax.annotation.concurrent.Immutable; |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 4 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 7 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.UnsignedInts; |
| 9 | |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 10 | @Immutable |
| 11 | public class VRF implements OFValueType<VRF> { |
| 12 | static final int LENGTH = 4; |
| 13 | private final int rawValue; |
| 14 | |
| 15 | public static final VRF NO_MASK = VRF.of(0xFFFFFFFF); |
| 16 | public static final VRF FULL_MASK = VRF.of(0x00000000); |
| 17 | |
| 18 | private VRF(final int rawValue) { |
| 19 | this.rawValue = rawValue; |
| 20 | } |
| 21 | |
| 22 | public static VRF of(final int raw) { |
| 23 | return new VRF(raw); |
| 24 | } |
| 25 | |
| 26 | public int getInt() { |
| 27 | return rawValue; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int getLength() { |
| 32 | return LENGTH; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public int hashCode() { |
| 37 | final int prime = 31; |
| 38 | int result = 1; |
| 39 | result = prime * result + rawValue; |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean equals(Object obj) { |
| 45 | if (this == obj) |
| 46 | return true; |
| 47 | if (obj == null) |
| 48 | return false; |
| 49 | if (getClass() != obj.getClass()) |
| 50 | return false; |
| 51 | VRF other = (VRF) obj; |
| 52 | if (rawValue != other.rawValue) |
| 53 | return false; |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public String toString() { |
| 59 | return Integer.toString(rawValue); |
| 60 | } |
| 61 | |
| 62 | public void write4Bytes(ChannelBuffer c) { |
| 63 | c.writeInt(rawValue); |
| 64 | } |
| 65 | |
| 66 | public static VRF read4Bytes(ChannelBuffer c) { |
| 67 | return VRF.of(c.readInt()); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public VRF applyMask(VRF mask) { |
| 72 | return VRF.of(this.rawValue & mask.rawValue); |
| 73 | } |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | public int compareTo(VRF o) { |
| 77 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 78 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | public void putTo(PrimitiveSink sink) { |
| 82 | sink.putInt(rawValue); |
| 83 | } |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 84 | } |