1 23 package com.lutris.appserver.server.sql; 24 25 import java.io.Serializable ; 26 import java.math.BigDecimal ; 27 import java.math.BigInteger ; 28 29 40 public class ObjectId implements Serializable { 41 42 45 private BigDecimal value; 46 47 50 static final public BigDecimal ONE = new BigDecimal ("1"); 51 52 56 static final public BigDecimal MAX = new BigDecimal ("9999999999999999999"); 57 58 75 public ObjectId(String val, int radix) 76 throws ObjectIdException, NumberFormatException { 77 value = new BigDecimal (new BigInteger (val, radix), 0); 78 if (value.signum() < 0) { 79 throw new ObjectIdException("Object IDs cannot be negative."); 80 } 81 if (value.compareTo(MAX) > 0) { 82 throw new ObjectIdException("Object IDs cannot exceed " 83 + MAX.toString()); 84 } 85 } 86 87 101 public ObjectId(String val) 102 throws ObjectIdException, NumberFormatException { 103 value = new BigDecimal (new BigInteger (val), 0); 104 if (value.signum() < 0) { 105 throw new ObjectIdException("Object IDs cannot be negative."); 106 } 107 if (value.compareTo(MAX) > 0) { 108 throw new ObjectIdException("Object IDs cannot exceed " 109 + MAX.toString()); 110 } 111 } 112 113 121 public ObjectId(long val) 122 throws ObjectIdException { 123 if (val < 0) { 124 throw new ObjectIdException("Object IDs cannot be negative."); 125 } 126 value = BigDecimal.valueOf(val); 127 if (value.compareTo(MAX) > 0) { 128 throw new ObjectIdException("Object IDs cannot exceed " 129 + MAX.toString()); 130 } 131 } 132 133 142 public ObjectId(BigDecimal val) 143 throws ObjectIdException { 144 if (val.signum() < 0) { 145 throw new ObjectIdException("Object IDs cannot be negative."); 146 } 147 if (val.scale() > 0) { 148 throw new ObjectIdException("Object IDs cannot have a scale " 149 + "greater than zero."); 150 } 151 if (val.compareTo(MAX) > 0) { 152 throw new ObjectIdException("Object IDs cannot exceed " 153 + MAX.toString()); 154 } 155 value = val; 156 } 157 158 169 public ObjectId add(ObjectId val) 170 throws ObjectIdException { 171 return new ObjectId(value.add(val.toBigDecimal())); 172 } 173 174 185 public ObjectId add(long val) 186 throws ObjectIdException { 187 return new ObjectId(value.add(BigDecimal.valueOf(val))); 188 } 189 190 199 public ObjectId increment() 200 throws ObjectIdException { 201 return new ObjectId(value.add(ONE)); 202 } 203 204 207 public BigDecimal toBigDecimal() { 208 return value; 209 } 210 211 214 public boolean equals(ObjectId oid) { 215 return (oid.toBigDecimal().compareTo(value) == 0); 216 } 217 218 223 public int hashCode() { 224 return toString().hashCode(); 225 } 226 227 230 public String toString() { 231 return value.toString(); 232 } 233 } 234 | Popular Tags |