1 9 10 package org.jboss.util; 11 12 18 public class MuFloat 19 extends MuNumber 20 { 21 22 private float value; 24 27 public MuFloat() {} 28 29 34 public MuFloat(float f) { 35 value = f; 36 } 37 38 43 public MuFloat(Object obj) { 44 setValue(obj); 45 } 46 47 53 public float set(float f) { 54 float old = value; 55 value = f; 56 return old; 57 } 58 59 64 public float get() { 65 return value; 66 } 67 68 76 public boolean commit(float assumed, float b) { 77 boolean success = Primitives.equals(assumed, value); 78 if (success) value = b; 79 return success; 80 } 81 82 88 public float swap(MuFloat b) { 89 if (b == this) return value; 90 91 float temp = value; 92 value = b.value; 93 b.value = temp; 94 95 return value; 96 } 97 98 104 public float add(float amount) { 105 return value += amount; 106 } 107 108 114 public float subtract(float amount) { 115 return value -= amount; 116 } 117 118 124 public float multiply(float factor) { 125 return value *= factor; 126 } 127 128 134 public float divide(float factor) { 135 return value /= factor; 136 } 137 138 143 public float negate() { 144 value = ((float)-value); 145 return value; 146 } 147 148 156 public int compareTo(float other) { 157 return (value < other) ? -1 : Primitives.equals(value, other) ? 0 : 1; 158 } 159 160 170 public int compareTo(Object obj) { 171 return compareTo((MuFloat)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, ((MuFloat)obj).floatValue()); 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 223 227 232 public byte byteValue() { 233 return (byte)value; 234 } 235 236 241 public short shortValue() { 242 return (short)value; 243 } 244 245 250 public int intValue() { 251 return (int)value; 252 } 253 254 259 public long longValue() { 260 return (long)value; 261 } 262 263 268 public float floatValue() { 269 return (float)value; 270 } 271 272 277 public double doubleValue() { 278 return (double)value; 279 } 280 281 282 286 293 public void setValue(Object obj) { 294 if (obj instanceof Number ) { 295 value = ((Number )obj).floatValue(); 296 } 297 else if (obj instanceof String ) { 298 try { 299 value = Float.parseFloat(String.valueOf(obj)); 300 } 301 catch (Exception e) { 302 throw new NotCoercibleException("can not convert to 'float': " + obj); 303 } 304 } 305 else { 306 throw new NotCoercibleException("can not convert to 'float': " + obj); 307 } 308 } 309 310 315 public Object getValue() { 316 return new Float (value); 317 } 318 } 319 | Popular Tags |