blob: a46ded3d72ff89187c1b35675ff0ab7362493670 [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
27 private U64(final long raw) {
28 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 }
67}