blob: dea6e6dad4e738275a15556337e6e664feb9a1f6 [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 Wundsam22ba3af2013-10-04 16:00:30 -070027import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070028import com.google.common.primitives.UnsignedLongs;
29
Andreas Wundsambc0aead2014-04-24 19:01:36 -070030public class U64 implements Writeable, OFValueType<U64>, HashValue<U64> {
Yotam Harcholf3f11152013-09-05 16:47:16 -070031 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070032 private final static long ZERO_VAL = 0;
33 public final static U64 ZERO = new U64(ZERO_VAL);
Yotam Harcholf3f11152013-09-05 16:47:16 -070034
35 private final long raw;
36
37 protected U64(final long raw) {
38 this.raw = raw;
39 }
40
41 public static U64 of(long raw) {
42 return ofRaw(raw);
43 }
44
45 public static U64 ofRaw(final long raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070046 if(raw == ZERO_VAL)
47 return ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 return new U64(raw);
49 }
50
51 public static U64 parseHex(String hex) {
52 return new U64(new BigInteger(hex, 16).longValue());
53 }
54
55 public long getValue() {
56 return raw;
57 }
58
59 public BigInteger getBigInteger() {
60 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
61 if (raw < 0) {
62 bigInt = bigInt.setBit(Long.SIZE - 1);
63 }
64 return bigInt;
65 }
66
67 @Override
68 public String toString() {
Andreas Wundsam8ad86992014-05-09 16:33:28 -070069 return String.format("0x%016x", raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 }
71
72 public static BigInteger f(final long value) {
73 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
74 if (value < 0) {
75 bigInt = bigInt.setBit(Long.SIZE - 1);
76 }
77 return bigInt;
78 }
79
80 public static long t(final BigInteger l) {
81 return l.longValue();
82 }
83
84 @Override
85 public int hashCode() {
86 final int prime = 31;
87 int result = 1;
88 result = prime * result + (int) (raw ^ (raw >>> 32));
89 return result;
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj)
95 return true;
96 if (obj == null)
97 return false;
98 if (getClass() != obj.getClass())
99 return false;
100 U64 other = (U64) obj;
101 if (raw != other.raw)
102 return false;
103 return true;
104 }
105
106 @Override
107 public int getLength() {
108 return 8;
109 }
110
111 @Override
112 public U64 applyMask(U64 mask) {
Andreas Wundsambc0aead2014-04-24 19:01:36 -0700113 return and(mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700114 }
115
116 @Override
117 public void writeTo(ChannelBuffer bb) {
118 bb.writeLong(raw);
119 }
120
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700121 @Override
122 public int compareTo(U64 o) {
123 return UnsignedLongs.compare(raw, o.raw);
124 }
125
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700126 @Override
127 public void putTo(PrimitiveSink sink) {
128 sink.putLong(raw);
129 }
xinwuf08ef682013-12-05 18:29:20 -0800130
Andreas Wundsambc0aead2014-04-24 19:01:36 -0700131 @Override
132 public U64 inverse() {
133 return U64.of(~raw);
134 }
135
136 @Override
137 public U64 or(U64 other) {
138 return U64.of(raw | other.raw);
139 }
140
141 @Override
142 public U64 and(U64 other) {
143 return ofRaw(raw & other.raw);
144 }
145 @Override
146 public U64 xor(U64 other) {
147 return U64.of(raw ^ other.raw);
148 }
149
150 /** return the "numBits" highest-order bits of the hash.
151 * @param numBits number of higest-order bits to return [0-32].
152 * @return a numberic value of the 0-32 highest-order bits.
153 */
154 @Override
155 public int prefixBits(int numBits) {
Andreas Wundsamc5e98702014-05-05 13:12:19 -0700156 return HashValueUtils.prefixBits(raw, numBits);
Andreas Wundsambc0aead2014-04-24 19:01:36 -0700157 }
158
159 @Override
160 public U64 combineWithValue(U64 value, int keyBits) {
161 return U64.of(HashValueUtils.combineWithValue(this.raw, value.raw, keyBits));
162 }
163
xinwuf08ef682013-12-05 18:29:20 -0800164 public final static Reader READER = new Reader();
165
166 private static class Reader implements OFMessageReader<U64> {
167 @Override
168 public U64 readFrom(ChannelBuffer bb) throws OFParseError {
169 return U64.ofRaw(bb.readLong());
170 }
171 }
Andreas Wundsambc0aead2014-04-24 19:01:36 -0700172
173
xinwu9b8989c2013-12-06 16:01:32 -0800174}