| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.apache.qpid.proton.amqp; |
| 23 |
|
| 24 |
import java.math.BigInteger; |
| 25 |
|
| 26 |
public final class UnsignedLong extends Number implements Comparable<UnsignedLong> |
| 27 |
{ |
| 28 |
private static final BigInteger TWO_TO_THE_SIXTY_FOUR = new BigInteger( new byte[] { (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }); |
| 29 |
private static final BigInteger LONG_MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE); |
| 30 |
|
| 31 |
private static final UnsignedLong[] cachedValues = new UnsignedLong[256]; |
| 32 |
|
| 33 |
static |
| 34 |
{ |
| 35 |
for(int i = 0; i<256; i++) |
| 36 |
{ |
| 37 |
cachedValues[i] = new UnsignedLong(i); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public static final UnsignedLong ZERO = cachedValues[0]; |
| 42 |
|
| 43 |
private final long _underlying; |
| 44 |
|
| 45 |
|
| 46 |
public UnsignedLong(long underlying) |
| 47 |
{ |
| 48 |
_underlying = underlying; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public int intValue() |
| 53 |
{ |
| 54 |
return (int) _underlying; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public long longValue() |
| 59 |
{ |
| 60 |
return _underlying; |
| 61 |
} |
| 62 |
|
| 63 |
public BigInteger bigIntegerValue() |
| 64 |
{ |
| 65 |
if(_underlying >= 0L) |
| 66 |
{ |
| 67 |
return BigInteger.valueOf(_underlying); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
return TWO_TO_THE_SIXTY_FOUR.add(BigInteger.valueOf(_underlying)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public float floatValue() |
| 77 |
{ |
| 78 |
return (float) longValue(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public double doubleValue() |
| 83 |
{ |
| 84 |
return (double) longValue(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean equals(Object o) |
| 89 |
{ |
| 90 |
if (this == o) |
| 91 |
{ |
| 92 |
return true; |
| 93 |
} |
| 94 |
if (o == null || getClass() != o.getClass()) |
| 95 |
{ |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
UnsignedLong that = (UnsignedLong) o; |
| 100 |
|
| 101 |
if (_underlying != that._underlying) |
| 102 |
{ |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
public int compareTo(UnsignedLong o) |
| 110 |
{ |
| 111 |
return bigIntegerValue().compareTo(o.bigIntegerValue()); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public int hashCode() |
| 116 |
{ |
| 117 |
return (int)(_underlying ^ (_underlying >>> 32)); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public String toString() |
| 122 |
{ |
| 123 |
return String.valueOf(bigIntegerValue()); |
| 124 |
} |
| 125 |
|
| 126 |
public static UnsignedLong valueOf(long underlying) |
| 127 |
{ |
| 128 |
if((underlying & 0xFFL) == underlying) |
| 129 |
{ |
| 130 |
return cachedValues[(int)underlying]; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
return new UnsignedLong(underlying); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public static UnsignedLong valueOf(final String value) |
| 139 |
{ |
| 140 |
BigInteger bigInt = new BigInteger(value); |
| 141 |
|
| 142 |
return valueOf(bigInt); |
| 143 |
} |
| 144 |
|
| 145 |
public static UnsignedLong valueOf(BigInteger bigInt) |
| 146 |
{ |
| 147 |
if(bigInt.signum() == -1 || bigInt.bitLength() > 64) |
| 148 |
{ |
| 149 |
throw new NumberFormatException("Value \""+bigInt+"\" lies outside the range [0 - 2^64)."); |
| 150 |
} |
| 151 |
else if(bigInt.compareTo(LONG_MAX_VALUE)>=0) |
| 152 |
{ |
| 153 |
return UnsignedLong.valueOf(bigInt.longValue()); |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
return UnsignedLong.valueOf(TWO_TO_THE_SIXTY_FOUR.subtract(bigInt).negate().longValue()); |
| 158 |
} |
| 159 |
} |
| 160 |
} |