001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package tec.units.ri.unit;
031
032import java.util.Map;
033
034import javax.measure.Dimension;
035import javax.measure.Quantity;
036import javax.measure.Unit;
037import javax.measure.UnitConverter;
038
039import tec.units.ri.AbstractUnit;
040import tec.uom.lib.common.function.UnitConverterSupplier;
041
042/**
043 * <p>
044 * This class represents the units derived from other units using {@linkplain UnitConverter converters}.
045 * </p>
046 *
047 * <p>
048 * Examples of transformed units:<code>
049 *         CELSIUS = KELVIN.shift(273.15);
050 *         FOOT = METRE.multiply(3048).divide(10000);
051 *         MILLISECOND = MILLI(SECOND);
052 *     </code>
053 * </p>
054 *
055 * <p>
056 * Transformed units have no symbol. But like all other units, they may have labels attached to them (see
057 * {@link javax.measure.format.UnitFormat#label(Unit, String)}
058 * </p>
059 *
060 * <p>
061 * Instances of this class are created through the {@link AbstractUnit#transform} method.
062 * </p>
063 *
064 * @param <Q>
065 *          The type of the quantity measured by this unit.
066 *
067 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
068 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
069 * @version 1.0.2, June 7, 2017
070 * @since 1.0
071 */
072public final class TransformedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> implements UnitConverterSupplier {
073
074  /**
075   * 
076   */
077  // private static final long serialVersionUID = 1L;
078
079  /**
080   * Holds the parent unit.
081   */
082  private final AbstractUnit<Q> parentUnit;
083
084  /**
085   * Holds the system unit.
086   */
087  private final Unit<Q> systemUnit;
088
089  /**
090   * Holds the converter to the parent unit.
091   */
092  private final UnitConverter converter;
093
094  /**
095   * Holds the symbol.
096   */
097  private String symbol;
098
099  /**
100   * Creates a transformed unit from the specified parent and system unit. using the parent as symbol
101   * 
102   * @param parentUnit
103   *          the parent unit from which this unit is derived.
104   * @param sysUnit
105   *          the system unit which this unit is based on.
106   * @param converter
107   *          the converter to the parent units.
108   */
109  public TransformedUnit(String symbol, Unit<Q> parentUnit, Unit<Q> sysUnit, UnitConverter unitConverter) {
110    if (parentUnit instanceof AbstractUnit) {
111      final AbstractUnit<Q> abParent = (AbstractUnit<Q>) parentUnit;
112
113      this.systemUnit = sysUnit;
114      // if (!abParent.isSystemUnit()) {
115      // throw new IllegalArgumentException("The parent unit: " + abParent
116      // + " is not a system unit");
117      // }
118      this.parentUnit = abParent;
119      this.converter = unitConverter;
120      this.symbol = symbol;
121      // this.symbol = symbol; //TODO see
122      // https://github.com/unitsofmeasurement/uom-se/issues/54
123    } else {
124      throw new IllegalArgumentException("The parent unit: " + parentUnit + " is not an abstract unit.");
125    }
126  }
127
128  /**
129   * Creates a transformed unit from the specified parent unit.
130   *
131   * @param parentUnit
132   *          the system unit from which this unit is derived.
133   * @param unitConverter
134   *          the converter to the parent units.
135   */
136  public TransformedUnit(AbstractUnit<Q> parentUnit, UnitConverter unitConverter) {
137    this(null, parentUnit, unitConverter);
138  }
139
140  /**
141   * Creates a transformed unit from the specified parent unit.
142   *
143   * @param symbol
144   *          the symbol to use with this transformed unit.
145   * @param parentUnit
146   *          the parent unit from which this unit is derived.
147   * @param unitConverter
148   *          the converter to the parent units.
149   */
150  public TransformedUnit(String symbol, Unit<Q> parentUnit, UnitConverter unitConverter) {
151    this(null, parentUnit, parentUnit.getSystemUnit(), unitConverter);
152  }
153
154  /**
155   * Creates a transformed unit from the specified system unit.
156   *
157   * @param parentUnit
158   *          the system unit from which this unit is derived.
159   * @param toParentUnit
160   *          the converter to the parent units.
161   * @throws IllegalArgumentException
162   *           if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit}
163   * @throws ClassCastException
164   *           if parentUnit is not a valid {@link Unit} implementation
165   */
166  public TransformedUnit(Unit<Q> parentUnit, UnitConverter toParentUnit) {
167    this((AbstractUnit<Q>) parentUnit, toParentUnit);
168  }
169
170  @Override
171  public Dimension getDimension() {
172    return parentUnit.getDimension();
173  }
174
175  @Override
176  public UnitConverter getSystemConverter() {
177    return parentUnit.getSystemConverter().concatenate(converter);
178  }
179
180  @Override
181  public AbstractUnit<Q> toSystemUnit() {
182    return parentUnit.getSystemUnit();
183  }
184
185  @Override
186  public Map<? extends Unit<?>, Integer> getBaseUnits() {
187    return parentUnit.getBaseUnits();
188  }
189
190  @Override
191  public int hashCode() {
192    return parentUnit.hashCode() + converter.hashCode();
193  }
194
195  @Override
196  public boolean equals(Object that) {
197    if (this == that)
198      return true;
199    if (!(that instanceof TransformedUnit))
200      return false;
201    TransformedUnit<?> thatUnit = (TransformedUnit<?>) that;
202    return this.parentUnit.equals(thatUnit.parentUnit) && this.converter.equals(thatUnit.converter);
203  }
204
205  @Override
206  // TODO clarify JavaDoc or adjust to what the Unit JavaDoc says.
207  public String getSymbol() {
208    if (super.getSymbol() != null) {
209      return super.getSymbol();
210    }
211    return symbol;
212  }
213
214  /**
215   * Returns the parent unit for this unit. The parent unit is the untransformed unit from which this unit is derived.
216   *
217   * @return the untransformed unit from which this unit is derived.
218   */
219  public Unit<Q> getParentUnit() {
220    return parentUnit;
221  }
222
223  /**
224   * Returns the converter to the parent unit.
225   *
226   * @return the converter to the parent unit.
227   */
228  @Override
229  public UnitConverter getConverter() {
230    return converter;
231  }
232}