1 18 package org.osgi.util.measurement; 19 20 import java.util.Hashtable ; 21 22 35 51 public class Unit { 52 private final static long UNITY = createType(0, 0, 0, 0, 0, 0, 0, 53 0, 0); 54 private final static long ZERO = 0x40L; 55 private final static long MASK = 0x7fL; 56 private final static int m_SHIFT = 0; 57 private final static int s_SHIFT = 7; 58 private final static int kg_SHIFT = 14; 59 private final static int K_SHIFT = 21; 60 private final static int A_SHIFT = 28; 61 private final static int mol_SHIFT = 35; 62 private final static int cd_SHIFT = 42; 63 private final static int rad_SHIFT = 49; 64 private final static int x_SHIFT = 56; 65 private final static long x_MASK = MASK << x_SHIFT; 66 67 public final static Unit unity = new Unit("", UNITY); 69 70 public final static Unit m = new Unit("m", createType(0, 0, 0, 71 0, 0, 0, 0, 0, 1)); 74 public final static Unit s = new Unit("s", createType(0, 0, 0, 75 0, 0, 0, 0, 1, 0)); 79 public final static Unit kg = new Unit("kg", createType(0, 0, 80 0, 0, 0, 0, 1, 0, 0)); 84 public final static Unit K = new Unit("K", createType(0, 0, 0, 85 0, 0, 1, 0, 0, 0)); 89 public final static Unit A = new Unit("A", createType(0, 0, 0, 90 0, 1, 0, 0, 0, 0)); 94 public final static Unit mol = new Unit("mol", createType(0, 0, 95 0, 1, 0, 0, 0, 0, 0)); 99 public final static Unit cd = new Unit("cd", createType(0, 0, 100 1, 0, 0, 0, 0, 0, 0)); 104 105 public final static Unit m_s = new Unit("m/s", createType(0, 0, 106 0, 0, 0, 0, 0, -1, 1)); 109 public final static Unit m_s2 = new Unit("m/s2", createType(0, 0, 110 0, 0, 0, 0, 0, -2, 1)); 113 public final static Unit m2 = new Unit("m2", createType(0, 0, 114 0, 0, 0, 0, 0, 0, 2)); 117 public final static Unit m3 = new Unit("m3", createType(0, 0, 118 0, 0, 0, 0, 0, 0, 3)); 125 public final static Unit Hz = new Unit("Hz", createType(0, 0, 126 0, 0, 0, 0, 0, -1, 0)); 133 public final static Unit N = new Unit("N", createType(0, 0, 0, 134 0, 0, 0, 1, -2, 1)); 143 public final static Unit Pa = new Unit("Pa", createType(0, 0, 144 0, 0, 0, 0, 1, -2, -1)); 153 public final static Unit J = new Unit("J", createType(0, 0, 0, 154 0, 0, 0, 1, -2, 2)); 163 public final static Unit W = new Unit("W", createType(0, 0, 0, 164 0, 0, 0, 1, -3, 2)); 172 public final static Unit C = new Unit("C", createType(0, 0, 0, 173 0, 1, 0, 0, 1, 0)); 182 public final static Unit V = new Unit("V", createType(0, 0, 0, 183 0, -1, 0, 1, -3, 2)); 193 public final static Unit F = new Unit("F", createType(0, 0, 0, 194 0, 2, 0, -1, 4, -2)); 203 public final static Unit Ohm = new Unit("Ohm", createType(0, 0, 204 0, 0, -2, 0, 1, -3, 2)); 213 public final static Unit S = new Unit("S", createType(0, 0, 0, 214 0, 2, 0, -1, 3, -2)); 223 public final static Unit Wb = new Unit("Wb", createType(0, 0, 224 0, 0, -1, 0, 1, -2, 2)); 234 public final static Unit T = new Unit("T", createType(0, 0, 0, 235 0, -1, 0, 1, -2, 0)); 245 public final static Unit lx = new Unit("lx", createType(0, 0, 246 1, 0, 0, 0, 0, 0, -2)); 255 public final static Unit Gy = new Unit("Gy", createType(0, 0, 256 0, 0, 0, 0, 0, -2, 2)); 265 public final static Unit kat = new Unit("kat", createType(0, 0, 266 0, 1, 0, 0, 0, -1, 0)); 271 public final static Unit rad = new Unit("rad", createType(0, 1, 272 0, 0, 0, 0, 0, 0, 0)); 279 private final static Unit[] allUnits = new Unit[] {m, s, kg, K, A, mol, 280 cd, rad, m_s, m_s2, m2, m3, Hz, N, Pa, J, W, C, V, F, Ohm, S, Wb, 281 T, lx, Gy, kat, unity }; 282 private static Hashtable base; 283 private String name; 284 private long type; 285 286 292 private Unit(String name, long type) { 293 this.name = name; 294 this.type = type; 295 } 297 298 302 private static long createType(int x, int rad, int cd, int mol, int A, 303 int K, int kg, int s, int m) { 304 return (((ZERO + m) & MASK) << m_SHIFT) 305 | (((ZERO + s) & MASK) << s_SHIFT) 306 | (((ZERO + kg) & MASK) << kg_SHIFT) 307 | (((ZERO + K) & MASK) << K_SHIFT) 308 | (((ZERO + A) & MASK) << A_SHIFT) 309 | (((ZERO + mol) & MASK) << mol_SHIFT) 310 | (((ZERO + cd) & MASK) << cd_SHIFT) 311 | (((ZERO + rad) & MASK) << rad_SHIFT) 312 | (((long) x) << x_SHIFT); 313 } 314 315 325 public boolean equals(Object obj) { 326 if (this == obj) { 327 return true; 328 } 329 if (!(obj instanceof Unit)) { 330 return false; 331 } 332 return ((Unit) obj).type == type; 333 } 334 335 340 public int hashCode() { 341 return (int) ((type >>> 32) ^ type); 342 } 343 344 358 Unit mul(Unit that) { 359 if (this.isSpecial() && that.isSpecial()) { 360 throw new ArithmeticException ("Cannot multiply " + this + " with " 361 + that); 362 } 363 return find(this.type - UNITY + that.type); 364 } 365 366 379 Unit div(Unit that) { 380 if (this.isSpecial() && that.isSpecial()) { 381 if (this.type == that.type) { 382 return Unit.unity; 383 } 384 throw new ArithmeticException ("Cannot divide " + this + " by " 385 + that); 386 } 387 return find(this.type - that.type + UNITY); 388 } 389 390 402 Unit add(Unit that) { 403 if (!this.equals(that)) { 404 throw new ArithmeticException ("Cannot add " + this + " to " + that); 405 } 406 return this; 407 } 408 409 421 Unit sub(Unit that) { 422 if (!this.equals(that)) { 423 throw new ArithmeticException ("Cannot subtract " + that + " from " 424 + this); 425 } 426 return this; 427 } 428 429 438 static Unit find(long type) { 439 if (base == null) { 440 synchronized (Unit.class) { 441 if (base == null) { 442 int size = allUnits.length; 443 base = new Hashtable (size << 1); 444 for (int i = 0; i < size; i++) { 445 base.put(allUnits[i], allUnits[i]); 446 } 447 } 448 } 449 } 450 Unit unit = new Unit(null, type); 451 Unit out = (Unit) base.get(unit); 452 if (out == null) { 453 base.put(unit, unit); 454 out = unit; 455 } 456 return out; 457 } 458 459 464 public String toString() { 465 if (name == null) { 466 int m = (int) (((type >> m_SHIFT) & MASK) - ZERO); 467 int s = (int) (((type >> s_SHIFT) & MASK) - ZERO); 468 int kg = (int) (((type >> kg_SHIFT) & MASK) - ZERO); 469 int K = (int) (((type >> K_SHIFT) & MASK) - ZERO); 470 int A = (int) (((type >> A_SHIFT) & MASK) - ZERO); 471 int mol = (int) (((type >> mol_SHIFT) & MASK) - ZERO); 472 int cd = (int) (((type >> cd_SHIFT) & MASK) - ZERO); 473 int rad = (int) (((type >> rad_SHIFT) & MASK) - ZERO); 474 StringBuffer numerator = new StringBuffer (); 475 StringBuffer denominator = new StringBuffer (); 476 addSIname(m, "m", numerator, denominator); 477 addSIname(s, "s", numerator, denominator); 478 addSIname(kg, "kg", numerator, denominator); 479 addSIname(K, "K", numerator, denominator); 480 addSIname(A, "A", numerator, denominator); 481 addSIname(mol, "mol", numerator, denominator); 482 addSIname(cd, "cd", numerator, denominator); 483 addSIname(rad, "rad", numerator, denominator); 484 if (denominator.length() > 0) { 485 if (numerator.length() == 0) { 486 numerator.append("1"); 487 } 488 numerator.append("/"); 489 numerator.append((Object ) denominator); 495 } 496 name = numerator.toString(); 497 } 498 return name; 499 } 500 501 private void addSIname(int si, String name, StringBuffer numerator, 502 StringBuffer denominator) { 503 if (si != 0) { 504 StringBuffer sb = (si > 0) ? numerator : denominator; 505 if (sb.length() > 0) { 506 sb.append("*"); 507 } 508 sb.append(name); 509 int power = Math.abs(si); 510 if (power > 1) { 511 sb.append("^"); 512 sb.append(power); 513 } 514 } 515 } 516 517 522 private boolean isSpecial() { 523 return (type & x_MASK) != 0; 524 } 525 } 526 | Popular Tags |