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