| 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 units derived from other units using |
| 16 | * {@link UnitConverter converters}.</p> |
| 17 | * |
| 18 | * <p> Examples of transformed units:[code] |
| 19 | * CELSIUS = KELVIN.add(273.15); |
| 20 | * FOOT = METER.multiply(0.3048); |
| 21 | * MILLISECOND = MILLI(SECOND); |
| 22 | * [/code]</p> |
| 23 | * |
| 24 | * <p> Transformed units have no label. But like any other units, |
| 25 | * they may have labels attached to them:[code] |
| 26 | * UnitFormat.getStandardInstance().label(FOOT, "ft"); |
| 27 | * [/code] |
| 28 | * or aliases: [code] |
| 29 | * UnitFormat.getStandardInstance().alias(CENTI(METER)), "centimeter"); |
| 30 | * UnitFormat.getStandardInstance().alias(CENTI(METER)), "centimetre"); |
| 31 | * [/code]</p> |
| 32 | * |
| 33 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
| 34 | * @version 3.1, April 22, 2006 |
| 35 | * @see Unit#plus(double) |
| 36 | * @see Unit#times(double) |
| 37 | * @see Unit#transform(UnitConverter) |
| 38 | * @see UnitFormat |
| 39 | */ |
| 40 | public final class TransformedUnit<Q extends Quantity> extends DerivedUnit<Q> { |
| 41 | |
| 42 | /** |
| 43 | * Holds the parent unit (not a transformed unit). |
| 44 | */ |
| 45 | private final Unit<Q> _parentUnit; |
| 46 | |
| 47 | /** |
| 48 | * Holds the converter to the parent unit. |
| 49 | */ |
| 50 | private final UnitConverter _toParentUnit; |
| 51 | |
| 52 | /** |
| 53 | * Creates a transformed unit from the specified parent unit. |
| 54 | * |
| 55 | * @param parentUnit the untransformed unit from which this unit is |
| 56 | * derived. |
| 57 | * @param toParentUnit the converter to the parent units. |
| 58 | * @throws IllegalArgumentException if <code>toParentUnit == |
| 59 | * {@link UnitConverter#IDENTITY UnitConverter.IDENTITY}</code> |
| 60 | */ |
| 61 | TransformedUnit(Unit<Q> parentUnit, UnitConverter toParentUnit) { |
| 62 | if (toParentUnit == UnitConverter.IDENTITY) |
| 63 | throw new IllegalArgumentException("Identity not allowed"); |
| 64 | _parentUnit = parentUnit; |
| 65 | _toParentUnit = toParentUnit; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the parent unit for this unit. The parent unit is the |
| 70 | * untransformed unit from which this unit is derived. |
| 71 | * |
| 72 | * @return the untransformed unit from which this unit is derived. |
| 73 | */ |
| 74 | public Unit<Q> getParentUnit() { |
| 75 | return _parentUnit; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the converter to the parent unit. |
| 80 | * |
| 81 | * @return the converter to the parent unit. |
| 82 | */ |
| 83 | public UnitConverter toParentUnit() { |
| 84 | return _toParentUnit; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Indicates if this transformed unit is considered equals to the specified |
| 89 | * object (both are transformed units with equal parent unit and equal |
| 90 | * converter to parent unit). |
| 91 | * |
| 92 | * @param that the object to compare for equality. |
| 93 | * @return <code>true</code> if <code>this</code> and <code>that</code> |
| 94 | * are considered equals; <code>false</code>otherwise. |
| 95 | */ |
| 96 | public boolean equals(Object that) { |
| 97 | if (this == that) return true; |
| 98 | if (!(that instanceof TransformedUnit)) return false; |
| 99 | TransformedUnit<?> thatUnit = (TransformedUnit<?>) that; |
| 100 | return this._parentUnit.equals(thatUnit._parentUnit) && |
| 101 | this._toParentUnit.equals(thatUnit._toParentUnit); |
| 102 | } |
| 103 | |
| 104 | // Implements abstract method. |
| 105 | public int hashCode() { |
| 106 | return _parentUnit.hashCode() + _toParentUnit.hashCode(); |
| 107 | } |
| 108 | |
| 109 | // Implements abstract method. |
| 110 | public Unit<? super Q> getStandardUnit() { |
| 111 | return _parentUnit.getStandardUnit(); |
| 112 | } |
| 113 | |
| 114 | // Implements abstract method. |
| 115 | public UnitConverter toStandardUnit() { |
| 116 | return _parentUnit.toStandardUnit().concatenate(_toParentUnit); |
| 117 | } |
| 118 | |
| 119 | private static final long serialVersionUID = 1L; |
| 120 | |
| 121 | } |