]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedLong.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / primitives / UnsignedLong.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedLong.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/primitives/UnsignedLong.java
new file mode 100644 (file)
index 0000000..9761a14
--- /dev/null
@@ -0,0 +1,200 @@
+package org.simantics.databoard.primitives;\r
+\r
+import java.math.BigInteger;\r
+\r
+\r
+/**\r
+ * Unsigned immutable 64-bit integer value. The value is between 0 and 18446744073709551615.\r
+ *\r
+ * @author Toni Kalajainen <toni.kalajainen@iki.fi>\r
+ */\r
+public abstract class UnsignedLong extends Number implements Comparable<Number> {\r
+\r
+       static final long serialVersionUID = 1L;\r
+       static final long L_MIN_VALUE = 0;\r
+       static final BigInteger HALF = new BigInteger("9223372036854775808"); // 2^63\r
+       static final BigInteger BI_MIN_VALUE = new BigInteger("0");\r
+       static final BigInteger BI_MAX_VALUE = new BigInteger("18446744073709551615"); // 2^64-1\r
+       \r
+       public static final UnsignedLong MIN_VALUE, MAX_VALUE, ZERO;\r
+       \r
+\r
+       /** Value container */\r
+       long value;\r
+       \r
+       /**\r
+        * Get cached or create new immutable unsigned long\r
+        *  \r
+        * @param value\r
+        * @return immutable unsigned long\r
+        */\r
+    public static UnsignedLong valueOf(long value) {\r
+               if ( value>=0 && value<CACHE.length ) return CACHE[(int)value];\r
+       if ( value<L_MIN_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               return new UnsignedLong.Immutable(value);       \r
+    }    \r
+       \r
+    /**\r
+     * Get cached or create new immutable unsigned long\r
+     * \r
+     * @param bits long bits\r
+     * @return immutable unsigned long\r
+     */\r
+    public static UnsignedLong fromBits(long bits) {\r
+               if (bits>=0 && bits<CACHE.length) return CACHE[(int)bits];\r
+       UnsignedLong result = new UnsignedLong.Immutable();     \r
+       result.value = bits;\r
+       return result;\r
+    }\r
+    \r
+       public static class Mutable extends UnsignedLong {\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 ) 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 ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+\r
+           public Mutable(BigInteger value) throws IllegalArgumentException {\r
+                       if (value.compareTo(BI_MIN_VALUE)<0 || value.compareTo(BI_MAX_VALUE)>0) throw new IllegalArgumentException("Argument is not within range");             \r
+                       this.value = value.compareTo(HALF)<0 ? value.longValue() : value.subtract(HALF).longValue() | 0x80000000l;\r
+               }\r
+           \r
+           public Mutable(String stringValue) throws IllegalArgumentException {\r
+               long value = Long.parseLong(stringValue);\r
+               if ( value<L_MIN_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }   \r
+           \r
+           public static Mutable fromBits(long bits) {\r
+               Mutable result = new Mutable();\r
+               result.value = bits;\r
+               return result;\r
+           }       \r
+           \r
+           public void setFromBits(int intBits) {\r
+               this.value = intBits;\r
+           }\r
+           \r
+           public void setValue(int value) {\r
+               if ( value<L_MIN_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 ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+\r
+           public void setValue(BigInteger value) {\r
+                       if (value.compareTo(BI_MIN_VALUE)<0 || value.compareTo(BI_MAX_VALUE)>0) throw new IllegalArgumentException("Argument is not within range");             \r
+                       this.value = value.compareTo(HALF)<0 ? value.longValue() : value.subtract(HALF).longValue() | 0x80000000l;\r
+           }       \r
+           \r
+       }\r
+       \r
+       public final static class Immutable extends UnsignedLong {\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 ) 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 ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }\r
+\r
+           public Immutable(BigInteger value) throws IllegalArgumentException {\r
+                       if (value.compareTo(BI_MIN_VALUE)<0 || value.compareTo(BI_MAX_VALUE)>0) throw new IllegalArgumentException("Argument is not within range");             \r
+                       this.value = value.compareTo(HALF)<0 ? value.longValue() : value.subtract(HALF).longValue() | 0x80000000l;\r
+               }\r
+           \r
+           public Immutable(String stringValue) throws IllegalArgumentException {\r
+               long value = Long.parseLong(stringValue);\r
+               if ( value<L_MIN_VALUE ) throw new IllegalArgumentException("Argument is not within range");\r
+               this.value = (int) value;\r
+           }   \r
+           \r
+       }       \r
+       \r
+    public long toBits() {\r
+       return value;\r
+    }\r
+       \r
+       @Override\r
+       public int intValue() {\r
+               return (int) value;\r
+       }\r
+       \r
+       @Override\r
+       public long longValue() {\r
+               return value;\r
+       }\r
+       \r
+       @Override\r
+       public float floatValue() {\r
+               return value;\r
+       }\r
+       @Override\r
+       public double doubleValue() {\r
+               return value;\r
+       }\r
+       \r
+       /**\r
+        * Create big integer value\r
+        * \r
+        * @return new big integer\r
+        */\r
+       public BigInteger toBigInteger() {\r
+               return value>=0 ? BigInteger.valueOf(value) : BigInteger.valueOf(value & 0x7fffffff).add(HALF); \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 UnsignedLong == false) return false;\r
+               UnsignedLong other = (UnsignedLong) obj;\r
+               return value == other.value;\r
+       }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return value>=0 ? Long.toString(value) : toBigInteger().toString();\r
+    }\r
+    \r
+       @Override\r
+       public int compareTo(Number obj) {\r
+               return value>=0 ? Long.signum( value - obj.longValue() ) : 1;\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+           return (int)value | (int)(value>>32);\r
+       }\r
+\r
+       // Initialize Cache\r
+       private static int CACHE_SIZE = 16;\r
+       private static UnsignedLong.Immutable[] CACHE;\r
+       static {\r
+               CACHE = new UnsignedLong.Immutable[CACHE_SIZE];\r
+               for (int i=0; i<CACHE_SIZE; i++) CACHE[i] = new UnsignedLong.Immutable(i);\r
+               ZERO = MIN_VALUE = CACHE[0];\r
+               MAX_VALUE = UnsignedLong.fromBits(0xFFFFFFFF);\r
+       }\r
+\r
+}\r