]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedInteger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / primitives / UnsignedInteger.java
1 package org.simantics.databoard.primitives;\r
2 \r
3 \r
4 /**\r
5  * Unsigned 32-bit integer value, either mutable or immutable. \r
6  * The value is between 0 and 4294967295.\r
7  * \r
8  * Example use: \r
9  *    UnsignedInteger x = new UnsignedInteger.Mutable(); \r
10  *    UnsignedInteger y = new UnsignedInteger.Immutable(4); \r
11  *    UnsignedInteger z = UnsingedInteger.valueOf(5);\r
12  *\r
13  * @author Toni Kalajainen <toni.kalajainen@iki.fi>\r
14  */\r
15 public abstract class UnsignedInteger extends Number implements Comparable<Number> {\r
16 \r
17         private static final long serialVersionUID = 1L;\r
18 \r
19         public static final UnsignedInteger MIN_VALUE, MAX_VALUE, ZERO;\r
20         \r
21         static long MASK = 0xFFFFFFFFl;\r
22         public static final long L_MAX_VALUE = 0xFFFFFFFFL;\r
23         public static final long L_MIN_VALUE = 0;\r
24 \r
25         /** Value container */\r
26         int value;\r
27         \r
28     public static UnsignedInteger fromBits(int intBits) {\r
29                 if (intBits>=0 && intBits<CACHE.length) return CACHE[intBits];\r
30                 return UnsignedInteger.Immutable.fromBits(intBits);\r
31     }   \r
32 \r
33     public static UnsignedInteger valueOf(long value) {\r
34                 if ( value>=0 && value<CACHE.length ) return CACHE[(int)value];\r
35         if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");               \r
36                 return new Immutable(value);            \r
37     }\r
38         \r
39         public static class Mutable extends UnsignedInteger {\r
40 \r
41                 private static final long serialVersionUID = 1L;\r
42 \r
43                 public Mutable() {}\r
44                 \r
45                 /**\r
46                  * Create new unsigned integer from a signed integer.\r
47                  * Use #fromBits() to make bitwise conversion\r
48                  * \r
49                  * @param value signed integer\r
50                  * @throws IllegalArgumentException if argument is sub zero\r
51                  */\r
52             public Mutable(int value) throws IllegalArgumentException {\r
53                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
54                 this.value = value;\r
55             }\r
56 \r
57             public Mutable(long value) throws IllegalArgumentException {\r
58                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
59                 this.value = (int) value;\r
60             }\r
61 \r
62             public Mutable(String stringValue) throws IllegalArgumentException {\r
63                 long value = Long.parseLong(stringValue);\r
64                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
65                 this.value = (int) value;\r
66             }   \r
67             \r
68             public static UnsignedInteger fromBits(int intBits) {\r
69                 Mutable result = new Mutable();\r
70                 result.value = intBits;\r
71                 return result;\r
72             }   \r
73             \r
74             public void setBits(int intBits) {\r
75                 this.value = intBits;\r
76             }\r
77             \r
78             public void setValue(int value) {\r
79                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
80                 this.value = value;\r
81             }\r
82             \r
83             public void setValue(long value) {\r
84                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
85                 this.value = (int) value;\r
86             }       \r
87                 \r
88         }\r
89         \r
90         public final static class Immutable extends UnsignedInteger {\r
91 \r
92                 private static final long serialVersionUID = 1L;        \r
93 \r
94                 Immutable() {}\r
95                 \r
96                 /**\r
97                  * Create new unsigned integer from a signed integer.\r
98                  * Use #fromBits() to make bitwise conversion\r
99                  * \r
100                  * @param value signed integer\r
101                  * @throws IllegalArgumentException if argument is sub zero\r
102                  */\r
103             public Immutable(int value) throws IllegalArgumentException {\r
104                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
105                 this.value = value;\r
106             }\r
107 \r
108             public Immutable(long value) throws IllegalArgumentException {\r
109                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
110                 this.value = (int) value;\r
111             }\r
112 \r
113             public Immutable(String stringValue) throws IllegalArgumentException {\r
114                 long value = Long.parseLong(stringValue);\r
115                 if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
116                 this.value = (int) value;\r
117             }   \r
118                                     \r
119         }       \r
120             \r
121     public int toBits() {\r
122         return value;\r
123     }\r
124         \r
125         @Override\r
126         public int intValue() {\r
127                 return value;\r
128         }\r
129         \r
130         @Override\r
131         public long longValue() {\r
132                 return value & MASK;\r
133         }\r
134         \r
135         @Override\r
136         public float floatValue() {\r
137                 return value & MASK;\r
138         }\r
139         @Override\r
140         public double doubleValue() {\r
141                 return value & MASK;\r
142         }\r
143         \r
144     @Override\r
145         public boolean equals(Object obj) {\r
146                 if (obj == null) return false;\r
147                 if (obj == this) return true;\r
148                 \r
149                 if (obj instanceof UnsignedInteger == false) return false;\r
150                 UnsignedInteger other = (UnsignedInteger) obj;\r
151                 return value == other.value;\r
152         }\r
153     \r
154     @Override\r
155     public String toString() {\r
156         return Long.toString(value & MASK);\r
157     }\r
158     \r
159         @Override\r
160         public int compareTo(Number obj) {\r
161         return Long.signum( (value & MASK) - obj.longValue() );\r
162         }\r
163         \r
164         @Override\r
165         public int hashCode() {\r
166                 return value;\r
167         }\r
168 \r
169         // Initialize Cache\r
170         private static int CACHE_SIZE = 16;\r
171         private static UnsignedInteger.Immutable[] CACHE;\r
172         static {\r
173                 CACHE = new UnsignedInteger.Immutable[CACHE_SIZE];\r
174                 for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedInteger.Immutable(i);\r
175                 ZERO = MIN_VALUE = CACHE[0];\r
176                 MAX_VALUE = new UnsignedInteger.Immutable(L_MAX_VALUE);\r
177         }                   \r
178         \r
179 }\r