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