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 Unit which is the product or ratio of two other Units.
8  * @author	Per Bothner
9  */
10 
11 class MulUnit extends Unit implements Externalizable
12 {
13   Unit unit1;
14   Unit unit2;
15   int power1;
16   int power2;
17   MulUnit next;
18 
MulUnit(Unit unit1, int power1, Unit unit2, int power2)19   MulUnit (Unit unit1, int power1, Unit unit2, int power2)
20   {
21     this.unit1 = unit1;
22     this.unit2 = unit2;
23     this.power1 = power1;
24     this.power2 = power2;
25     this.dims = Dimensions.product (unit1.dims, power1, unit2.dims, power2);
26 
27     if (power1 == 1)
28       factor = unit1.factor;
29     else
30       factor = Math.pow (unit1.factor, (double) power1);
31     if (power2 < 0)
32       {
33 	for (int i = -power2;  --i >= 0; )
34 	  factor /= unit2.factor;
35       }
36     else
37       {
38 	for (int i = power2;  --i >= 0; )
39 	  factor *= unit2.factor;
40       }
41 
42     next = unit1.products;
43     unit1.products = this;
44   }
45 
MulUnit(Unit unit1, Unit unit2, int power2)46   MulUnit (Unit unit1, Unit unit2, int power2)
47   {
48     this (unit1, 1, unit2, power2);
49   }
50 
toString()51   public String toString ()
52   {
53     StringBuffer str = new StringBuffer(60);
54     str.append(unit1);
55     if (power1 != 1)
56       {
57 	str.append('^');
58 	str.append(power1);
59       }
60     if (power2 != 0)
61       {
62 	str.append('*');
63 	str.append(unit2);
64 	if (power2 != 1)
65 	  {
66 	    str.append('^');
67 	    str.append(power2);
68 	  }
69       }
70     return str.toString();
71   }
72 
sqrt()73   public Unit sqrt ()
74   {
75     if ((power1 & 1) == 0 && (power2 & 1) == 0)
76       return times(unit1, power1 >> 1, unit2, power2 >> 1);
77     return super.sqrt();
78   }
79 
lookup(Unit unit1, int power1, Unit unit2, int power2)80   static MulUnit lookup (Unit unit1, int power1, Unit unit2, int power2)
81   {
82     // Search for an existing matching MulUnit.
83     for (MulUnit u = unit1.products;  u != null;  u = u.next)
84       {
85 	if (u.unit1 == unit1 && u.unit2 == unit2
86 	    && u.power1 == power1 && u.power2 == power2)
87 	  return u;
88       }
89     return null;
90   }
91 
make(Unit unit1, int power1, Unit unit2, int power2)92   public static MulUnit make (Unit unit1, int power1, Unit unit2, int power2)
93   {
94     MulUnit u = lookup(unit1, power1, unit2, power2);
95     if (u != null)
96       return u;
97     return new MulUnit (unit1, power1, unit2, power2);
98   }
99 
100   /**
101    * @serialData
102    */
103 
writeExternal(ObjectOutput out)104   public void writeExternal(ObjectOutput out) throws IOException
105   {
106     out.writeObject(unit1);
107     out.writeInt(power1);
108     out.writeObject(unit2);
109     out.writeInt(power2);
110   }
111 
readExternal(ObjectInput in)112   public void readExternal(ObjectInput in)
113     throws IOException, ClassNotFoundException
114   {
115     unit1 = (Unit) in.readObject();
116     power1 = in.readInt();
117     unit2 = (Unit) in.readObject();
118     power2 = in.readInt();
119   }
120 
readResolve()121   public Object readResolve() throws ObjectStreamException
122   {
123     MulUnit u = lookup(unit1, power1, unit2, power2);
124     if (u != null)
125       return u;
126     return this;
127   }
128 }
129