1 package org.simantics.databoard.primitives;
\r
6 * Unsigned 8-bit integer value. The value is between 0 and 255.
\r
8 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
\r
10 public abstract class UnsignedByte extends Number implements Comparable<Number> {
\r
12 private static final long serialVersionUID = 1L;
\r
14 public static final UnsignedByte MIN_VALUE, MAX_VALUE, ZERO;
\r
16 static long MASK = 0xFFl;
\r
17 public static final long L_MAX_VALUE = 0xFFL;
\r
18 public static final long L_MIN_VALUE = 0;
\r
20 /** Value container */
\r
23 public static UnsignedByte valueOf(long value) {
\r
24 if (value>=0 && value<CACHE.length) return CACHE[(int)value];
\r
25 return new UnsignedByte.Immutable(value);
\r
28 public static UnsignedByte fromBits(int intBits) {
\r
29 if (intBits>=0 && intBits<CACHE.length) return CACHE[intBits];
\r
30 UnsignedByte result = new UnsignedByte.Immutable();
\r
31 result.value = intBits & 0xFFFF;
\r
35 public static class Mutable extends UnsignedByte {
\r
37 private static final long serialVersionUID = 1L;
\r
41 public Mutable(int value) throws IllegalArgumentException {
\r
42 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
46 public Mutable(long value) throws IllegalArgumentException {
\r
47 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
48 this.value = (int) value;
\r
51 public Mutable(String stringValue) throws IllegalArgumentException {
\r
52 long value = Long.parseLong(stringValue);
\r
53 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
54 this.value = (int) value;
\r
57 public static Mutable fromBits(int intBits) {
\r
58 Mutable result = new Mutable();
\r
59 result.value = intBits;
\r
63 public void setBits(int intBits) {
\r
64 this.value = intBits;
\r
67 public void setValue(int value) {
\r
68 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
72 public void setValue(long value) {
\r
73 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
74 this.value = (int) value;
\r
79 public static final class Immutable extends UnsignedByte {
\r
81 private static final long serialVersionUID = 1L;
\r
85 public Immutable(int value) throws IllegalArgumentException {
\r
86 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
90 public Immutable(long value) throws IllegalArgumentException {
\r
91 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
92 this.value = (int) value;
\r
95 public Immutable(String stringValue) throws IllegalArgumentException {
\r
96 long value = Long.parseLong(stringValue);
\r
97 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");
\r
98 this.value = (int) value;
\r
103 public int toBits() {
\r
108 public int intValue() {
\r
113 public long longValue() {
\r
114 return value & MASK;
\r
118 public float floatValue() {
\r
119 return value & MASK;
\r
122 public double doubleValue() {
\r
123 return value & MASK;
\r
127 public boolean equals(Object obj) {
\r
128 if (obj == null) return false;
\r
129 if (obj == this) return true;
\r
131 if (obj instanceof UnsignedByte == false) return false;
\r
132 UnsignedByte other = (UnsignedByte) obj;
\r
133 return value == other.value;
\r
137 public String toString() {
\r
138 return Long.toString(value & MASK);
\r
142 public int compareTo(Number obj) {
\r
143 return Long.signum( (value & MASK) - obj.longValue() );
\r
147 public int hashCode() {
\r
151 // Initialize Cache
\r
152 private static int CACHE_SIZE = 16;
\r
153 private static UnsignedByte.Immutable[] CACHE;
\r
155 CACHE = new UnsignedByte.Immutable[CACHE_SIZE];
\r
156 for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedByte.Immutable(i);
\r
157 ZERO = MIN_VALUE = CACHE[0];
\r
158 MAX_VALUE = new UnsignedByte.Immutable(L_MAX_VALUE);
\r