blob: 7871445d92052b11518219e22f4cf1464f7a21e7 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -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.openflow.types;
19
20import java.math.BigInteger;
21
22public class U64 {
Andreas Wundsam16b9d872013-07-18 19:58:10 -070023 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070024
25 private final long raw;
26
Yotam Harchol6fccde62013-08-15 12:04:52 -070027 protected U64(final long raw) {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070028 this.raw = raw;
29 }
30
31 public static U64 of(final long raw) {
32 return new U64(raw);
33 }
34
Andreas Wundsamf4abdd32013-08-01 22:12:06 -070035 public static U64 parseHex(String hex) {
36 return new U64(new BigInteger(hex, 16).longValue());
37 }
38
Andreas Wundsam40e14f72013-05-06 14:49:08 -070039 public long getValue() {
40 return raw;
41 }
42
43 public BigInteger getBigInteger() {
Andreas Wundsam16b9d872013-07-18 19:58:10 -070044 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
45 if (raw < 0) {
46 bigInt = bigInt.setBit(Long.SIZE - 1);
47 }
48 return bigInt;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070049 }
50
51 @Override
52 public String toString() {
53 return getBigInteger().toString();
54 }
55
Andreas Wundsam16b9d872013-07-18 19:58:10 -070056 public static BigInteger f(final long value) {
57 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
58 if (value < 0) {
59 bigInt = bigInt.setBit(Long.SIZE - 1);
60 }
61 return bigInt;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070062 }
63
64 public static long t(final BigInteger l) {
65 return l.longValue();
66 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070067
68 @Override
69 public int hashCode() {
70 final int prime = 31;
71 int result = 1;
72 result = prime * result + (int) (raw ^ (raw >>> 32));
73 return result;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj)
79 return true;
80 if (obj == null)
81 return false;
82 if (getClass() != obj.getClass())
83 return false;
84 U64 other = (U64) obj;
85 if (raw != other.raw)
86 return false;
87 return true;
88 }
89
Andreas Wundsam40e14f72013-05-06 14:49:08 -070090}