blob: 1353b4210e2e31a4264e9a0e552eda09ef135e0e [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -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 /** return the "numBits" highest-order bits of the hash.
155 * @param numBits number of higest-order bits to return [0-32].
156 * @return a numberic value of the 0-32 highest-order bits.
157 */
158 @Override
159 public int prefixBits(int numBits) {
160 return HashValueUtils.prefixBits(raw, numBits);
161 }
162
163 @Override
164 public U64 combineWithValue(U64 value, int keyBits) {
165 return U64.of(HashValueUtils.combineWithValue(this.raw, value.raw, keyBits));
166 }
167
168 public final static Reader READER = new Reader();
169
170 private static class Reader implements OFMessageReader<U64> {
171 @Override
172 public U64 readFrom(ChannelBuffer bb) throws OFParseError {
173 return U64.ofRaw(bb.readLong());
174 }
175 }
176
177
178}