Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 972bc42 | 2014-05-05 13:29:50 -0700 | [diff] [blame] | 3 | import javax.annotation.Nonnull; |
| 4 | |
Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | |
Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 7 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 972bc42 | 2014-05-05 13:29:50 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.UnsignedLongs; |
Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 9 | |
| 10 | public class U128 implements OFValueType<U128>, HashValue<U128> { |
| 11 | |
| 12 | static final int LENGTH = 16; |
| 13 | |
| 14 | private final long raw1; // MSBs |
| 15 | private final long raw2; // LSBs |
| 16 | |
| 17 | public static final U128 ZERO = new U128(0, 0); |
| 18 | |
| 19 | private U128(long raw1, long raw2) { |
| 20 | this.raw1 = raw1; |
| 21 | this.raw2 = raw2; |
| 22 | } |
| 23 | |
| 24 | public static U128 of(long raw1, long raw2) { |
| 25 | if (raw1 == 0 && raw2 == 0) |
| 26 | return ZERO; |
| 27 | return new U128(raw1, raw2); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int getLength() { |
| 32 | return LENGTH; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public long getMsb() { |
| 37 | return raw1; |
| 38 | } |
| 39 | |
| 40 | public long getLsb() { |
| 41 | return raw2; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public U128 applyMask(U128 mask) { |
| 46 | return and(mask); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int hashCode() { |
| 51 | final int prime = 31; |
| 52 | int result = 1; |
| 53 | result = prime * result + (int) (raw1 ^ (raw1 >>> 32)); |
| 54 | result = prime * result + (int) (raw2 ^ (raw2 >>> 32)); |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean equals(Object obj) { |
| 60 | if (this == obj) |
| 61 | return true; |
| 62 | if (obj == null) |
| 63 | return false; |
| 64 | if (getClass() != obj.getClass()) |
| 65 | return false; |
| 66 | U128 other = (U128) obj; |
| 67 | if (raw1 != other.raw1) |
| 68 | return false; |
| 69 | if (raw2 != other.raw2) |
| 70 | return false; |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | public void write16Bytes(ChannelBuffer cb) { |
| 75 | cb.writeLong(raw1); |
| 76 | cb.writeLong(raw2); |
| 77 | } |
| 78 | |
| 79 | public static U128 read16Bytes(ChannelBuffer cb) { |
| 80 | long raw1 = cb.readLong(); |
| 81 | long raw2 = cb.readLong(); |
| 82 | return of(raw1, raw2); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return String.format("0x%016x%016x", raw1, raw2); |
| 88 | } |
| 89 | |
| 90 | @Override |
Andreas Wundsam | 972bc42 | 2014-05-05 13:29:50 -0700 | [diff] [blame] | 91 | public int compareTo(@Nonnull U128 o) { |
| 92 | int msb = UnsignedLongs.compare(this.raw1, o.raw1); |
| 93 | if(msb != 0) |
| 94 | return msb; |
| 95 | else |
| 96 | return UnsignedLongs.compare(this.raw2, o.raw2); |
Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void putTo(PrimitiveSink sink) { |
| 101 | sink.putLong(raw1); |
| 102 | sink.putLong(raw2); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public U128 inverse() { |
| 107 | return U128.of(~raw1, ~raw2); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public U128 or(U128 other) { |
| 112 | return U128.of(raw1 | other.raw1, raw2 | other.raw2); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public U128 and(U128 other) { |
| 117 | return U128.of(raw1 & other.raw1, raw2 & other.raw2); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public U128 xor(U128 other) { |
| 122 | return U128.of(raw1 ^ other.raw1, raw2 ^ other.raw2); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int prefixBits(int numBits) { |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 127 | return HashValueUtils.prefixBits(this.raw1, numBits); |
Andreas Wundsam | 7c15b17 | 2014-04-24 19:02:40 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public U128 combineWithValue(U128 value, int keyBits) { |
| 132 | return U128.of( |
| 133 | HashValueUtils.combineWithValue(this.raw1, value.raw1, Math.min(64, keyBits)), |
| 134 | HashValueUtils.combineWithValue(this.raw2, value.raw2, Math.max(0,keyBits-64)) |
| 135 | ); |
| 136 | } |
| 137 | } |