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 javax.measure.Dimension;
033import javax.measure.Quantity;
034import javax.measure.Unit;
035import javax.measure.UnitConverter;
036
037import tec.uom.se.AbstractUnit;
038
039import java.util.Map;
040import java.util.Objects;
041
042/**
043 * <p>
044 * This class represents units used in expressions to distinguish between quantities of a different nature but of the same dimensions.
045 * </p>
046 *
047 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
048 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
049 * @version 0.3, June 09, 2014
050 */
051public final class AlternateUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
052
053  /**
054         * 
055         */
056  private static final long serialVersionUID = 4696690756456282705L;
057
058  /**
059   * Holds the parent unit (a system unit).
060   */
061  private final Unit<?> parentUnit;
062
063  /**
064   * Holds the symbol for this unit.
065   */
066  private final String symbol;
067
068  /**
069   * Creates an alternate unit for the specified system unit identified by the specified name and symbol.
070   *
071   * @param parent
072   *          the system unit from which this alternate unit is derived.
073   * @param symbol
074   *          the symbol for this alternate unit.
075   * @throws IllegalArgumentException
076   *           if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit}
077   */
078  public AlternateUnit(Unit<?> parentUnit, String symbol) {
079    if (!((AbstractUnit) parentUnit).isSystemUnit())
080      throw new IllegalArgumentException("The parent unit: " + parentUnit + " is not an unscaled SI unit");
081    this.parentUnit = (parentUnit instanceof AlternateUnit) ? ((AlternateUnit) parentUnit).getParentUnit() : parentUnit;
082    this.symbol = symbol;
083  }
084
085  /**
086   * Returns the parent unit of this alternate unit, always a system unit and never an alternate unit.
087   *
088   * @return the parent unit.
089   */
090  public Unit<?> getParentUnit() {
091    return parentUnit;
092  }
093
094  @Override
095  public String getSymbol() {
096    return symbol;
097  }
098
099  @Override
100  public Dimension getDimension() {
101    return parentUnit.getDimension();
102  }
103
104  @Override
105  public UnitConverter getSystemConverter() {
106    return ((AbstractUnit) parentUnit).getSystemConverter();
107  }
108
109  @Override
110  public AbstractUnit<Q> toSystemUnit() {
111    return this; // Alternate units are SI units.
112  }
113
114  @Override
115  public Map<? extends Unit<?>, Integer> getBaseUnits() {
116    return parentUnit.getBaseUnits();
117  }
118
119  @Override
120  public int hashCode() {
121    return Objects.hashCode(symbol);
122  }
123
124  @Override
125  public boolean equals(Object obj) {
126    if (this == obj) {
127      return true;
128    }
129    if (obj instanceof AlternateUnit) {
130      AlternateUnit that = (AlternateUnit) obj;
131      return Objects.equals(parentUnit, that.parentUnit) && Objects.equals(symbol, that.symbol);
132    }
133    return false;
134  }
135
136}