KickJava   Java API By Example, From Geeks To Geeks.

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


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 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  *
31  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
32  * @version 3.1, April 22, 2006
33  * @see <a HREF="http://en.wikipedia.org/wiki/SI_base_unit">
34  * Wikipedia: SI base unit</a>
35  */

36 public class BaseUnit<Q extends Quantity> extends Unit<Q> {
37
38     /**
39      * Holds the symbol.
40      */

41     private final String JavaDoc _symbol;
42
43     /**
44      * Creates a base unit having the specified symbol.
45      *
46      * @param symbol the symbol of this base unit.
47      * @throws IllegalArgumentException if the specified symbol is
48      * associated to a different unit.
49      */

50     public BaseUnit(String JavaDoc symbol) {
51         _symbol = symbol;
52         // Checks if the symbol is associated to a different unit.
53
synchronized (Unit.SYMBOL_TO_UNIT) {
54             Unit unit = Unit.SYMBOL_TO_UNIT.get(symbol);
55             if (unit == null) {
56                 Unit.SYMBOL_TO_UNIT.put(symbol, this);
57                 return;
58             }
59             if (!(unit instanceof BaseUnit))
60                throw new IllegalArgumentException JavaDoc("Symbol " + symbol
61                     + " is associated to a different unit");
62         }
63     }
64
65     /**
66      * Returns the unique symbol for this base unit.
67      *
68      * @return this base unit symbol.
69      */

70     public final String JavaDoc getSymbol() {
71         return _symbol;
72     }
73
74     /**
75      * Indicates if this base unit is considered equals to the specified
76      * object (both are base units with equal symbol, standard dimension and
77      * standard transform).
78      *
79      * @param that the object to compare for equality.
80      * @return <code>true</code> if <code>this</code> and <code>that</code>
81      * are considered equals; <code>false</code>otherwise.
82      */

83     public boolean equals(Object JavaDoc that) {
84         if (this == that)
85             return true;
86         if (!(that instanceof BaseUnit))
87             return false;
88         BaseUnit thatUnit = (BaseUnit) that;
89         return this._symbol.equals(thatUnit._symbol);
90     }
91         
92     @Override JavaDoc
93     public int hashCode() {
94         return _symbol.hashCode();
95     }
96
97     @Override JavaDoc
98     public Unit<? super Q> getSystemUnit() {
99         return this;
100     }
101
102     @Override JavaDoc
103     public UnitConverter toSystemUnit() {
104         return UnitConverter.IDENTITY;
105     }
106
107     private static final long serialVersionUID = 1L;
108 }
Popular Tags