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