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