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