1 9 package javax.measure.units; 10 11 import javax.measure.converters.UnitConverter; 12 import javax.measure.quantities.Quantity; 13 14 27 public final class AlternateUnit<Q extends Quantity> extends DerivedUnit<Q> { 28 29 32 private final String _symbol; 33 34 37 private final Unit _parent; 38 39 52 public AlternateUnit(String symbol, Unit parent) { 53 if (!parent.getSystemUnit().equals(parent)) 54 throw new IllegalArgumentException (parent + " is not a system unit"); 55 _symbol = symbol; 56 _parent = parent; 57 synchronized (Unit.SYMBOL_TO_UNIT) { 59 Unit unit = Unit.SYMBOL_TO_UNIT.get(symbol); 60 if (unit == null) { 61 Unit.SYMBOL_TO_UNIT.put(symbol, this); 62 return; 63 } 64 if (unit instanceof AlternateUnit) { 65 AlternateUnit existingUnit = (AlternateUnit) unit; 66 if (symbol.equals(existingUnit._symbol) 67 && _parent.equals(existingUnit._parent)) 68 return; } 70 throw new IllegalArgumentException ("Symbol " + symbol 71 + " is associated to a different unit"); 72 } 73 } 74 75 80 public final String getSymbol() { 81 return _symbol; 82 } 83 84 90 @SuppressWarnings ("unchecked") 91 public final Unit<? super Q> getParent() { 92 return _parent; 93 } 94 95 @Override 96 public final Unit<? super Q> getSystemUnit() { 97 return this; 98 } 99 100 @Override 101 public final UnitConverter toSystemUnit() { 102 return UnitConverter.IDENTITY; 103 } 104 105 114 public boolean equals(Object that) { 115 if (this == that) 116 return true; 117 if (!(that instanceof AlternateUnit)) 118 return false; 119 AlternateUnit thatUnit = (AlternateUnit) that; 120 return this._symbol.equals(thatUnit._symbol); } 122 123 public int hashCode() { 125 return _symbol.hashCode(); 126 } 127 128 private static final long serialVersionUID = 1L; 129 } | Popular Tags |