]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedByte.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / primitives / UnsignedByte.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedByte.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedByte.java
new file mode 100644 (file)
index 0000000..8283b68
--- /dev/null
@@ -0,0 +1,161 @@
+package org.simantics.databoard.primitives;\r
+\r
+\r
+\r
+/**\r
+ * Unsigned 8-bit integer value. The value is between 0 and 255.\r
+ *\r
+ * @author Toni Kalajainen <toni.kalajainen@iki.fi>\r
+ */\r
+public abstract class UnsignedByte extends Number implements Comparable<Number> {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       public static final UnsignedByte MIN_VALUE, MAX_VALUE, ZERO;\r
+       \r
+       static long MASK = 0xFFl;\r
+       public static final long L_MAX_VALUE = 0xFFL;\r
+       public static final long L_MIN_VALUE = 0;\r
+\r
+       /** Value container */\r
+       int value;\r
+\r
+    public static UnsignedByte valueOf(long value) {\r
+               if (value>=0 && value<CACHE.length) return CACHE[(int)value];\r
+               return new UnsignedByte.Immutable(value);       \r
+    }\r
+    \r
+    public static UnsignedByte fromBits(int intBits) {\r
+               if (intBits>=0 && intBits<CACHE.length) return CACHE[intBits];\r
+       UnsignedByte result = new UnsignedByte.Immutable();\r
+       result.value = intBits & 0xFFFF;\r
+       return result;\r
+    }  \r
+       \r
+       public static class Mutable extends UnsignedByte {\r
+               \r
+               private static final long serialVersionUID = 1L;\r
+\r
+               Mutable() {}\r
+               \r
+           public Mutable(int value) throws IllegalArgumentException {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = value;\r
+           }\r
+\r
+           public Mutable(long value) throws IllegalArgumentException {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+\r
+           public Mutable(String stringValue) throws IllegalArgumentException {\r
+               long value = Long.parseLong(stringValue);\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }   \r
+           \r
+           public static Mutable fromBits(int intBits) {\r
+               Mutable result = new Mutable();\r
+               result.value = intBits;\r
+               return result;\r
+           }               \r
+               \r
+           public void setBits(int intBits) {\r
+               this.value = intBits;\r
+           }\r
+           \r
+           public void setValue(int value) {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = value;\r
+           }\r
+           \r
+           public void setValue(long value) {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+           \r
+       }\r
+       \r
+       public static final class Immutable extends UnsignedByte {\r
+\r
+               private static final long serialVersionUID = 1L;\r
+               \r
+               Immutable() {}\r
+               \r
+           public Immutable(int value) throws IllegalArgumentException {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = value;\r
+           }\r
+\r
+           public Immutable(long value) throws IllegalArgumentException {\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+\r
+           public Immutable(String stringValue) throws IllegalArgumentException {\r
+               long value = Long.parseLong(stringValue);\r
+               if ( value<L_MIN_VALUE || value>L_MAX_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }   \r
+               \r
+       }\r
+       \r
+    public int toBits() {\r
+       return value;\r
+    }\r
+       \r
+       @Override\r
+       public int intValue() {\r
+               return value;\r
+       }\r
+       \r
+       @Override\r
+       public long longValue() {\r
+               return value & MASK;\r
+       }\r
+       \r
+       @Override\r
+       public float floatValue() {\r
+               return value & MASK;\r
+       }\r
+       @Override\r
+       public double doubleValue() {\r
+               return value & MASK;\r
+       }\r
+       \r
+    @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj == null) return false;\r
+               if (obj == this) return true;\r
+               \r
+               if (obj instanceof UnsignedByte == false) return false;\r
+               UnsignedByte other = (UnsignedByte) obj;\r
+               return value == other.value;\r
+       }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return Long.toString(value & MASK);\r
+    }\r
+    \r
+       @Override\r
+       public int compareTo(Number obj) {\r
+        return Long.signum( (value & MASK) - obj.longValue() );\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               return value;\r
+       }\r
+\r
+       // Initialize Cache\r
+       private static int CACHE_SIZE = 16;\r
+       private static UnsignedByte.Immutable[] CACHE;\r
+       static {\r
+               CACHE = new UnsignedByte.Immutable[CACHE_SIZE];\r
+               for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedByte.Immutable(i);\r
+               ZERO = MIN_VALUE = CACHE[0];\r
+               MAX_VALUE = new UnsignedByte.Immutable(L_MAX_VALUE);\r
+       }\r
+\r
+}\r