| 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 building blocks on top of which all others |
| 16 | * units are created. Base units are typically dimensionally independent. |
| 17 | * The actual unit dimension is determinated by the current |
| 18 | * {@link Dimension.Model model}. For example using the {@link |
| 19 | * Dimension.Model#STANDARD standard} model, {@link SI#CANDELA} |
| 20 | * has the dimension of {@link SI#WATT watt}:[code] |
| 21 | * // Standard model. |
| 22 | * BaseUnit<Length> METER = new BaseUnit<Length>("m"); |
| 23 | * BaseUnit<LuminousIntensity> CANDELA = new BaseUnit<LuminousIntensity>("cd"); |
| 24 | * System.out.println(METER.getDimension()); |
| 25 | * System.out.println(CANDELA.getDimension()); |
| 26 | * |
| 27 | * > [L] |
| 28 | * > [L]²·[M]/[T]³ |
| 29 | * [/code]</p> |
| 30 | * <p> This class represents the "standard base units" which includes SI base |
| 31 | * units and possibly others user-defined base units. It does not represent |
| 32 | * the base units of any specific {@link SystemOfUnits} (they would have |
| 33 | * be base units accross all possible systems otherwise).</p> |
| 34 | * |
| 35 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
| 36 | * @version 3.1, April 22, 2006 |
| 37 | * @see <a href="http://en.wikipedia.org/wiki/SI_base_unit"> |
| 38 | * Wikipedia: SI base unit</a> |
| 39 | */ |
| 40 | public class BaseUnit<Q extends Quantity> extends Unit<Q> { |
| 41 | |
| 42 | /** |
| 43 | * Holds the symbol. |
| 44 | */ |
| 45 | private final String _symbol; |
| 46 | |
| 47 | /** |
| 48 | * Creates a base unit having the specified symbol. |
| 49 | * |
| 50 | * @param symbol the symbol of this base unit. |
| 51 | * @throws IllegalArgumentException if the specified symbol is |
| 52 | * associated to a different unit. |
| 53 | */ |
| 54 | public BaseUnit(String symbol) { |
| 55 | _symbol = symbol; |
| 56 | // Checks if the symbol is associated to a different unit. |
| 57 | synchronized (Unit.SYMBOL_TO_UNIT) { |
| 58 | Unit<?> unit = Unit.SYMBOL_TO_UNIT.get(symbol); |
| 59 | if (unit == null) { |
| 60 | Unit.SYMBOL_TO_UNIT.put(symbol, this); |
| 61 | return; |
| 62 | } |
| 63 | if (!(unit instanceof BaseUnit)) |
| 64 | throw new IllegalArgumentException("Symbol " + symbol |
| 65 | + " is associated to a different unit"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the unique symbol for this base unit. |
| 71 | * |
| 72 | * @return this base unit symbol. |
| 73 | */ |
| 74 | public final String getSymbol() { |
| 75 | return _symbol; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Indicates if this base unit is considered equals to the specified |
| 80 | * object (both are base units with equal symbol, standard dimension and |
| 81 | * standard transform). |
| 82 | * |
| 83 | * @param that the object to compare for equality. |
| 84 | * @return <code>true</code> if <code>this</code> and <code>that</code> |
| 85 | * are considered equals; <code>false</code>otherwise. |
| 86 | */ |
| 87 | public boolean equals(Object that) { |
| 88 | if (this == that) |
| 89 | return true; |
| 90 | if (!(that instanceof BaseUnit)) |
| 91 | return false; |
| 92 | BaseUnit<?> thatUnit = (BaseUnit<?>) that; |
| 93 | return this._symbol.equals(thatUnit._symbol); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
| 98 | return _symbol.hashCode(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Unit<? super Q> getStandardUnit() { |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public UnitConverter toStandardUnit() { |
| 108 | return UnitConverter.IDENTITY; |
| 109 | } |
| 110 | |
| 111 | private static final long serialVersionUID = 1L; |
| 112 | } |