KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > BaseUnit


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 primitive Unit of measurement (such as a meter).
8  * @author Per Bothner
9  */

10
11 public class BaseUnit extends NamedUnit implements Externalizable
12 {
13   /** The name of the "dimension" this is a base unit for. */
14   String JavaDoc dimension;
15
16   /** BaseUnits are numberd with globally unique indexes. */
17   static int base_count = 0;
18
19   /** This is an index in the bases array.
20    * The index may change if there are insertions in bases. */

21   int index;
22
23   /* Array of all existing BaseUnits.
24    * This array is kept sorted (according to the compareTo method).
25    * The reason is to make it easy to keep the BaseUnits in a Dimensions
26    * array to be sorted "lexicographically". One reason we want to
27    * do that is to have a stable serialization representtion. *
28   // static BaseUnit[] bases = null;
29
30   /** A name for the dimension bing measured.
31    * A meter has the dimension "Length".
32    * BaseUnits are considered equal if their name <em>and</em>
33    * their dimension are equal. (In that case they are also identical.)
34    * We use dimension as a partial guard against accidental name clashes.
35    */

36   public String JavaDoc getDimension()
37   {
38     return dimension;
39   }
40
41   /** Name for Unit.Empty. */
42   private static final String JavaDoc unitName = "(name)";
43
44   /** Should only be used for serialization, and Unit.Empty. */
45   public BaseUnit()
46   {
47     name = unitName;
48     index = 0x7fffffff;
49     dims = Dimensions.Empty;
50   }
51
52   protected void init()
53   {
54     this.base = this;
55     this.scale = 1.0;
56     this.dims = new Dimensions (this);
57     super.init();
58
59     this.index = BaseUnit.base_count++;
60     /*
61     if (bases == null)
62       bases = new BaseUnit[10];
63     else if (index >= bases.length)
64       {
65     BaseUnit[] b = new BaseUnit[2 * index];
66     System.arraycopy(bases, 0, b, 0, bases.length);
67     bases = b;
68       }
69     bases[index] = this;
70     // Make sure bases array is sorted.
71     for (int i = index; --i >= 0; )
72       {
73     BaseUnit old = bases[i];
74     int code = compare(old, this);
75     if (code == 0)
76       throw new Error("internal invariant failure");
77     if (code > 0)
78       break;
79     // Swap old and this, and their index fields.
80     bases[i] = this;
81     bases[index] = old;
82     old.index = index;
83     index = i;
84       }
85     */

86   }
87
88   public BaseUnit (String JavaDoc name)
89   {
90     this.name = name;
91     init();
92   }
93
94   public BaseUnit (String JavaDoc name, String JavaDoc dimension)
95   {
96     this.name = name;
97     this.dimension = dimension;
98     init();
99   }
100
101   public int hashCode () { return name.hashCode(); }
102
103   public Unit unit() { return this; }
104
105   /** Look for an existing matching BaseUnit.
106    * @param name name of desired BaseUnit, such as "m"
107    * @param dimension a name for what the unit measures, such as "Length".
108    */

109   public static BaseUnit lookup(String JavaDoc name, String JavaDoc dimension)
110   {
111     name = name.intern();
112     if (name == unitName && dimension == null)
113       return Unit.Empty;
114     int hash = name.hashCode();
115     int index = (hash & 0x7FFFFFFF) % table.length;
116     for (NamedUnit unit = table[index]; unit != null; unit = unit.chain)
117       {
118     if (unit.name == name && unit instanceof BaseUnit)
119       {
120         BaseUnit bunit = (BaseUnit) unit;
121         if (bunit.dimension == dimension)
122           return bunit;
123       }
124       }
125     return null;
126   }
127
128   public static BaseUnit make(String JavaDoc name, String JavaDoc dimension)
129   {
130     BaseUnit old = lookup(name, dimension);
131     return old == null ? new BaseUnit(name, dimension) : old;
132   }
133
134   public static int compare (BaseUnit unit1, BaseUnit unit2)
135   {
136     int code = unit1.name.compareTo(unit2.name);
137     if (code != 0)
138       return code;
139     String JavaDoc dim1 = unit1.dimension;
140     String JavaDoc dim2 = unit2.dimension;
141     if (dim1 == dim2)
142       return 0;
143     if (dim1 == null)
144       return -1;
145     if (dim2 == null)
146       return 1;
147     return dim1.compareTo(dim2);
148   }
149
150   /**
151    * @serialData Write the unit name (using writeUTF), followed.
152    * followed by the name of the dimension it is a unit for.
153    * The latter is either null or a String and is written with writeObject.
154    */

155
156   public void writeExternal(ObjectOutput out) throws IOException
157   {
158     out.writeUTF(name);
159     out.writeObject(dimension);
160   }
161
162   public void readExternal(ObjectInput in)
163     throws IOException, ClassNotFoundException JavaDoc
164   {
165     name = in.readUTF();
166     dimension = (String JavaDoc) in.readObject();
167   }
168
169   public Object JavaDoc readResolve() throws ObjectStreamException
170   {
171     BaseUnit unit = lookup(name, dimension);
172     if (unit != null)
173       return unit;
174     init();
175     return this;
176   }
177 }
178
Popular Tags