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