blob: d77d700f8f4029095cef8f19198e5a8e0824f3ac [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;
23import org.projectfloodlight.openflow.protocol.Writeable;
24
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070025import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070026import com.google.common.primitives.UnsignedLongs;
27
Yotam Harcholf3f11152013-09-05 16:47:16 -070028public class U64 implements Writeable, OFValueType<U64> {
29 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070030 private final static long ZERO_VAL = 0;
31 public final static U64 ZERO = new U64(ZERO_VAL);
Yotam Harcholf3f11152013-09-05 16:47:16 -070032
33 private final long raw;
34
35 protected U64(final long raw) {
36 this.raw = raw;
37 }
38
39 public static U64 of(long raw) {
40 return ofRaw(raw);
41 }
42
43 public static U64 ofRaw(final long raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070044 if(raw == ZERO_VAL)
45 return ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070046 return new U64(raw);
47 }
48
49 public static U64 parseHex(String hex) {
50 return new U64(new BigInteger(hex, 16).longValue());
51 }
52
53 public long getValue() {
54 return raw;
55 }
56
57 public BigInteger getBigInteger() {
58 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
59 if (raw < 0) {
60 bigInt = bigInt.setBit(Long.SIZE - 1);
61 }
62 return bigInt;
63 }
64
65 @Override
66 public String toString() {
67 return getBigInteger().toString();
68 }
69
70 public static BigInteger f(final long value) {
71 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
72 if (value < 0) {
73 bigInt = bigInt.setBit(Long.SIZE - 1);
74 }
75 return bigInt;
76 }
77
78 public static long t(final BigInteger l) {
79 return l.longValue();
80 }
81
82 @Override
83 public int hashCode() {
84 final int prime = 31;
85 int result = 1;
86 result = prime * result + (int) (raw ^ (raw >>> 32));
87 return result;
88 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj)
93 return true;
94 if (obj == null)
95 return false;
96 if (getClass() != obj.getClass())
97 return false;
98 U64 other = (U64) obj;
99 if (raw != other.raw)
100 return false;
101 return true;
102 }
103
104 @Override
105 public int getLength() {
106 return 8;
107 }
108
109 @Override
110 public U64 applyMask(U64 mask) {
111 return ofRaw(raw & mask.raw);
112 }
113
114 @Override
115 public void writeTo(ChannelBuffer bb) {
116 bb.writeLong(raw);
117 }
118
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700119 @Override
120 public int compareTo(U64 o) {
121 return UnsignedLongs.compare(raw, o.raw);
122 }
123
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700124 @Override
125 public void putTo(PrimitiveSink sink) {
126 sink.putLong(raw);
127 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700128}