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