1 19 20 package com.maverick.util; 21 22 import java.math.BigInteger ; 23 24 29 public class UnsignedInteger64 { 30 31 32 public final static BigInteger MAX_VALUE = new BigInteger ( 33 "18446744073709551615"); 34 35 36 public final static BigInteger MIN_VALUE = new BigInteger ("0"); 37 private BigInteger bigInt; 38 39 46 public UnsignedInteger64(String sval) throws NumberFormatException { 47 bigInt = new BigInteger (sval); 48 49 if ( (bigInt.compareTo(MIN_VALUE) < 0) 50 || (bigInt.compareTo(MAX_VALUE) > 0)) { 51 throw new NumberFormatException (); 52 } 53 } 54 55 62 public UnsignedInteger64(byte[] bval) throws NumberFormatException { 63 bigInt = new BigInteger (bval); 64 65 if ( (bigInt.compareTo(MIN_VALUE) < 0) 66 || (bigInt.compareTo(MAX_VALUE) > 0)) { 67 throw new NumberFormatException (); 68 } 69 } 70 71 public UnsignedInteger64(long value) { 72 bigInt = BigInteger.valueOf(value); 73 } 74 75 82 public UnsignedInteger64(BigInteger input) { 83 bigInt = new BigInteger (input.toString()); 84 85 if ( (bigInt.compareTo(MIN_VALUE) < 0) 86 || (bigInt.compareTo(MAX_VALUE) > 0)) { 87 throw new NumberFormatException (); 88 } 89 } 90 91 99 public boolean equals(Object o) { 100 try { 101 UnsignedInteger64 u = (UnsignedInteger64) o; 102 103 return u.bigInt.equals(this.bigInt); 104 } 105 catch (ClassCastException ce) { 106 return false; 108 } 109 } 110 111 116 public BigInteger bigIntValue() { 117 return bigInt; 118 } 119 120 125 public long longValue() { 126 return bigInt.longValue(); 127 } 128 129 130 135 public String toString() { 136 return bigInt.toString(10); 137 } 138 139 144 public int hashCode() { 145 return bigInt.hashCode(); 146 } 147 148 156 public static UnsignedInteger64 add(UnsignedInteger64 x, UnsignedInteger64 y) { 157 return new UnsignedInteger64(x.bigInt.add(y.bigInt)); 158 } 159 160 168 public static UnsignedInteger64 add(UnsignedInteger64 x, int y) { 169 return new UnsignedInteger64(x.bigInt.add(BigInteger.valueOf(y))); 170 } 171 172 176 public byte[] toByteArray() { 177 byte[] raw = new byte[8]; 178 byte[] bi = bigIntValue().toByteArray(); 179 System.arraycopy(bi, 0, raw, raw.length - bi.length, bi.length); 180 return raw; 181 } 182 } 183 | Popular Tags |