1 18 package org.osgi.util.measurement; 19 20 21 51 public class Measurement implements Comparable { 52 53 final double value; 54 final double error; 55 final long time; 56 final Unit unit; 57 private transient String name; 58 59 70 public Measurement(double value, double error, Unit unit, long time) { 71 this.value = value; 72 this.error = Math.abs(error); 73 this.unit = (unit != null) ? unit : Unit.unity; 74 this.time = time; 75 } 76 77 86 public Measurement(double value, double error, Unit unit) { 87 this(value, error, unit, 0l); 88 } 89 90 99 public Measurement(double value, Unit unit) { 100 this(value, 0.0d, unit, 0l); 101 } 102 103 109 public Measurement(double value) { 110 this(value, 0.0d, null, 0l); 111 } 112 113 118 public final double getValue() { 119 return value; 120 } 121 122 128 public final double getError() { 129 return error; 130 } 131 132 139 public final Unit getUnit() { 140 return unit; 141 } 142 143 151 public final long getTime() { 152 return time; 153 } 154 155 169 public Measurement mul(Measurement m) { 170 double mvalue = m.value; 171 return new Measurement(value * mvalue, Math.abs(value) * m.error 172 + error * Math.abs(mvalue), unit.mul(m.unit), time); 173 } 174 175 189 public Measurement mul(double d, Unit u) { 190 return new Measurement(value * d, error * Math.abs(d), unit.mul(u), 191 time); 192 } 193 194 204 public Measurement mul(double d) { 205 return new Measurement(value * d, error * Math.abs(d), unit, time); 206 } 207 208 222 public Measurement div(Measurement m) { 223 double mvalue = m.value; 224 return new Measurement(value / mvalue, 225 (Math.abs(value) * m.error + error * Math.abs(mvalue)) 226 / (mvalue * mvalue), unit.div(m.unit), time); 227 } 228 229 243 public Measurement div(double d, Unit u) { 244 return new Measurement(value / d, error / Math.abs(d), unit.div(u), 245 time); 246 } 247 248 258 public Measurement div(double d) { 259 return new Measurement(value / d, error / Math.abs(d), unit, time); 260 } 261 262 277 public Measurement add(Measurement m) { 278 return new Measurement(value + m.value, error + m.error, unit 279 .add(m.unit), time); 280 } 281 282 296 public Measurement add(double d, Unit u) { 297 return new Measurement(value + d, error, unit.add(u), time); 298 } 299 300 310 public Measurement add(double d) { 311 return new Measurement(value + d, error, unit, time); 312 } 313 314 328 public Measurement sub(Measurement m) { 329 return new Measurement(value - m.value, error + m.error, unit 330 .sub(m.unit), time); 331 } 332 333 347 public Measurement sub(double d, Unit u) { 348 return new Measurement(value - d, error, unit.sub(u), time); 349 } 350 351 361 public Measurement sub(double d) { 362 return new Measurement(value - d, error, unit, time); 363 } 364 365 372 public String toString() { 373 if (name == null) { 374 StringBuffer sb = new StringBuffer (); 375 sb.append(value); 376 if (error != 0.0d) { 377 sb.append(" +/- "); 378 sb.append(error); 379 } 380 String u = unit.toString(); 381 if (u.length() > 0) { 382 sb.append(" "); 383 sb.append(u); 384 } 385 name = sb.toString(); 386 } 387 return name; 388 } 389 390 416 public int compareTo(Object obj) { 417 if (this == obj) { 418 return 0; 419 } 420 Measurement that = (Measurement) obj; 421 if (!unit.equals(that.unit)) { 422 throw new ArithmeticException ("Cannot compare " + this + " and " 423 + that); 424 } 425 if (value == that.value) { 426 return 0; 427 } 428 if (value < that.value) { 429 if ((value + error) >= (that.value - that.error)) { 430 return 0; 431 } 432 else { 433 return -1; 434 } 435 } 436 else { 437 if ((value - error) <= (that.value + that.error)) { 438 return 0; 439 } 440 else { 441 return 1; 442 } 443 } 444 } 445 446 451 public int hashCode() { 452 long bits = Double.doubleToLongBits(value + error); 453 return ((int) (bits ^ (bits >>> 32))) ^ unit.hashCode(); 454 } 455 456 469 public boolean equals(Object obj) { 470 if (this == obj) { 471 return true; 472 } 473 if (!(obj instanceof Measurement)) { 474 return false; 475 } 476 Measurement that = (Measurement) obj; 477 return (value == that.value) && (error == that.error) 478 && unit.equals(that.unit); 479 } 480 } 481 | Popular Tags |