1 package org.simantics.databoard.primitives;
5 * Unsigned 16-bit integer value. The value is between 0 and 65535.
7 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
9 public abstract class UnsignedShort extends Number implements Comparable<Number> {
11 private static final long serialVersionUID = 1L;
13 public static final UnsignedShort MIN_VALUE, MAX_VALUE, ZERO;
15 static long MASK = 0xFFFFl;
16 public static final long L_MAX_VALUE = 0xFFFFL;
17 public static final long L_MIN_VALUE = 0;
19 /** Value container */
22 public static UnsignedShort valueOf(long value) {
23 if (value>=0 && value<CACHE.length) return CACHE[(int)value];
24 return new UnsignedShort.Immutable(value);
27 public static UnsignedShort fromBits(int intBits) {
28 if (intBits>=0 && intBits<CACHE.length) return CACHE[intBits];
29 UnsignedShort result = new UnsignedShort.Immutable();
30 result.value = intBits & 0xFFFF;
34 public static class Mutable extends UnsignedShort {
36 private static final long serialVersionUID = 1L;
40 public Mutable(int value) throws IllegalArgumentException {
41 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
45 public Mutable(long value) throws IllegalArgumentException {
46 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
47 this.value = (int) value;
50 public Mutable(String stringValue) throws IllegalArgumentException {
51 long value = Long.parseLong(stringValue);
52 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
53 this.value = (int) value;
56 public static UnsignedShort fromBits(int intBits) {
57 UnsignedShort result = new Mutable();
58 result.value = intBits & 0xFFFF;
62 public void setBits(int intBits) {
66 public void setValue(int value) {
67 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
71 public void setValue(long value) {
72 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
73 this.value = (int) value;
78 public static class Immutable extends UnsignedShort {
80 private static final long serialVersionUID = 1L;
84 public Immutable(int value) throws IllegalArgumentException {
85 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
89 public Immutable(long value) throws IllegalArgumentException {
90 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
91 this.value = (int) value;
94 public Immutable(String stringValue) throws IllegalArgumentException {
95 long value = Long.parseLong(stringValue);
96 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
97 this.value = (int) value;
100 public static UnsignedShort fromBits(int intBits) {
101 UnsignedShort result = new Immutable();
102 result.value = intBits & 0xFFFF;
109 public int toBits() {
114 public int intValue() {
119 public long longValue() {
124 public float floatValue() {
128 public double doubleValue() {
133 public boolean equals(Object obj) {
134 if (obj == null) return false;
135 if (obj == this) return true;
137 if (obj instanceof UnsignedShort == false) return false;
138 UnsignedShort other = (UnsignedShort) obj;
139 return value == other.value;
143 public String toString() {
144 return Long.toString(value & MASK);
148 public int compareTo(Number obj) {
149 return Long.signum( (value & MASK) - obj.longValue() );
153 public int hashCode() {
158 private static int CACHE_SIZE = 16;
159 private static UnsignedShort.Immutable[] CACHE;
161 CACHE = new UnsignedShort.Immutable[CACHE_SIZE];
162 for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedShort.Immutable(i);
163 ZERO = MIN_VALUE = CACHE[0];
164 MAX_VALUE = new UnsignedShort.Immutable(L_MAX_VALUE);