NamedUnit.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2000 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.math;
  4. import java.io.*;
  5. /** A Unit that has a name. */
  6. public class NamedUnit extends Unit implements Externalizable
  7. {
  8. /** The interned name of this Unit, for example "cm". */
  9. String name;
  10. /** The value of this Unit is scale*base.
  11. * The value of this.factor is scale*base.factor, so scale is
  12. * redundant - were it not for rounding error concerns. */
  13. double scale;
  14. /** The value this was initialized from. */
  15. Unit base;
  16. /** Next NamedUnit in table bucket. */
  17. NamedUnit chain;
  18. public NamedUnit ()
  19. {
  20. }
  21. public NamedUnit (String name, DQuantity value)
  22. {
  23. this.name = name.intern();
  24. scale = value.factor;
  25. base = value.unt;
  26. init();
  27. }
  28. public NamedUnit (String name, double factor, Unit base)
  29. {
  30. this.name = name;
  31. this.base = base;
  32. scale = factor;
  33. init();
  34. }
  35. protected void init ()
  36. {
  37. factor = scale * base.factor;
  38. dims = base.dims;
  39. name = name.intern();
  40. int hash = name.hashCode();
  41. int index = (hash & 0x7FFFFFFF) % table.length;
  42. chain = table[index];
  43. table[index] = this;
  44. }
  45. public String getName() { return name; }
  46. public static NamedUnit lookup (String name)
  47. {
  48. name = name.intern();
  49. int hash = name.hashCode();
  50. int index = (hash & 0x7FFFFFFF) % table.length;
  51. for (NamedUnit unit = table[index]; unit != null; unit = unit.chain)
  52. {
  53. if (unit.name == name)
  54. return unit;
  55. }
  56. return null;
  57. }
  58. public static NamedUnit lookup (String name, double scale, Unit base)
  59. {
  60. name = name.intern();
  61. int hash = name.hashCode();
  62. int index = (hash & 0x7FFFFFFF) % table.length;
  63. for (NamedUnit unit = table[index]; unit != null; unit = unit.chain)
  64. {
  65. if (unit.name == name && unit.scale == scale && unit.base == base)
  66. return unit;
  67. }
  68. return null;
  69. }
  70. public static NamedUnit make (String name, double scale, Unit base)
  71. {
  72. NamedUnit old = lookup(name, scale, base);
  73. return old == null ? new NamedUnit(name, scale, base) : old;
  74. }
  75. public static NamedUnit make (String name, Quantity value)
  76. {
  77. double scale;
  78. if (value instanceof DQuantity)
  79. scale = ((DQuantity) value).factor;
  80. else if (value.imValue() != 0.0)
  81. throw new ArithmeticException("defining " + name
  82. + " using complex value");
  83. else
  84. scale = value.re().doubleValue();
  85. Unit base = value.unit();
  86. NamedUnit old = lookup(name, scale, base);
  87. return old == null ? new NamedUnit(name, scale, base) : old;
  88. }
  89. /**
  90. * @serialData Write the unit name (using writeUTF), followed by
  91. * the definition (value) of this unit as a scale followed by a base.
  92. */
  93. public void writeExternal(ObjectOutput out) throws IOException
  94. {
  95. out.writeUTF(name);
  96. out.writeDouble(scale);
  97. out.writeObject(base);
  98. }
  99. public void readExternal(ObjectInput in)
  100. throws IOException, ClassNotFoundException
  101. {
  102. name = in.readUTF();
  103. scale = in.readDouble();
  104. base = (Unit) in.readObject();
  105. }
  106. public Object readResolve() throws ObjectStreamException
  107. {
  108. NamedUnit unit = lookup(name, scale, base);
  109. if (unit != null)
  110. return unit;
  111. init();
  112. return this;
  113. }
  114. }