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;
040
041/**
042 * <p>
043 * This class represents units used in expressions to distinguish between quantities of a different nature but of the same dimensions.
044 * </p>
045 *
046 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
047 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
048 * @version 1.0, October 6, 2016
049 * @since 1.0
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 AbstractUnit<?> 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(AbstractUnit<?> parentUnit, String symbol) {
079    if (!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    // Checks if the symbol is associated to a different unit. TODO verify
085    // if we want these checks
086    /*
087     * synchronized (AbstractUnit.SYMBOL_TO_UNIT) { AbstractUnit<?> unit =
088     * (AbstractUnit<?>) AbstractUnit.SYMBOL_TO_UNIT.get(symbol); if (unit
089     * == null) { AbstractUnit.SYMBOL_TO_UNIT.put(symbol, this); return; }
090     * if (unit instanceof AlternateUnit<?>) { AlternateUnit<?> existingUnit
091     * = (AlternateUnit<?>) unit; if
092     * (symbol.equals(existingUnit.getSymbol()) &&
093     * this.parentUnit.equals(existingUnit.parentUnit)) return; // OK, same
094     * unit. } throw new IllegalArgumentException("Symbol " + symbol +
095     * " is associated to a different unit"); }
096     */
097  }
098
099  /**
100   * Creates an alternate unit for the specified system unit identified by the specified name and symbol.
101   *
102   * @param parent
103   *          the system unit from which this alternate unit is derived.
104   * @param symbol
105   *          the symbol for this alternate unit.
106   * @throws IllegalArgumentException
107   *           if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit}
108   * @throws ClassCastException
109   *           if parentUnit is not a valid {@link Unit} implementation
110   */
111  public AlternateUnit(Unit<?> parentUnit, String symbol) {
112    this((AbstractUnit<?>) parentUnit, symbol);
113  }
114
115  /**
116   * Returns the parent unit of this alternate unit, always a system unit and never an alternate unit.
117   *
118   * @return the parent unit.
119   */
120  public AbstractUnit<?> getParentUnit() {
121    return parentUnit;
122  }
123
124  @Override
125  public String getSymbol() {
126    return symbol;
127  }
128
129  @Override
130  public Dimension getDimension() {
131    return parentUnit.getDimension();
132  }
133
134  @Override
135  public UnitConverter getSystemConverter() {
136    return parentUnit.getSystemConverter();
137  }
138
139  @Override
140  public AbstractUnit<Q> toSystemUnit() {
141    return this; // Alternate units are SI units.
142  }
143
144  @Override
145  public Map<? extends Unit<?>, Integer> getBaseUnits() {
146    return parentUnit.getBaseUnits();
147  }
148
149  @Override
150  public int hashCode() {
151    return symbol.hashCode();
152  }
153
154  @Override
155  public boolean equals(Object obj) {
156    if (this == obj)
157      return true;
158    if (!(obj instanceof AlternateUnit))
159      return false;
160    AlternateUnit<?> that = (AlternateUnit<?>) obj;
161    return this.parentUnit.equals(that.parentUnit) && this.symbol.equals(that.symbol);
162  }
163
164}