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