blob: dd62cd0e9a47509c8dff6643813de92c3bb0d521 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001/**
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
18package org.projectfloodlight.openflow.types;
19
20import java.math.BigInteger;
21
22import org.jboss.netty.buffer.ChannelBuffer;
xinwuf08ef682013-12-05 18:29:20 -080023import org.projectfloodlight.openflow.exceptions.OFParseError;
24import org.projectfloodlight.openflow.protocol.OFMessageReader;
Yotam Harcholf3f11152013-09-05 16:47:16 -070025import org.projectfloodlight.openflow.protocol.Writeable;
26
Andreas Wundsambc0aead2014-04-24 19:01:36 -070027import com.google.common.base.Preconditions;
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070028import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070029import com.google.common.primitives.UnsignedLongs;
30
Andreas Wundsambc0aead2014-04-24 19:01:36 -070031public class U64 implements Writeable, OFValueType<U64>, HashValue<U64> {
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070033 private final static long ZERO_VAL = 0;
34 public final static U64 ZERO = new U64(ZERO_VAL);
Yotam Harcholf3f11152013-09-05 16:47:16 -070035
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 Wundsamb75c4ad2013-09-23 14:45:35 -070047 if(raw == ZERO_VAL)
48 return ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070049 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 Wundsambc0aead2014-04-24 19:01:36 -0700114 return and(mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700115 }
116
117 @Override
118 public void writeTo(ChannelBuffer bb) {
119 bb.writeLong(raw);
120 }
121
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700122 @Override
123 public int compareTo(U64 o) {
124 return UnsignedLongs.compare(raw, o.raw);
125 }
126
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700127 @Override
128 public void putTo(PrimitiveSink sink) {
129 sink.putLong(raw);
130 }
xinwuf08ef682013-12-05 18:29:20 -0800131
Andreas Wundsambc0aead2014-04-24 19:01:36 -0700132 @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
xinwuf08ef682013-12-05 18:29:20 -0800171 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 Wundsambc0aead2014-04-24 19:01:36 -0700179
180
xinwu9b8989c2013-12-06 16:01:32 -0800181}