blob: f15544f9c4b4d4e4885418ab490d4c1b55a14482 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -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;
23import org.projectfloodlight.openflow.exceptions.OFParseError;
24import org.projectfloodlight.openflow.protocol.OFMessageReader;
25import org.projectfloodlight.openflow.protocol.Writeable;
26
27import com.google.common.hash.PrimitiveSink;
28import com.google.common.primitives.UnsignedLongs;
29
30public class U64 implements Writeable, OFValueType<U64>, HashValue<U64> {
31 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
32 private final static long ZERO_VAL = 0;
33 public final static U64 ZERO = new U64(ZERO_VAL);
34
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
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) {
50 if(raw == ZERO_VAL)
51 return ZERO;
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() {
73 return String.format("0x%016x", raw);
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) {
117 return and(mask);
118 }
119
120 @Override
121 public void writeTo(ChannelBuffer bb) {
122 bb.writeLong(raw);
123 }
124
125 @Override
126 public int compareTo(U64 o) {
127 return UnsignedLongs.compare(raw, o.raw);
128 }
129
130 @Override
131 public void putTo(PrimitiveSink sink) {
132 sink.putLong(raw);
133 }
134
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
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
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) {
170 return HashValueUtils.prefixBits(raw, numBits);
171 }
172
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 }
181
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
237}