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