]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedByte.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / primitives / UnsignedByte.java
1 package org.simantics.databoard.primitives;\r
2 \r
3 \r
4 \r
5 /**\r
6  * Unsigned 8-bit integer value. The value is between 0 and 255.\r
7  *\r
8  * @author Toni Kalajainen <toni.kalajainen@iki.fi>\r
9  */\r
10 public abstract class UnsignedByte extends Number implements Comparable<Number> {\r
11 \r
12         private static final long serialVersionUID = 1L;\r
13 \r
14         public static final UnsignedByte MIN_VALUE, MAX_VALUE, ZERO;\r
15         \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
19 \r
20         /** Value container */\r
21         int value;\r
22 \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
26     }\r
27     \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
32         return result;\r
33     }   \r
34         \r
35         public static class Mutable extends UnsignedByte {\r
36                 \r
37                 private static final long serialVersionUID = 1L;\r
38 \r
39                 Mutable() {}\r
40                 \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
43                 this.value = value;\r
44             }\r
45 \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
49             }\r
50 \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
55             }   \r
56             \r
57             public static Mutable fromBits(int intBits) {\r
58                 Mutable result = new Mutable();\r
59                 result.value = intBits;\r
60                 return result;\r
61             }               \r
62                 \r
63             public void setBits(int intBits) {\r
64                 this.value = intBits;\r
65             }\r
66             \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
69                 this.value = value;\r
70             }\r
71             \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
75             }\r
76             \r
77         }\r
78         \r
79         public static final class Immutable extends UnsignedByte {\r
80 \r
81                 private static final long serialVersionUID = 1L;\r
82                 \r
83                 Immutable() {}\r
84                 \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
87                 this.value = value;\r
88             }\r
89 \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
93             }\r
94 \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
99             }   \r
100                 \r
101         }\r
102         \r
103     public int toBits() {\r
104         return value;\r
105     }\r
106         \r
107         @Override\r
108         public int intValue() {\r
109                 return value;\r
110         }\r
111         \r
112         @Override\r
113         public long longValue() {\r
114                 return value & MASK;\r
115         }\r
116         \r
117         @Override\r
118         public float floatValue() {\r
119                 return value & MASK;\r
120         }\r
121         @Override\r
122         public double doubleValue() {\r
123                 return value & MASK;\r
124         }\r
125         \r
126     @Override\r
127         public boolean equals(Object obj) {\r
128                 if (obj == null) return false;\r
129                 if (obj == this) return true;\r
130                 \r
131                 if (obj instanceof UnsignedByte == false) return false;\r
132                 UnsignedByte other = (UnsignedByte) obj;\r
133                 return value == other.value;\r
134         }\r
135     \r
136     @Override\r
137     public String toString() {\r
138         return Long.toString(value & MASK);\r
139     }\r
140     \r
141         @Override\r
142         public int compareTo(Number obj) {\r
143         return Long.signum( (value & MASK) - obj.longValue() );\r
144         }\r
145         \r
146         @Override\r
147         public int hashCode() {\r
148                 return value;\r
149         }\r
150 \r
151         // Initialize Cache\r
152         private static int CACHE_SIZE = 16;\r
153         private static UnsignedByte.Immutable[] CACHE;\r
154         static {\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
159         }\r
160 \r
161 }\r