1 package org.simantics.databoard.primitives;
\r
5 * Unsigned 32-bit integer value, either mutable or immutable.
\r
6 * The value is between 0 and 4294967295.
\r
9 * UnsignedInteger x = new UnsignedInteger.Mutable();
\r
10 * UnsignedInteger y = new UnsignedInteger.Immutable(4);
\r
11 * UnsignedInteger z = UnsingedInteger.valueOf(5);
\r
13 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
\r
15 public abstract class UnsignedInteger extends Number implements Comparable<Number> {
\r
17 private static final long serialVersionUID = 1L;
\r
19 public static final UnsignedInteger MIN_VALUE, MAX_VALUE, ZERO;
\r
21 static long MASK = 0xFFFFFFFFl;
\r
22 public static final long L_MAX_VALUE = 0xFFFFFFFFL;
\r
23 public static final long L_MIN_VALUE = 0;
\r
25 /** Value container */
\r
28 public static UnsignedInteger fromBits(int intBits) {
\r
29 if (intBits>=0 && intBits<CACHE.length) return CACHE[intBits];
\r
30 return UnsignedInteger.Immutable.fromBits(intBits);
\r
33 public static UnsignedInteger valueOf(long value) {
\r
34 if ( value>=0 && value<CACHE.length ) return CACHE[(int)value];
\r
35 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
36 return new Immutable(value);
\r
39 public static class Mutable extends UnsignedInteger {
\r
41 private static final long serialVersionUID = 1L;
\r
46 * Create new unsigned integer from a signed integer.
\r
47 * Use #fromBits() to make bitwise conversion
\r
49 * @param value signed integer
\r
50 * @throws IllegalArgumentException if argument is sub zero
\r
52 public Mutable(int value) throws IllegalArgumentException {
\r
53 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
57 public Mutable(long value) throws IllegalArgumentException {
\r
58 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
59 this.value = (int) value;
\r
62 public Mutable(String stringValue) throws IllegalArgumentException {
\r
63 long value = Long.parseLong(stringValue);
\r
64 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
65 this.value = (int) value;
\r
68 public static UnsignedInteger fromBits(int intBits) {
\r
69 Mutable result = new Mutable();
\r
70 result.value = intBits;
\r
74 public void setBits(int intBits) {
\r
75 this.value = intBits;
\r
78 public void setValue(int value) {
\r
79 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
83 public void setValue(long value) {
\r
84 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
85 this.value = (int) value;
\r
90 public final static class Immutable extends UnsignedInteger {
\r
92 private static final long serialVersionUID = 1L;
\r
97 * Create new unsigned integer from a signed integer.
\r
98 * Use #fromBits() to make bitwise conversion
\r
100 * @param value signed integer
\r
101 * @throws IllegalArgumentException if argument is sub zero
\r
103 public Immutable(int value) throws IllegalArgumentException {
\r
104 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
105 this.value = value;
\r
108 public Immutable(long value) throws IllegalArgumentException {
\r
109 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
110 this.value = (int) value;
\r
113 public Immutable(String stringValue) throws IllegalArgumentException {
\r
114 long value = Long.parseLong(stringValue);
\r
115 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
116 this.value = (int) value;
\r
121 public int toBits() {
\r
126 public int intValue() {
\r
131 public long longValue() {
\r
132 return value & MASK;
\r
136 public float floatValue() {
\r
137 return value & MASK;
\r
140 public double doubleValue() {
\r
141 return value & MASK;
\r
145 public boolean equals(Object obj) {
\r
146 if (obj == null) return false;
\r
147 if (obj == this) return true;
\r
149 if (obj instanceof UnsignedInteger == false) return false;
\r
150 UnsignedInteger other = (UnsignedInteger) obj;
\r
151 return value == other.value;
\r
155 public String toString() {
\r
156 return Long.toString(value & MASK);
\r
160 public int compareTo(Number obj) {
\r
161 return Long.signum( (value & MASK) - obj.longValue() );
\r
165 public int hashCode() {
\r
169 // Initialize Cache
\r
170 private static int CACHE_SIZE = 16;
\r
171 private static UnsignedInteger.Immutable[] CACHE;
\r
173 CACHE = new UnsignedInteger.Immutable[CACHE_SIZE];
\r
174 for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedInteger.Immutable(i);
\r
175 ZERO = MIN_VALUE = CACHE[0];
\r
176 MAX_VALUE = new UnsignedInteger.Immutable(L_MAX_VALUE);
\r