KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > physics > units > BaseUnit


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - 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 org.jscience.physics.units;
10 import org.jscience.physics.quantities.Quantity;
11
12 import javolution.realtime.LocalReference;
13
14 /**
15  * <p> This class represents the building blocks on top of which all others
16  * units are created.</p>
17  * <p> Base units are typically mutually independent. Although, in specialized
18  * context (e.g. relativistic context), conversions between base units
19  * is possible (Ref. {@link #setDimension}).</p>
20  * <p> Examples of base units:
21  * <pre><code>
22  * METER = new BaseUnit&lt;Length>("m", Dimension.LENGTH, Converter.IDENTITY);
23  * KILOGRAM = new BaseUnit&lt;Mass>("kg", Dimension.MASS, Converter.IDENTITY);
24  * SECOND = new BaseUnit&lt;Duration>("s", Dimension.TIME, Converter.IDENTITY);
25  * </code></pre></p>
26  *
27  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
28  * @version 1.1, May 24, 2005
29  */

30 public class BaseUnit<Q extends Quantity> extends Unit<Q> {
31
32     /**
33      * Holds the local dimension.
34      */

35     final LocalReference<Dimension> _dimension;
36
37     /**
38      * Holds the local transform.
39      */

40     final LocalReference<Converter> _transform;
41
42     /**
43      * Creates a new base unit having the specified symbol.
44      *
45      * @param symbol the symbol of this base unit.
46      * @param dimension the default dimension for this unit.
47      * @param transform the default intrinsic dimensional transform.
48      * @throws IllegalArgumentException if the specified symbol is already
49      * used.
50      */

51     public BaseUnit(String JavaDoc symbol, Dimension dimension, Converter transform) {
52         _symbol = symbol;
53         _dimension = new LocalReference<Dimension>(dimension);
54         _transform = new LocalReference<Converter>(transform);
55         getInstance(this); // Ensures unicity.
56
}
57
58     /**
59      * Returns the symbol for this base unit.
60      *
61      * @return this base unit symbol.
62      */

63     public final String JavaDoc getSymbol() {
64         return _symbol;
65     }
66
67     /**
68      * Sets the {@link javolution.realtime.LocalContext local} dimension of
69      * this base unit. For example:<pre>
70      * LocalContext.enter(); // Ensures that setting is local to current thread.
71      * try {
72      * SI.METER.setDimension(TIME, new MultiplyConverter(1e9 / c));
73      * // In this high-energy context, length and time are compatible,
74      * // they have the same "ns" dimensional unit.
75      * } finally {
76      * LocalContext.exit();
77      * }</pre>
78      *
79      * @param dimension the unit identifying the new dimension of this
80      * base unit.
81      * @param transform the dimensional intrinsic transform for this
82      * base unit (typically a multiply converter).
83      */

84     public void setDimension(Dimension dimension, Converter transform) {
85         _dimension.set(dimension);
86         _transform.set(transform);
87     }
88
89    // Implements abstract method.
90
protected boolean equalsImpl(Object JavaDoc that) {
91         return (that instanceof BaseUnit) &&
92                 ((BaseUnit) that)._symbol.equals(_symbol);
93     }
94
95     // Implements abstract method.
96
protected int hashCodeImpl() {
97         return _symbol.hashCode();
98     }
99
100     // Implements abstract method.
101
protected Unit<Q> getParentUnitImpl() {
102         return this;
103     }
104     
105     // Implements abstract method.
106
protected Converter toParentUnitImpl() {
107         return Converter.IDENTITY;
108     }
109     
110     private static final long serialVersionUID = 1L;
111 }
Popular Tags