001/*
002 * Units of Measurement Implementation for Java SE
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.uom.se.unit;
031
032import java.util.Map;
033import java.util.Objects;
034
035import javax.measure.Dimension;
036import javax.measure.Quantity;
037import javax.measure.Unit;
038import javax.measure.UnitConverter;
039
040import tec.uom.lib.common.function.UnitConverterSupplier;
041import tec.uom.se.AbstractUnit;
042
043/**
044 * <p>
045 * This class represents the units derived from other units using {@linkplain UnitConverter converters}.
046 * </p>
047 *
048 * <p>
049 * Examples of transformed units:<code>
050 *         CELSIUS = KELVIN.shift(273.15);
051 *         FOOT = METRE.multiply(3048).divide(10000);
052 *         MILLISECOND = MILLI(SECOND);
053 *     </code>
054 * </p>
055 *
056 * <p>
057 * Transformed units have no symbol. But like any other units, they may have labels attached to them (see
058 * {@link javax.measure.format.UnitFormat#label(Unit, String) UnitFormat.label}
059 * </p>
060 *
061 * <p>
062 * Instances of this class are created through the {@link AbstractUnit#transform} method.
063 * </p>
064 *
065 * @param <Q>
066 *          The type of the quantity measured by this unit.
067 *
068 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
069 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
070 * @version 1.0.4, June 9, 2017
071 * @since 1.0
072 */
073public final class TransformedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> implements UnitConverterSupplier {
074
075  /**
076     *
077     */
078  private static final long serialVersionUID = 1L;
079
080  /**
081   * Holds the parent unit.
082   */
083  private final AbstractUnit<Q> parentUnit;
084
085  /**
086   * Holds the system unit.
087   */
088  private final Unit<Q> systemUnit;
089
090  /**
091   * Holds the converter to the parent unit.
092   */
093  private final UnitConverter converter;
094
095  /**
096   * Holds the symbol.
097   */
098  private String symbol;
099
100  /**
101   * Creates a transformed unit from the specified system unit. using the parent as symbol
102   * 
103   * @param parentUnit
104   *          the system unit from which this unit is derived.
105   * @param converter
106   *          the converter to the parent units.
107   */
108  public TransformedUnit(Unit<Q> parentUnit, UnitConverter unitConverter) {
109    this(null, parentUnit, unitConverter);
110  }
111
112  /**
113   * Creates a transformed unit from the specified parent unit.
114   *
115   * @param symbol
116   *          the symbol to use with this transformed unit.
117   * @param parentUnit
118   *          the parent unit from which this unit is derived.
119   * @param unitConverter
120   *          the converter to the parent units.
121   */
122  public TransformedUnit(String symbol, Unit<Q> parentUnit, UnitConverter unitConverter) {
123    this(symbol, parentUnit, parentUnit.getSystemUnit(), unitConverter);
124  }
125
126  /**
127   * Creates a transformed unit from the specified parent and system unit. using the parent as symbol
128   * 
129   * @param parentUnit
130   *          the parent unit from which this unit is derived.
131   * @param sysUnit
132   *          the system unit which this unit is based on.
133   * @param converter
134   *          the converter to the parent units.
135   */
136  public TransformedUnit(String symbol, Unit<Q> parentUnit, Unit<Q> sysUnit, UnitConverter unitConverter) {
137    if (parentUnit instanceof AbstractUnit) {
138      final AbstractUnit<Q> abParent = (AbstractUnit<Q>) parentUnit;
139
140      this.systemUnit = sysUnit;
141      // if (!abParent.isSystemUnit()) {
142      // throw new IllegalArgumentException("The parent unit: " + abParent
143      // + " is not a system unit");
144      // }
145      this.parentUnit = abParent;
146      this.converter = unitConverter;
147      this.symbol = symbol;
148      // see https://github.com/unitsofmeasurement/uom-se/issues/54
149    } else {
150      throw new IllegalArgumentException("The parent unit: " + parentUnit + " is not an abstract unit.");
151    }
152  }
153
154  @Override
155  public Dimension getDimension() {
156    return parentUnit.getDimension();
157  }
158
159  @Override
160  public UnitConverter getSystemConverter() {
161    return parentUnit.getSystemConverter().concatenate(converter);
162  }
163
164  /**
165   * Returns the converter to the parent unit.
166   *
167   * @return the converter to the parent unit.
168   */
169  @Override
170  public UnitConverter getConverter() {
171    return converter;
172  }
173
174  @Override
175  protected Unit<Q> toSystemUnit() {
176    return (systemUnit != null ? systemUnit : parentUnit.getSystemUnit());
177  }
178
179  @Override
180  public Map<? extends Unit<?>, Integer> getBaseUnits() {
181    return parentUnit.getBaseUnits();
182  }
183
184  @Override
185  public int hashCode() {
186    return Objects.hash(parentUnit, converter);
187  }
188
189  @Override
190  public boolean equals(Object obj) {
191    if (this == obj) {
192      return true;
193    }
194    if (obj instanceof TransformedUnit) {
195      TransformedUnit<?> other = (TransformedUnit<?>) obj;
196      return Objects.equals(parentUnit, other.parentUnit) && Objects.equals(converter, other.converter);
197    }
198    return false;
199  }
200
201  @Override
202  public String getSymbol() {
203    return symbol;
204  }
205
206  /**
207   * Returns the parent unit for this unit. The parent unit is the untransformed unit from which this unit is derived.
208   *
209   * @return the untransformed unit from which this unit is derived.
210   */
211  public Unit<Q> getParentUnit() {
212    return parentUnit;
213  }
214}