001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2016, 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 an annotated unit.
044 * </p>
045 * 
046 * <p>
047 * Instances of this class are created through the {@link AbstractUnit#annotate(String)} method.
048 * </p>
049 *
050 * @param <Q>
051 *          The type of the quantity measured by this unit.
052 *
053 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
054 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
055 * @version 0.4, January 18, 2015
056 */
057public final class AnnotatedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
058
059  /**
060   * Holds the actual unit.
061   */
062  private final Unit<?> actualUnit;
063
064  /**
065   * Holds the annotation.
066   */
067  private final String annotation;
068
069  /**
070   * Creates an annotated unit equivalent to the specified unit.
071   *
072   * @param actualUnit
073   *          the unit to be annotated.
074   * @param annotation
075   *          the annotation.
076   * @return the annotated unit.
077   */
078  public AnnotatedUnit(Unit<?> actualUnit, String annotation) {
079    this.actualUnit = (actualUnit instanceof AnnotatedUnit) ? ((AnnotatedUnit<Q>) actualUnit).actualUnit : actualUnit;
080    this.annotation = annotation;
081  }
082
083  /**
084   * Returns the actual unit of this annotated unit (never an annotated unit itself).
085   *
086   * @return the actual unit.
087   */
088  public Unit<?> getActualUnit() {
089    return actualUnit;
090  }
091
092  /**
093   * Returns the annotation of this annotated unit.
094   *
095   * @return the annotation.
096   */
097  public String getAnnotation() {
098    return annotation;
099  }
100
101  @Override
102  public String getSymbol() {
103    return actualUnit.getSymbol();
104  }
105
106  @Override
107  public Map<? extends Unit<?>, Integer> getBaseUnits() {
108    return actualUnit.getBaseUnits();
109  }
110
111  @Override
112  public AbstractUnit<Q> toSystemUnit() {
113    return (AbstractUnit<Q>) actualUnit.getSystemUnit();
114  }
115
116  @Override
117  public Dimension getDimension() {
118    return actualUnit.getDimension();
119  }
120
121  @Override
122  public UnitConverter getSystemConverter() {
123    if (actualUnit instanceof AbstractUnit) {
124      return ((AbstractUnit<?>) actualUnit).getSystemConverter();
125    } else {
126      throw new ClassCastException("Unknown type " + actualUnit.getClass());
127    }
128  }
129
130  @Override
131  public int hashCode() {
132    return actualUnit.hashCode() + annotation.hashCode();
133  }
134
135  @Override
136  public boolean equals(Object that) {
137    if (this == that)
138      return true;
139    if (!(that instanceof AnnotatedUnit<?>))
140      return false;
141    AnnotatedUnit<?> thatUnit = (AnnotatedUnit<?>) that;
142    return this.actualUnit.equals(thatUnit.actualUnit) && this.annotation.equals(thatUnit.annotation);
143  }
144}