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