1 9 10 package org.jboss.util; 11 12 18 public class MuDouble 19 extends MuNumber 20 { 21 22 private double value = 0; 23 24 27 public MuDouble() {} 28 29 34 public MuDouble(double d) { 35 value = d; 36 } 37 38 43 public MuDouble(Object obj) { 44 setValue(obj); 45 } 46 47 53 public double set(double f) { 54 double old = value; 55 value = f; 56 return old; 57 } 58 59 64 public double get() { 65 return value; 66 } 67 68 76 public boolean commit(double assumed, double b) { 77 boolean success = Primitives.equals(assumed, value); 78 if (success) value = b; 79 return success; 80 } 81 82 88 public double swap(MuDouble b) { 89 if (b == this) return value; 90 91 double temp = value; 92 value = b.value; 93 b.value = temp; 94 95 return value; 96 } 97 98 104 public double add(double amount) { 105 return value += amount; 106 } 107 108 114 public double subtract(double amount) { 115 return value -= amount; 116 } 117 118 124 public double multiply(double factor) { 125 return value *= factor; 126 } 127 128 134 public double divide(double factor) { 135 return value /= factor; 136 } 137 138 143 public double negate() { 144 value = ((double)-value); 145 return value; 146 } 147 148 156 public int compareTo(double other) { 157 return (value < other) ? -1 : Primitives.equals(value, other) ? 0 : 1; 158 } 159 160 170 public int compareTo(Object obj) { 171 return compareTo((MuDouble)obj); 172 } 173 174 179 public String toString() { 180 return String.valueOf(value); 181 } 182 183 188 public int hashCode() { 189 return HashCode.generate(value); 190 } 191 192 198 public boolean equals(Object obj) { 199 if (obj == this) return true; 200 201 if (obj != null && obj.getClass() == getClass()) { 202 return Primitives.equals(value, ((MuDouble)obj).doubleValue()); 203 } 204 205 return false; 206 } 207 208 213 public Object clone() { 214 try { 215 return super.clone(); 216 } 217 catch (CloneNotSupportedException e) { 218 throw new InternalError (); 219 } 220 } 221 222 226 231 public byte byteValue() { 232 return (byte)value; 233 } 234 235 240 public short shortValue() { 241 return (short)value; 242 } 243 244 249 public int intValue() { 250 return (int)value; 251 } 252 253 258 public long longValue() { 259 return (long)value; 260 } 261 262 267 public float floatValue() { 268 return (float)value; 269 } 270 271 276 public double doubleValue() { 277 return (double)value; 278 } 279 280 281 285 292 public void setValue(Object obj) { 293 if (obj instanceof Number ) { 294 value = ((Number )obj).doubleValue(); 295 } 296 else { 297 throw new NotCoercibleException("can not convert to 'double': " + obj); 298 } 299 } 300 301 306 public Object getValue() { 307 return new Double (value); 308 } 309 } 310 | Popular Tags |