blob: c0cd475e61ef2d1a6acac7b6af66dd17d96537f6 [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
35 public long getValue() {
36 return raw;
37 }
38
39 public BigInteger getBigInteger() {
Andreas Wundsam16b9d872013-07-18 19:58:10 -070040 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
41 if (raw < 0) {
42 bigInt = bigInt.setBit(Long.SIZE - 1);
43 }
44 return bigInt;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070045 }
46
47 @Override
48 public String toString() {
49 return getBigInteger().toString();
50 }
51
Andreas Wundsam16b9d872013-07-18 19:58:10 -070052 public static BigInteger f(final long value) {
53 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
54 if (value < 0) {
55 bigInt = bigInt.setBit(Long.SIZE - 1);
56 }
57 return bigInt;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070058 }
59
60 public static long t(final BigInteger l) {
61 return l.longValue();
62 }
63}