KickJava   Java API By Example, From Geeks To Geeks.

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


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 multi-radix units (such as "hour:min:sec").
16  * Instances of this class are created using the {@link Unit#compound
17  * Unit.compound} method.</p>
18  *
19  * <p> Examples of compound units:[code]
20  * Unit<Duration> HOUR_MINUTE_SECOND = HOUR.compound(MINUTE).compound(SECOND);
21  * Unit<Angle> DEGREE_MINUTE_ANGLE = DEGREE_ANGLE.compound(MINUTE_ANGLE);
22  * [/code]</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 3.1, April 22, 2006
26  */

27 public final class CompoundUnit<Q extends Quantity> extends DerivedUnit<Q> {
28
29     /**
30      * Holds the first unit (cannot be a compound unit)
31      */

32     private final Unit<Q> _first;
33
34     /**
35      * Holds the next units.
36      */

37     private final Unit<Q> _next;
38
39     /**
40      * Creates a compound unit from the specified units.
41      *
42      * @param unit the first unit.
43      * @param next the next unit(s)
44      * @throws IllegalArgumentException if both units do not the same system
45      * unit.
46      */

47     CompoundUnit(Unit<Q> first, Unit<Q> next) {
48         if (!first.getSystemUnit().equals(next.getSystemUnit()))
49             throw new IllegalArgumentException JavaDoc(
50                     "Both units do not have the same system unit");
51         if (first instanceof CompoundUnit) {
52             CompoundUnit<Q> firstUnit = (CompoundUnit<Q>) first;
53             _first = firstUnit._first;
54             _next = new CompoundUnit<Q>(firstUnit._next, next);
55         } else {
56             _first = first;
57             _next = next;
58         }
59     }
60
61     /**
62      * Returns the first unit of this compound unit (never a compound unit)
63      *
64      * @return the first unit.
65      */

66     public Unit<Q> getFirst() {
67         return _first;
68     }
69
70     /**
71      * Returns the next unit(s) of this compound unit (can be a compound unit).
72      *
73      * @return the next units.
74      */

75     public Unit<Q> getNext() {
76         return _next;
77     }
78
79     /**
80      * Indicates if this compound unit is considered equals to the specified
81      * object (both are compound units with same composing units in the
82      * same order).
83      *
84      * @param that the object to compare for equality.
85      * @return <code>true</code> if <code>this</code> and <code>that</code>
86      * are considered equals; <code>false</code>otherwise.
87      */

88     public boolean equals(Object JavaDoc that) {
89         if (this == that)
90             return true;
91         if (!(that instanceof CompoundUnit))
92             return false;
93         CompoundUnit thatUnit = (CompoundUnit) that;
94         return this._first.equals(thatUnit._first)
95                 && this._next.equals(thatUnit._next);
96     }
97
98     @Override JavaDoc
99     public int hashCode() {
100         return _first.hashCode() ^ _next.hashCode();
101     }
102
103     @Override JavaDoc
104     public Unit<? super Q> getSystemUnit() {
105         return _first.getSystemUnit(); // equals(_next.getSystemUnit())
106
}
107
108     @Override JavaDoc
109     public UnitConverter toSystemUnit() {
110         return _first.toSystemUnit();
111     }
112
113     private static final long serialVersionUID = 1L;
114 }
Popular Tags