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.unit; |
10 | |
11 | import javax.measure.converter.UnitConverter; |
12 | import javax.measure.quantity.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> Instances of this class are created through the |
19 | * {@link Unit#alternate(String)} method.</p> |
20 | * |
21 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
22 | * @version 4.2, August 26, 2007 |
23 | */ |
24 | public final class AlternateUnit<Q extends Quantity> extends DerivedUnit<Q> { |
25 | |
26 | /** |
27 | * Holds the symbol. |
28 | */ |
29 | private final String _symbol; |
30 | |
31 | /** |
32 | * Holds the parent unit (a system unit). |
33 | */ |
34 | private final Unit<?> _parent; |
35 | |
36 | /** |
37 | * Creates an alternate unit for the specified unit identified by the |
38 | * specified symbol. |
39 | * |
40 | * @param symbol the symbol for this alternate unit. |
41 | * @param parent the system unit from which this alternate unit is |
42 | * derived. |
43 | * @throws UnsupportedOperationException if the source is not |
44 | * a standard unit. |
45 | * @throws IllegalArgumentException if the specified symbol is |
46 | * associated to a different unit. |
47 | */ |
48 | AlternateUnit(String symbol, Unit<?> parent) { |
49 | if (!parent.isStandardUnit()) |
50 | throw new UnsupportedOperationException(this |
51 | + " is not a standard unit"); |
52 | _symbol = symbol; |
53 | _parent = parent; |
54 | // Checks if the symbol is associated to a different unit. |
55 | synchronized (Unit.SYMBOL_TO_UNIT) { |
56 | Unit<?> unit = Unit.SYMBOL_TO_UNIT.get(symbol); |
57 | if (unit == null) { |
58 | Unit.SYMBOL_TO_UNIT.put(symbol, this); |
59 | return; |
60 | } |
61 | if (unit instanceof AlternateUnit) { |
62 | AlternateUnit<?> existingUnit = (AlternateUnit<?>) unit; |
63 | if (symbol.equals(existingUnit._symbol) |
64 | && _parent.equals(existingUnit._parent)) |
65 | return; // OK, same unit. |
66 | } |
67 | throw new IllegalArgumentException("Symbol " + symbol |
68 | + " is associated to a different unit"); |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Returns the symbol for this alternate unit. |
74 | * |
75 | * @return this alternate unit symbol. |
76 | */ |
77 | public final String getSymbol() { |
78 | return _symbol; |
79 | } |
80 | |
81 | /** |
82 | * Returns the parent unit from which this alternate unit is derived |
83 | * (a system unit itself). |
84 | * |
85 | * @return the parent of the alternate unit. |
86 | */ |
87 | @SuppressWarnings("unchecked") |
88 | public final Unit<? super Q> getParent() { |
89 | return (Unit<? super Q>) _parent; |
90 | } |
91 | |
92 | @Override |
93 | public final Unit<? super Q> getStandardUnit() { |
94 | return this; |
95 | } |
96 | |
97 | @Override |
98 | public final UnitConverter toStandardUnit() { |
99 | return UnitConverter.IDENTITY; |
100 | } |
101 | |
102 | /** |
103 | * Indicates if this alternate unit is considered equals to the specified |
104 | * object (both are alternate units with equal symbol, equal base units |
105 | * and equal converter to base units). |
106 | * |
107 | * @param that the object to compare for equality. |
108 | * @return <code>true</code> if <code>this</code> and <code>that</code> |
109 | * are considered equals; <code>false</code>otherwise. |
110 | */ |
111 | public boolean equals(Object that) { |
112 | if (this == that) |
113 | return true; |
114 | if (!(that instanceof AlternateUnit)) |
115 | return false; |
116 | AlternateUnit<?> thatUnit = (AlternateUnit<?>) that; |
117 | return this._symbol.equals(thatUnit._symbol); // Symbols are unique. |
118 | } |
119 | |
120 | // Implements abstract method. |
121 | public int hashCode() { |
122 | return _symbol.hashCode(); |
123 | } |
124 | |
125 | private static final long serialVersionUID = 1L; |
126 | } |