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 | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 27 | import com.google.common.base.Preconditions; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 28 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 29 | import com.google.common.primitives.UnsignedLongs; |
| 30 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 31 | public class U64 implements Writeable, OFValueType<U64>, HashValue<U64> { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 32 | private static final long UNSIGNED_MASK = 0x7fffffffffffffffL; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 33 | private final static long ZERO_VAL = 0; |
| 34 | public final static U64 ZERO = new U64(ZERO_VAL); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 35 | |
| 36 | private final long raw; |
| 37 | |
| 38 | protected U64(final long raw) { |
| 39 | this.raw = raw; |
| 40 | } |
| 41 | |
| 42 | public static U64 of(long raw) { |
| 43 | return ofRaw(raw); |
| 44 | } |
| 45 | |
| 46 | public static U64 ofRaw(final long raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 47 | if(raw == ZERO_VAL) |
| 48 | return ZERO; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 49 | return new U64(raw); |
| 50 | } |
| 51 | |
| 52 | public static U64 parseHex(String hex) { |
| 53 | return new U64(new BigInteger(hex, 16).longValue()); |
| 54 | } |
| 55 | |
| 56 | public long getValue() { |
| 57 | return raw; |
| 58 | } |
| 59 | |
| 60 | public BigInteger getBigInteger() { |
| 61 | BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK); |
| 62 | if (raw < 0) { |
| 63 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 64 | } |
| 65 | return bigInt; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | return getBigInteger().toString(); |
| 71 | } |
| 72 | |
| 73 | public static BigInteger f(final long value) { |
| 74 | BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK); |
| 75 | if (value < 0) { |
| 76 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 77 | } |
| 78 | return bigInt; |
| 79 | } |
| 80 | |
| 81 | public static long t(final BigInteger l) { |
| 82 | return l.longValue(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int hashCode() { |
| 87 | final int prime = 31; |
| 88 | int result = 1; |
| 89 | result = prime * result + (int) (raw ^ (raw >>> 32)); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object obj) { |
| 95 | if (this == obj) |
| 96 | return true; |
| 97 | if (obj == null) |
| 98 | return false; |
| 99 | if (getClass() != obj.getClass()) |
| 100 | return false; |
| 101 | U64 other = (U64) obj; |
| 102 | if (raw != other.raw) |
| 103 | return false; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int getLength() { |
| 109 | return 8; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public U64 applyMask(U64 mask) { |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 114 | return and(mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void writeTo(ChannelBuffer bb) { |
| 119 | bb.writeLong(raw); |
| 120 | } |
| 121 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public int compareTo(U64 o) { |
| 124 | return UnsignedLongs.compare(raw, o.raw); |
| 125 | } |
| 126 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void putTo(PrimitiveSink sink) { |
| 129 | sink.putLong(raw); |
| 130 | } |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 131 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public U64 inverse() { |
| 134 | return U64.of(~raw); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public U64 or(U64 other) { |
| 139 | return U64.of(raw | other.raw); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public U64 and(U64 other) { |
| 144 | return ofRaw(raw & other.raw); |
| 145 | } |
| 146 | @Override |
| 147 | public U64 xor(U64 other) { |
| 148 | return U64.of(raw ^ other.raw); |
| 149 | } |
| 150 | |
| 151 | /** return the "numBits" highest-order bits of the hash. |
| 152 | * @param numBits number of higest-order bits to return [0-32]. |
| 153 | * @return a numberic value of the 0-32 highest-order bits. |
| 154 | */ |
| 155 | @Override |
| 156 | public int prefixBits(int numBits) { |
| 157 | Preconditions.checkArgument(numBits >= 0 && numBits < 32, |
| 158 | "numBits must be in range [0, 32["); |
| 159 | |
| 160 | final int mask = (1 << numBits) -1; |
| 161 | final int shiftDown = 64 - numBits; |
| 162 | |
| 163 | return (int) ((raw >>> shiftDown) & mask); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public U64 combineWithValue(U64 value, int keyBits) { |
| 168 | return U64.of(HashValueUtils.combineWithValue(this.raw, value.raw, keyBits)); |
| 169 | } |
| 170 | |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 171 | public final static Reader READER = new Reader(); |
| 172 | |
| 173 | private static class Reader implements OFMessageReader<U64> { |
| 174 | @Override |
| 175 | public U64 readFrom(ChannelBuffer bb) throws OFParseError { |
| 176 | return U64.ofRaw(bb.readLong()); |
| 177 | } |
| 178 | } |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 179 | |
| 180 | |
xinwu | 9b8989c | 2013-12-06 16:01:32 -0800 | [diff] [blame] | 181 | } |