1 4 package gnu.math; 5 import java.io.*; 6 7 10 11 public class BaseUnit extends NamedUnit implements Externalizable 12 { 13 14 String dimension; 15 16 17 static int base_count = 0; 18 19 21 int index; 22 23 36 public String getDimension() 37 { 38 return dimension; 39 } 40 41 42 private static final String unitName = "(name)"; 43 44 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 86 } 87 88 public BaseUnit (String name) 89 { 90 this.name = name; 91 init(); 92 } 93 94 public BaseUnit (String name, String 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 109 public static BaseUnit lookup(String name, String 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 name, String 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 dim1 = unit1.dimension; 140 String 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 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 164 { 165 name = in.readUTF(); 166 dimension = (String ) in.readObject(); 167 } 168 169 public Object 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 |