KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > measure > units > AlternateUnit


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javax.measure.units;
10
11 import javax.measure.converters.UnitConverter;
12 import javax.measure.quantities.Quantity;
13
14 /**
15  * <p> This class represents the units used in expressions to distinguish
16  * between quantities of a different nature but of the same dimensions.</p>
17  *
18  * <p> Examples of alternate units:[code]
19  * Unit<Angle> RADIAN = new AlternateUnit<Angle>("rad", ONE);
20  * Unit<Force> NEWTON = new AlternateUnit<Force>("N", METER.multiply(KILOGRAM).divide(SECOND.pow(2)));
21  * Unit<Pressure> PASCAL = new AlternateUnit<Pressure>("Pa", NEWTON.divide(METER.pow(2)));
22  * [/code]</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 3.1, April 22, 2006
26  */

27 public final class AlternateUnit<Q extends Quantity> extends DerivedUnit<Q> {
28
29     /**
30      * Holds the symbol.
31      */

32     private final String JavaDoc _symbol;
33
34     /**
35      * Holds the parent unit (a system unit).
36      */

37     private final Unit _parent;
38
39     /**
40      * Creates an alternate unit for the specified unit identified by the
41      * specified symbol.
42      *
43      * @param symbol the symbol for this alternate unit.
44      * @param parent the system unit from which this alternate unit is
45      * derived.
46      * @throws IllegalArgumentException if the specified parent unit is not a
47      * system unit (base unit, alternate unit or rational product
48      * of base units and alternate units).
49      * @throws IllegalArgumentException if the specified symbol is
50      * associated to a different unit.
51      */

52     public AlternateUnit(String JavaDoc symbol, Unit parent) {
53         if (!parent.getSystemUnit().equals(parent))
54             throw new IllegalArgumentException JavaDoc(parent + " is not a system unit");
55         _symbol = symbol;
56         _parent = parent;
57         // Checks if the symbol is associated to a different unit.
58
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; // OK, same unit.
69
}
70             throw new IllegalArgumentException JavaDoc("Symbol " + symbol
71                     + " is associated to a different unit");
72         }
73     }
74
75     /**
76      * Returns the symbol for this alternate unit.
77      *
78      * @return this alternate unit symbol.
79      */

80     public final String JavaDoc getSymbol() {
81         return _symbol;
82     }
83
84     /**
85      * Returns the parent unit from which this alternate unit is derived
86      * (a system unit itself).
87      *
88      * @return the parent of the alternate unit.
89      */

90     @SuppressWarnings JavaDoc("unchecked")
91     public final Unit<? super Q> getParent() {
92         return _parent;
93     }
94
95     @Override JavaDoc
96     public final Unit<? super Q> getSystemUnit() {
97         return this;
98     }
99
100     @Override JavaDoc
101     public final UnitConverter toSystemUnit() {
102         return UnitConverter.IDENTITY;
103     }
104
105     /**
106      * Indicates if this alternate unit is considered equals to the specified
107      * object (both are alternate units with equal symbol, equal base units
108      * and equal converter to base units).
109      *
110      * @param that the object to compare for equality.
111      * @return <code>true</code> if <code>this</code> and <code>that</code>
112      * are considered equals; <code>false</code>otherwise.
113      */

114     public boolean equals(Object JavaDoc 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); // Symbols are unique.
121
}
122
123     // Implements abstract method.
124
public int hashCode() {
125         return _symbol.hashCode();
126     }
127
128     private static final long serialVersionUID = 1L;
129 }
Popular Tags