| 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.unit; |
| 10 | |
| 11 | import javax.measure.converter.UnitConverter; |
| 12 | import javax.measure.quantity.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 higher unit. |
| 31 | */ |
| 32 | private final Unit<Q> _high; |
| 33 | |
| 34 | /** |
| 35 | * Holds the lower unit. |
| 36 | */ |
| 37 | private final Unit<Q> _low; |
| 38 | |
| 39 | /** |
| 40 | * Creates a compound unit from the specified units. |
| 41 | * |
| 42 | * @param high the high unit. |
| 43 | * @param low the lower unit(s) |
| 44 | * @throws IllegalArgumentException if both units do not the same system |
| 45 | * unit. |
| 46 | */ |
| 47 | CompoundUnit(Unit<Q> high, Unit<Q> low) { |
| 48 | if (!high.getStandardUnit().equals(low.getStandardUnit())) |
| 49 | throw new IllegalArgumentException( |
| 50 | "Both units do not have the same system unit"); |
| 51 | _high = high; |
| 52 | _low = low; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the lower unit of this compound unit. |
| 58 | * |
| 59 | * @return the lower unit. |
| 60 | */ |
| 61 | public Unit<Q> getLower() { |
| 62 | return _low; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the higher unit of this compound unit. |
| 67 | * |
| 68 | * @return the higher unit. |
| 69 | */ |
| 70 | public Unit<Q> getHigher() { |
| 71 | return _high; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Indicates if this compound unit is considered equals to the specified |
| 76 | * object (both are compound units with same composing units in the |
| 77 | * same order). |
| 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 that) { |
| 84 | if (this == that) |
| 85 | return true; |
| 86 | if (!(that instanceof CompoundUnit)) |
| 87 | return false; |
| 88 | CompoundUnit<?> thatUnit = (CompoundUnit<?>) that; |
| 89 | return this._high.equals(thatUnit._high) |
| 90 | && this._low.equals(thatUnit._low); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int hashCode() { |
| 95 | return _high.hashCode() ^ _low.hashCode(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Unit<? super Q> getStandardUnit() { |
| 100 | return _low.getStandardUnit(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public UnitConverter toStandardUnit() { |
| 105 | return _low.toStandardUnit(); |
| 106 | } |
| 107 | |
| 108 | private static final long serialVersionUID = 1L; |
| 109 | } |