KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > Unit


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.math;
5
6 /* A unit of measurement, either primitive (meter) or derived (kilogram).
7  * @author Per Bothner
8  */

9
10 public abstract class Unit extends Quantity
11 {
12   Dimensions dims;
13   /** The value of this Unit is factor*dims. */
14   double factor;
15   MulUnit products;
16
17   /** A Unit equivalent to this unit, divided by factor.
18       Same as the value of the dimensions() only. */

19   Unit base;
20
21   /** A hash table of named Units. */
22   static NamedUnit[] table = new NamedUnit[100];
23
24   public final Dimensions dimensions() { return dims; }
25
26   public final double doubleValue() { return factor; }
27
28   public int hashCode () { return dims.hashCode (); }
29
30   public String JavaDoc getName() { return null; }
31
32   static Unit times(Unit unit1, int power1, Unit unit2, int power2)
33   {
34     // First try various simplifications.
35
if (unit1 == unit2)
36       {
37     power1 += power2;
38     unit2 = Unit.Empty;
39     power2 = 0;
40       }
41     if (power1 == 0 || unit1 == Unit.Empty)
42       {
43     unit1 = unit2;
44     power1 = power2;
45     unit2 = Unit.Empty;
46     power2 = 0;
47       }
48     if (power2 == 0 || unit2 == Unit.Empty)
49       {
50     if (power1 == 1)
51       return unit1;
52     if (power1 == 0)
53       return Unit.Empty;
54       }
55     if (unit1 instanceof MulUnit)
56       {
57     MulUnit munit1 = (MulUnit) unit1;
58     if (munit1.unit1 == unit2)
59       return times(unit2, munit1.power1 * power1 + power2,
60                munit1.unit2, munit1.power2 * power1);
61     if (munit1.unit2 == unit2)
62       return times(munit1.unit1, munit1.power1 * power1,
63                unit2, munit1.power2 * power1 + power2);
64     if (unit2 instanceof MulUnit)
65       {
66         MulUnit munit2 = (MulUnit) unit2;
67         if (munit1.unit1 == munit2.unit1 && munit1.unit2 == munit2.unit2)
68           return times(munit1.unit1,
69                munit1.power1 * power1 + munit2.power1 * power2,
70                munit1.unit2,
71                munit1.power2 * power1 + munit2.power2 * power2);
72         if (munit1.unit1 == munit2.unit2 && munit1.unit2 == munit2.unit1)
73           return times(munit1.unit1,
74                munit1.power1 * power1 + munit2.power2 * power2,
75                munit1.unit2,
76                munit1.power2 * power1 + munit2.power1 * power2);
77       }
78       }
79     if (unit2 instanceof MulUnit)
80       {
81     MulUnit munit2 = (MulUnit) unit2;
82     if (munit2.unit1 == unit1)
83       return times(unit1, power1 + munit2.power1 * power2,
84                munit2.unit2, munit2.power2 * power2);
85     if (munit2.unit2 == unit1)
86       return times(munit2.unit1, munit2.power1 * power2,
87                unit1, power1 + munit2.power2 * power2);
88       }
89
90     return MulUnit.make(unit1, power1, unit2, power2);
91   }
92
93   public static Unit times(Unit unit1, Unit unit2)
94   {
95     return times(unit1, 1, unit2, 1);
96   }
97
98   public static Unit divide (Unit unit1, Unit unit2)
99   {
100     return times(unit1, 1, unit2, -1);
101   }
102
103   public static Unit pow (Unit unit, int power)
104   {
105     return times(unit, power, Unit.Empty, 0);
106   }
107
108   Unit ()
109   {
110     factor = 1.0;
111   }
112
113   public static NamedUnit make (String JavaDoc name, Quantity value)
114   {
115     return NamedUnit.make(name, value);
116   }
117
118   public static Unit define (String JavaDoc name, DQuantity value)
119   {
120     return new NamedUnit (name, value);
121   }
122
123   public static Unit define (String JavaDoc name, double factor, Unit base)
124   {
125     return new NamedUnit (name, factor, base);
126   }
127
128   public Complex number() { return DFloNum.one(); }
129   public boolean isExact () { return false; }
130   public final boolean isZero () { return false; }
131
132   public Numeric power (IntNum y)
133   {
134     if (y.words != null)
135       throw new ArithmeticException JavaDoc("Unit raised to bignum power");
136     return pow (this, y.ival);
137   }
138
139   public Unit sqrt ()
140   {
141     if (this == Unit.Empty)
142       return this;
143     throw new RuntimeException JavaDoc ("unimplemented Unit.sqrt");
144   }
145
146   public static BaseUnit Empty = new BaseUnit();
147   static { Dimensions.Empty.bases[0] = Empty; }
148
149   public static NamedUnit lookup (String JavaDoc name)
150   {
151     return NamedUnit.lookup(name);
152   }
153
154   public String JavaDoc toString (double val)
155   {
156     String JavaDoc str = Double.toString(val);
157     if (this == Unit.Empty)
158       return str;
159     else
160       return str + this.toString();
161   }
162
163   public String JavaDoc toString (RealNum val)
164   {
165     return toString (val.doubleValue());
166   }
167
168   /*
169   public String toString (Complex val)
170   {
171     String str = toString(val.re());
172     RealNum im = val.im();
173     if (im.isZero())
174       return str;
175     // This conflicts with using '@' for polar notation.
176     return str + "@" + toString(im);
177   }
178   */

179
180   public String JavaDoc toString ()
181   {
182     String JavaDoc name = getName();
183     if (name != null)
184       return name;
185     else if (this == Unit.Empty)
186       return "unit";
187     else
188       return Double.toString(factor) + "<unnamed unit>";
189   }
190
191   public Unit unit ()
192   {
193     return this;
194   }
195
196   /** A magic factor to indicate units that have the same "dimension"
197    * but not a fixed multiple.
198    * E.g. "month" and "day", or money of different currencies.
199    * Since they have the same dimension, they can be added to get
200    * an (unimplemented) combined quantity, but they cannot be compared.
201    * No general support yet, but used for time Duration.
202    */

203   public static double NON_COMBINABLE = 0.0;
204
205   public static final BaseUnit meter = new BaseUnit ("m", "Length");
206   public static final BaseUnit duration = new BaseUnit ("duration", "Time");
207   public static final BaseUnit gram = new BaseUnit ("g", "Mass");
208   public static final Unit cm = define("cm", 0.01, meter);
209   public static final Unit mm = define("mm", 0.1, cm);
210   public static final Unit in = define("in", 0.0254, meter);
211   public static final Unit pt = define("pt", 0.0003527778, meter);
212   public static final Unit pica = define("pica", 0.004233333, meter);
213   public static final Unit radian = define("rad", 1.0, Unit.Empty);
214
215   public static final NamedUnit date =
216     new NamedUnit("date", NON_COMBINABLE, duration);
217   public static final NamedUnit second =
218     new NamedUnit("s", NON_COMBINABLE, duration);
219   public static final NamedUnit month =
220     new NamedUnit("month", NON_COMBINABLE, duration);
221   public static final Unit minute = define("min", 60.0, second);
222   public static final Unit hour = define("hour", 60.0, minute);
223 }
224
Popular Tags