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 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 1.0, October 6, 2016
056 * @since 1.0
057 */
058public final class AnnotatedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
059
060  /**
061   * Holds the actual unit.
062   */
063  private final Unit<?> actualUnit;
064
065  /**
066   * Holds the annotation.
067   */
068  private final String annotation;
069
070  /**
071   * Creates an annotated unit equivalent to the specified unit.
072   *
073   * @param actualUnit
074   *          the unit to be annotated.
075   * @param annotation
076   *          the annotation.
077   * @return the annotated unit.
078   */
079  public AnnotatedUnit(Unit<?> actualUnit, String annotation) {
080    this.actualUnit = (actualUnit instanceof AnnotatedUnit) ? ((AnnotatedUnit<Q>) actualUnit).actualUnit : actualUnit;
081    this.annotation = annotation;
082  }
083
084  /**
085   * Returns the actual unit of this annotated unit (never an annotated unit itself).
086   *
087   * @return the actual unit.
088   */
089  public Unit<?> getActualUnit() {
090    return actualUnit;
091  }
092
093  /**
094   * Returns the annotation of this annotated unit.
095   *
096   * @return the annotation.
097   */
098  public String getAnnotation() {
099    return annotation;
100  }
101
102  @Override
103  public String getSymbol() {
104    return actualUnit.getSymbol();
105  }
106
107  @Override
108  public Map<? extends Unit<?>, Integer> getBaseUnits() {
109    return actualUnit.getBaseUnits();
110  }
111
112  @Override
113  public AbstractUnit<Q> toSystemUnit() {
114    return (AbstractUnit<Q>) actualUnit.getSystemUnit();
115  }
116
117  @Override
118  public Dimension getDimension() {
119    return actualUnit.getDimension();
120  }
121
122  @Override
123  public UnitConverter getSystemConverter() {
124    if (actualUnit instanceof AbstractUnit) {
125      return ((AbstractUnit<?>) actualUnit).getSystemConverter();
126    } else {
127      throw new ClassCastException("Unknown type " + actualUnit.getClass());
128    }
129  }
130
131  @Override
132  public int hashCode() {
133    return actualUnit.hashCode() + annotation.hashCode();
134  }
135
136  @Override
137  public boolean equals(Object that) {
138    if (this == that)
139      return true;
140    if (!(that instanceof AnnotatedUnit<?>))
141      return false;
142    AnnotatedUnit<?> thatUnit = (AnnotatedUnit<?>) that;
143    return this.actualUnit.equals(thatUnit.actualUnit) && this.annotation.equals(thatUnit.annotation);
144  }
145}