KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import org.jscience.physics.quantities.Quantity;
12
13 /**
14  * <p> This class represents a unit derived from another unit using
15  * a {@link Converter}.</p>
16  * <p> Examples of transformed units:
17  * <pre><code>
18  * CELSIUS = KELVIN.add(273.15); // Use label from unit database.
19  * MILLISECOND = SECOND.multiply(1e-3); // Use label from unit database.
20  * </code></pre></p>
21  * <p> Transformed units have no intrinsic symbol. But like any other units,
22  * they may have labels attached to them:
23  * <pre><code>
24  * FOOT = METER.multiply(0.3048).label("ft");
25  * CENTIMETER = METER.multiply(0.01).label"cm");
26  * CALENDAR_YEAR = DAY.multiply(365).label("year");
27  * </code></pre>
28  * or aliases:
29  * <pre><code>
30  * KELVIN.add(273.15).alias("Celsius");
31  * METER.multiply(0.01).alias("centimeter");
32  * METER.multiply(0.01).alias("centimetre");
33  * </code></pre>
34  *
35  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
36  * @version 1.1, May 24, 2004
37  * @see Unit#plus(double)
38  * @see Unit#times(double)
39  * @see Unit#transform(Converter)
40  * @see UnitFormat
41  */

42 class TransformedUnit<Q extends Quantity> extends DerivedUnit<Q> {
43
44     /**
45      * Creates a transformed unit derived from the specified unit using
46      * the specified converter.
47      *
48      * @param sourceUnit the units from which this unit is derived.
49      * @param toParentUnit the converter to the parent unit.
50      */

51     protected TransformedUnit(Unit<? super Q> parentUnit, Converter toParentUnit) {
52         while (parentUnit instanceof TransformedUnit) {
53             toParentUnit = parentUnit._toParentUnit.concatenate(toParentUnit);
54             parentUnit = parentUnit._parentUnit;
55         }
56         _parentUnit = parentUnit;
57         _toParentUnit = toParentUnit;
58     }
59
60     // Implements abstract method.
61
protected boolean equalsImpl(Object JavaDoc that) {
62         return (that instanceof TransformedUnit)
63                 && (((TransformedUnit) that)._parentUnit == _parentUnit)
64                 && ((TransformedUnit) that)._toParentUnit.equals(_toParentUnit);
65     }
66
67     // Implements abstract method.
68
protected int hashCodeImpl() {
69         return _parentUnit.hashCode() + _toParentUnit.hashCode();
70     }
71
72     // Implements abstract method.
73
protected final Unit<? super Q> getParentUnitImpl() {
74         return _parentUnit;
75     }
76
77     // Implements abstract method.
78
protected final Converter toParentUnitImpl() {
79         return _toParentUnit;
80     }
81
82     private static final long serialVersionUID = 1L;
83 }
Popular Tags