001: /*
002: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003: * Copyright (C) 2006 - JScience (http://jscience.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javax.measure.unit;
010:
011: import javax.measure.converter.UnitConverter;
012: import javax.measure.quantity.Quantity;
013:
014: /**
015: * <p> This class represents the units derived from other units using
016: * {@link UnitConverter converters}.</p>
017: *
018: * <p> Examples of transformed units:[code]
019: * CELSIUS = KELVIN.add(273.15);
020: * FOOT = METER.multiply(0.3048);
021: * MILLISECOND = MILLI(SECOND);
022: * [/code]</p>
023: *
024: * <p> Transformed units have no label. But like any other units,
025: * they may have labels attached to them:[code]
026: * UnitFormat.getStandardInstance().label(FOOT, "ft");
027: * [/code]
028: * or aliases: [code]
029: * UnitFormat.getStandardInstance().alias(CENTI(METER)), "centimeter");
030: * UnitFormat.getStandardInstance().alias(CENTI(METER)), "centimetre");
031: * [/code]</p>
032: *
033: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
034: * @version 3.1, April 22, 2006
035: * @see Unit#plus(double)
036: * @see Unit#times(double)
037: * @see Unit#transform(UnitConverter)
038: * @see UnitFormat
039: */
040: public final class TransformedUnit<Q extends Quantity> extends
041: DerivedUnit<Q> {
042:
043: /**
044: * Holds the parent unit (not a transformed unit).
045: */
046: private final Unit<Q> _parentUnit;
047:
048: /**
049: * Holds the converter to the parent unit.
050: */
051: private final UnitConverter _toParentUnit;
052:
053: /**
054: * Creates a transformed unit from the specified parent unit.
055: *
056: * @param parentUnit the untransformed unit from which this unit is
057: * derived.
058: * @param toParentUnit the converter to the parent units.
059: * @throws IllegalArgumentException if <code>toParentUnit ==
060: * {@link UnitConverter#IDENTITY UnitConverter.IDENTITY}</code>
061: */
062: TransformedUnit(Unit<Q> parentUnit, UnitConverter toParentUnit) {
063: if (toParentUnit == UnitConverter.IDENTITY)
064: throw new IllegalArgumentException("Identity not allowed");
065: _parentUnit = parentUnit;
066: _toParentUnit = toParentUnit;
067: }
068:
069: /**
070: * Returns the parent unit for this unit. The parent unit is the
071: * untransformed unit from which this unit is derived.
072: *
073: * @return the untransformed unit from which this unit is derived.
074: */
075: public Unit<Q> getParentUnit() {
076: return _parentUnit;
077: }
078:
079: /**
080: * Returns the converter to the parent unit.
081: *
082: * @return the converter to the parent unit.
083: */
084: public UnitConverter toParentUnit() {
085: return _toParentUnit;
086: }
087:
088: /**
089: * Indicates if this transformed unit is considered equals to the specified
090: * object (both are transformed units with equal parent unit and equal
091: * converter to parent unit).
092: *
093: * @param that the object to compare for equality.
094: * @return <code>true</code> if <code>this</code> and <code>that</code>
095: * are considered equals; <code>false</code>otherwise.
096: */
097: public boolean equals(Object that) {
098: if (this == that)
099: return true;
100: if (!(that instanceof TransformedUnit))
101: return false;
102: TransformedUnit<?> thatUnit = (TransformedUnit<?>) that;
103: return this ._parentUnit.equals(thatUnit._parentUnit)
104: && this ._toParentUnit.equals(thatUnit._toParentUnit);
105: }
106:
107: // Implements abstract method.
108: public int hashCode() {
109: return _parentUnit.hashCode() + _toParentUnit.hashCode();
110: }
111:
112: // Implements abstract method.
113: public Unit<? super Q> getStandardUnit() {
114: return _parentUnit.getStandardUnit();
115: }
116:
117: // Implements abstract method.
118: public UnitConverter toStandardUnit() {
119: return _parentUnit.toStandardUnit().concatenate(_toParentUnit);
120: }
121:
122: private static final long serialVersionUID = 1L;
123:
124: }
|