KickJava   Java API By Example, From Geeks To Geeks.

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


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 compound unit. Compound units are used
15  * to express quantites using multi-radix units. Instances of this
16  * class are created using the {@link Unit#compound} method.
17  * For example:<pre>
18  * Unit HOUR_MINUTE_SECOND
19  * = NonSI.HOUR.compound(NonSI.MINUTE).compound(SI.SECOND);</pre>
20  * </p>
21  *
22  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
23  * @version 1.1, May 24, 2005
24  * @see Unit#compound
25  */

26 public class CompoundUnit<Q extends Quantity> extends DerivedUnit<Q> {
27
28     /**
29      * Holds the main unit.
30      */

31     private final Unit<Q> _mainUnit;
32
33     /**
34      * Holds the sub-unit.
35      */

36     private final Unit<Q> _subUnit;
37
38     /**
39      * Creates a compound unit
40      *
41      * @param mainUnit the main unit.
42      * @param subUnit the sub-unit.
43      */

44     protected CompoundUnit(Unit<Q> mainUnit, Unit<Q> subUnit) {
45         _mainUnit = mainUnit;
46         _subUnit = subUnit;
47     }
48
49     /**
50      * Returns the main unit of this compound unit.
51      *
52      * @return the main unit.
53      */

54     public Unit<Q> getMainUnit() {
55         return _mainUnit;
56     }
57
58     /**
59      * Returns the sub-unit of this compound unit.
60      *
61      * @return the sub-unit.
62      */

63     public Unit<Q> getSubUnit() {
64         return _subUnit;
65     }
66
67     // Implements abstract method.
68
protected boolean equalsImpl(Object JavaDoc that) {
69         return (that instanceof CompoundUnit)
70                 && (((CompoundUnit) that)._mainUnit == _mainUnit)
71                 && ((CompoundUnit) that)._subUnit.equals(_subUnit);
72     }
73
74     // Implements abstract method.
75
protected int hashCodeImpl() {
76         return _mainUnit.hashCode() ^ _subUnit.hashCode();
77     }
78
79     // Implements abstract method.
80
protected Unit<? super Q> getParentUnitImpl() {
81         return _mainUnit.getParentUnit();
82     }
83
84     // Implements abstract method.
85
protected Converter toParentUnitImpl() {
86         return _mainUnit.toParentUnit();
87     }
88
89     private static final long serialVersionUID = 1L;
90 }
Popular Tags