Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | package org.openflow.types; |
| 19 | |
| 20 | import java.math.BigInteger; |
| 21 | |
| 22 | public class U64 { |
Andreas Wundsam | 16b9d87 | 2013-07-18 19:58:10 -0700 | [diff] [blame^] | 23 | private static final long UNSIGNED_MASK = 0x7fffffffffffffffL; |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 24 | |
| 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 Wundsam | 16b9d87 | 2013-07-18 19:58:10 -0700 | [diff] [blame^] | 40 | BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK); |
| 41 | if (raw < 0) { |
| 42 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 43 | } |
| 44 | return bigInt; |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public String toString() { |
| 49 | return getBigInteger().toString(); |
| 50 | } |
| 51 | |
Andreas Wundsam | 16b9d87 | 2013-07-18 19:58:10 -0700 | [diff] [blame^] | 52 | 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 Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public static long t(final BigInteger l) { |
| 61 | return l.longValue(); |
| 62 | } |
| 63 | } |