KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > DQuantity


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 import java.io.*;
6
7 /** A Quantity represented as the product of a plain double and a Unit.
8  * @author Per Bothner
9  */

10
11 public class DQuantity extends Quantity implements Externalizable
12 {
13   double factor;
14   Unit unt;
15   public final Unit unit() { return unt; }
16   public final Complex number() { return new DFloNum(factor); }
17
18   public final RealNum re() { return new DFloNum (factor); }
19   public final double doubleValue() { return factor * unt.factor; }
20
21   public DQuantity (double factor, Unit unit)
22   {
23     this.factor = factor;
24     this.unt = unit;
25   }
26
27   public boolean isExact () { return false; }
28
29   public boolean isZero () { return factor == 0.0; }
30
31   public static DQuantity add (DQuantity x, DQuantity y, double k)
32   {
33     if (x.dimensions() != y.dimensions())
34       throw new ArithmeticException JavaDoc ("units mis-match");
35     double unit_ratio = y.unit().factor / x.unit().factor;
36     return new DQuantity (x.factor + k * unit_ratio * y.factor, x.unit());
37   }
38
39   public static DQuantity times (DQuantity x, DQuantity y)
40   {
41     double factor = x.factor * y.factor;
42     Unit unit = Unit.times(x.unit(), y.unit());
43     return new DQuantity (factor, unit);
44   }
45
46   public static DQuantity divide (DQuantity x, DQuantity y)
47   {
48     double factor = x.factor / y.factor;
49     Unit unit = Unit.divide(x.unit(), y.unit());
50     return new DQuantity (factor, unit);
51   }
52
53   public Numeric add (Object JavaDoc y, int k)
54   {
55     if (y instanceof DQuantity)
56       return add (this, (DQuantity) y, (double) k);
57     if (dimensions() == Dimensions.Empty && y instanceof RealNum)
58       return new DQuantity (factor + k * ((RealNum)y).doubleValue (), unit());
59     if (!(y instanceof Numeric))
60       throw new IllegalArgumentException JavaDoc ();
61     return ((Numeric)y).addReversed(this, k);
62   }
63
64   public Numeric addReversed (Numeric x, int k)
65   {
66     if (dimensions() == Dimensions.Empty && x instanceof RealNum)
67       return new DFloNum (((RealNum)x).doubleValue () + k * factor);
68     throw new IllegalArgumentException JavaDoc ();
69   }
70
71   public Numeric mul (Object JavaDoc y)
72   {
73     if (y instanceof DQuantity)
74       return times(this, (DQuantity) y);
75     if (y instanceof RealNum)
76       return new DQuantity (factor * ((RealNum)y).doubleValue (), unit());
77     if (!(y instanceof Numeric))
78       throw new IllegalArgumentException JavaDoc ();
79     return ((Numeric)y).mulReversed(this);
80   }
81
82   public Numeric mulReversed (Numeric x)
83   {
84     if (x instanceof RealNum)
85       return new DQuantity (((RealNum)x).doubleValue () * factor, unit());
86     throw new IllegalArgumentException JavaDoc ();
87   }
88
89   public Numeric div (Object JavaDoc y)
90   {
91     if (y instanceof DQuantity)
92       {
93     DQuantity qy = (DQuantity) y;
94     if (dimensions() == qy.dimensions())
95       return new DFloNum ((factor * unit().doubleValue())
96                   / (qy.factor * qy.unit().factor));
97     return divide(this, qy);
98       }
99     if (y instanceof RealNum)
100       return new DQuantity (factor / ((RealNum)y).doubleValue (), unit());
101     if (!(y instanceof Numeric))
102       throw new IllegalArgumentException JavaDoc ();
103     return ((Numeric)y).divReversed(this);
104   }
105
106   public Numeric divReversed (Numeric x)
107   {
108     if (x instanceof RealNum)
109       return new DQuantity (((RealNum)x).doubleValue () / factor,
110                Unit.divide(Unit.Empty, unit()));
111     throw new IllegalArgumentException JavaDoc ();
112   }
113
114   /**
115    * @serialData Write the value (using writeDouble) followed
116    * by the Unit (using writeObject).
117    */

118
119   public void writeExternal(ObjectOutput out) throws IOException
120   {
121     out.writeDouble(factor);
122     out.writeObject(unt);
123   }
124
125   public void readExternal(ObjectInput in)
126     throws IOException, ClassNotFoundException JavaDoc
127   {
128     factor = in.readDouble();
129     unt = (Unit) in.readObject();
130   }
131 }
132
Popular Tags