| 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 |
public final class UnsignedInteger extends Number implements Comparable<UnsignedInteger> |
| 25 |
{ |
| 26 |
private final int _underlying; |
| 27 |
private static final UnsignedInteger[] cachedValues = new UnsignedInteger[256]; |
| 28 |
|
| 29 |
static |
| 30 |
{ |
| 31 |
for(int i = 0; i < 256; i++) |
| 32 |
{ |
| 33 |
cachedValues[i] = new UnsignedInteger(i); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public static final UnsignedInteger ZERO = cachedValues[0]; |
| 38 |
public static final UnsignedInteger ONE = cachedValues[1]; |
| 39 |
public static final UnsignedInteger MAX_VALUE = new UnsignedInteger(0xffffffff); |
| 40 |
|
| 41 |
|
| 42 |
public UnsignedInteger(int underlying) |
| 43 |
{ |
| 44 |
_underlying = underlying; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public int intValue() |
| 49 |
{ |
| 50 |
return _underlying; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public long longValue() |
| 55 |
{ |
| 56 |
return ((long) _underlying) & 0xFFFFFFFFl; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public float floatValue() |
| 61 |
{ |
| 62 |
return (float) longValue(); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public double doubleValue() |
| 67 |
{ |
| 68 |
return (double) longValue(); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean equals(Object o) |
| 73 |
{ |
| 74 |
if (this == o) |
| 75 |
{ |
| 76 |
return true; |
| 77 |
} |
| 78 |
if (o == null || getClass() != o.getClass()) |
| 79 |
{ |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
UnsignedInteger that = (UnsignedInteger) o; |
| 84 |
|
| 85 |
if (_underlying != that._underlying) |
| 86 |
{ |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
public int compareTo(UnsignedInteger o) |
| 94 |
{ |
| 95 |
return Long.signum(longValue() - o.longValue()); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public int hashCode() |
| 100 |
{ |
| 101 |
return _underlying; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public String toString() |
| 106 |
{ |
| 107 |
return String.valueOf(longValue()); |
| 108 |
} |
| 109 |
|
| 110 |
public static UnsignedInteger valueOf(int underlying) |
| 111 |
{ |
| 112 |
if((underlying & 0xFFFFFF00) == 0) |
| 113 |
{ |
| 114 |
return cachedValues[underlying]; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
return new UnsignedInteger(underlying); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public UnsignedInteger add(final UnsignedInteger i) |
| 123 |
{ |
| 124 |
int val = _underlying + i._underlying; |
| 125 |
return UnsignedInteger.valueOf(val); |
| 126 |
} |
| 127 |
|
| 128 |
public UnsignedInteger subtract(final UnsignedInteger i) |
| 129 |
{ |
| 130 |
int val = _underlying - i._underlying; |
| 131 |
return UnsignedInteger.valueOf(val); |
| 132 |
} |
| 133 |
|
| 134 |
public static UnsignedInteger valueOf(final String value) |
| 135 |
{ |
| 136 |
long longVal = Long.parseLong(value); |
| 137 |
return valueOf(longVal); |
| 138 |
} |
| 139 |
|
| 140 |
public static UnsignedInteger valueOf(final long longVal) |
| 141 |
{ |
| 142 |
if(longVal < 0L || longVal >= (1L<<32)) |
| 143 |
{ |
| 144 |
throw new NumberFormatException("Value \""+longVal+"\" lies outside the range [" + 0L + "-" + (1L<<32) +")."); |
| 145 |
} |
| 146 |
return valueOf((int)longVal); |
| 147 |
} |
| 148 |
|
| 149 |
} |