Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior |
| 3 | * University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package org.projectfloodlight.openflow.types; |
| 19 | |
| 20 | import java.math.BigInteger; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 23 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 26 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 27 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 28 | import com.google.common.primitives.UnsignedLongs; |
| 29 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 30 | public class U64 implements Writeable, OFValueType<U64>, HashValue<U64> { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 31 | private static final long UNSIGNED_MASK = 0x7fffffffffffffffL; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 32 | private final static long ZERO_VAL = 0; |
| 33 | public final static U64 ZERO = new U64(ZERO_VAL); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 34 | |
Byungjoon Lee | b4c38b8 | 2014-04-23 14:24:59 +0900 | [diff] [blame] | 35 | private static final long NO_MASK_VAL = 0xFFffFFffFFffFFffL; |
| 36 | public final static U64 NO_MASK = new U64(NO_MASK_VAL); |
| 37 | public static final U64 FULL_MASK = ZERO; |
| 38 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 39 | private final long raw; |
| 40 | |
| 41 | protected U64(final long raw) { |
| 42 | this.raw = raw; |
| 43 | } |
| 44 | |
| 45 | public static U64 of(long raw) { |
| 46 | return ofRaw(raw); |
| 47 | } |
| 48 | |
| 49 | public static U64 ofRaw(final long raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 50 | if(raw == ZERO_VAL) |
| 51 | return ZERO; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 52 | return new U64(raw); |
| 53 | } |
| 54 | |
| 55 | public static U64 parseHex(String hex) { |
| 56 | return new U64(new BigInteger(hex, 16).longValue()); |
| 57 | } |
| 58 | |
| 59 | public long getValue() { |
| 60 | return raw; |
| 61 | } |
| 62 | |
| 63 | public BigInteger getBigInteger() { |
| 64 | BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK); |
| 65 | if (raw < 0) { |
| 66 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 67 | } |
| 68 | return bigInt; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String toString() { |
Andreas Wundsam | 8ad8699 | 2014-05-09 16:33:28 -0700 | [diff] [blame] | 73 | return String.format("0x%016x", raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public static BigInteger f(final long value) { |
| 77 | BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK); |
| 78 | if (value < 0) { |
| 79 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 80 | } |
| 81 | return bigInt; |
| 82 | } |
| 83 | |
| 84 | public static long t(final BigInteger l) { |
| 85 | return l.longValue(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int hashCode() { |
| 90 | final int prime = 31; |
| 91 | int result = 1; |
| 92 | result = prime * result + (int) (raw ^ (raw >>> 32)); |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean equals(Object obj) { |
| 98 | if (this == obj) |
| 99 | return true; |
| 100 | if (obj == null) |
| 101 | return false; |
| 102 | if (getClass() != obj.getClass()) |
| 103 | return false; |
| 104 | U64 other = (U64) obj; |
| 105 | if (raw != other.raw) |
| 106 | return false; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int getLength() { |
| 112 | return 8; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public U64 applyMask(U64 mask) { |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 117 | return and(mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void writeTo(ChannelBuffer bb) { |
| 122 | bb.writeLong(raw); |
| 123 | } |
| 124 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 125 | @Override |
| 126 | public int compareTo(U64 o) { |
| 127 | return UnsignedLongs.compare(raw, o.raw); |
| 128 | } |
| 129 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void putTo(PrimitiveSink sink) { |
| 132 | sink.putLong(raw); |
| 133 | } |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 134 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public U64 inverse() { |
| 137 | return U64.of(~raw); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public U64 or(U64 other) { |
| 142 | return U64.of(raw | other.raw); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public U64 and(U64 other) { |
| 147 | return ofRaw(raw & other.raw); |
| 148 | } |
| 149 | @Override |
| 150 | public U64 xor(U64 other) { |
| 151 | return U64.of(raw ^ other.raw); |
| 152 | } |
| 153 | |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public U64 add(U64 other) { |
| 156 | return U64.of(this.raw + other.raw); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public U64 subtract(U64 other) { |
| 161 | return U64.of(this.raw - other.raw); |
| 162 | } |
| 163 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 164 | /** return the "numBits" highest-order bits of the hash. |
| 165 | * @param numBits number of higest-order bits to return [0-32]. |
| 166 | * @return a numberic value of the 0-32 highest-order bits. |
| 167 | */ |
| 168 | @Override |
| 169 | public int prefixBits(int numBits) { |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 170 | return HashValueUtils.prefixBits(raw, numBits); |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 173 | public final static Reader READER = new Reader(); |
| 174 | |
| 175 | private static class Reader implements OFMessageReader<U64> { |
| 176 | @Override |
| 177 | public U64 readFrom(ChannelBuffer bb) throws OFParseError { |
| 178 | return U64.ofRaw(bb.readLong()); |
| 179 | } |
| 180 | } |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 181 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public HashValue.Builder<U64> builder() { |
| 184 | return new U64Builder(raw); |
| 185 | } |
| 186 | |
| 187 | static class U64Builder implements Builder<U64> { |
| 188 | long raw; |
| 189 | |
| 190 | public U64Builder(long raw) { |
| 191 | this.raw = raw; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Builder<U64> add(U64 other) { |
| 196 | raw += other.raw; |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Builder<U64> subtract( |
| 202 | U64 other) { |
| 203 | raw -= other.raw; |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Builder<U64> invert() { |
| 209 | raw = ~raw; |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public Builder<U64> or(U64 other) { |
| 215 | raw |= other.raw; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Builder<U64> and(U64 other) { |
| 221 | raw &= other.raw; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Builder<U64> xor(U64 other) { |
| 227 | raw ^= other.raw; |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public U64 build() { |
| 233 | return U64.of(raw); |
| 234 | } |
| 235 | } |
| 236 | |
xinwu | 9b8989c | 2013-12-06 16:01:32 -0800 | [diff] [blame] | 237 | } |