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 | |
Andreas Wundsam | f4abdd3 | 2013-08-01 22:12:06 -0700 | [diff] [blame] | 35 | public static U64 parseHex(String hex) { |
| 36 | return new U64(new BigInteger(hex, 16).longValue()); |
| 37 | } |
| 38 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 39 | public long getValue() { |
| 40 | return raw; |
| 41 | } |
| 42 | |
| 43 | public BigInteger getBigInteger() { |
Andreas Wundsam | 16b9d87 | 2013-07-18 19:58:10 -0700 | [diff] [blame] | 44 | BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK); |
| 45 | if (raw < 0) { |
| 46 | bigInt = bigInt.setBit(Long.SIZE - 1); |
| 47 | } |
| 48 | return bigInt; |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String toString() { |
| 53 | return getBigInteger().toString(); |
| 54 | } |
| 55 | |
Andreas Wundsam | 16b9d87 | 2013-07-18 19:58:10 -0700 | [diff] [blame] | 56 | 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 Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public static long t(final BigInteger l) { |
| 65 | return l.longValue(); |
| 66 | } |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame^] | 67 | |
| 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 Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 90 | } |