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