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