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 tec.uom.se.AbstractConverter;
033import tec.uom.se.AbstractUnit;
034import tec.uom.se.quantity.QuantityDimension;
035
036import javax.measure.Dimension;
037import javax.measure.Quantity;
038import javax.measure.Unit;
039import javax.measure.UnitConverter;
040
041import java.util.Map;
042
043/**
044 * <p>
045 * This class represents the building blocks on top of which all others physical units are created. Base units are always unscaled SI units.
046 * </p>
047 * 
048 * <p>
049 * When using the {@link tec.uom.se.spi.StandardModel standard model}, all seven <b>SI</b> base units are dimensionally independent.
050 * </p>
051 *
052 * @see <a href="http://en.wikipedia.org/wiki/SI_base_unit"> Wikipedia: SI base unit</a>
053 *
054 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
055 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
056 * @version 1.0, Jan 21, 2017
057 */
058public final class BaseUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
059
060  /**
061     * 
062     */
063  private static final long serialVersionUID = 1721629233768215930L;
064
065  /**
066   * Holds the symbol.
067   */
068  private final String symbol;
069
070  /**
071   * Holds the base unit dimension.
072   */
073  private final Dimension dimension;
074
075  /**
076   * Creates a base unit having the specified symbol and dimension.
077   *
078   * @param symbol
079   *          the symbol of this base unit.
080   */
081  public BaseUnit(String symbol, Dimension dimension) {
082    this.symbol = symbol;
083    this.dimension = dimension;
084  }
085
086  /**
087   * Creates a base unit having the specified symbol and dimension.
088   *
089   * @param symbol
090   *          the symbol of this base unit.
091   */
092  public BaseUnit(String symbol) {
093    this.symbol = symbol;
094    this.dimension = QuantityDimension.NONE;
095  }
096
097  /**
098   * Creates a base unit having the specified symbol and name.
099   *
100   * @param symbol
101   *          the symbol of this base unit.
102   * @param name
103   *          the name of this base unit.
104   * @throws IllegalArgumentException
105   *           if the specified symbol is associated to a different unit.
106   */
107  public BaseUnit(String symbol, String name) {
108    this(symbol);
109    this.name = name;
110  }
111
112  @Override
113  public String getSymbol() {
114    return symbol;
115  }
116
117  @Override
118  public AbstractUnit<Q> toSystemUnit() {
119    return this;
120  }
121
122  @Override
123  public UnitConverter getSystemConverter() throws UnsupportedOperationException {
124    return AbstractConverter.IDENTITY;
125  }
126
127  @Override
128  public Dimension getDimension() {
129    return dimension;
130  }
131
132  @Override
133  public final boolean equals(Object that) {
134    if (this == that)
135      return true;
136    if (!(that instanceof BaseUnit))
137      return false;
138    BaseUnit<?> thatUnit = (BaseUnit<?>) that;
139    return this.symbol.equals(thatUnit.symbol) && this.dimension.equals(thatUnit.dimension);
140  }
141
142  @Override
143  public final int hashCode() {
144    return symbol.hashCode();
145  }
146
147  @Override
148  public Map<? extends AbstractUnit<Q>, Integer> getBaseUnits() {
149    // TODO Shall we return null, empty list or what (e.g. Optional in SE
150    // 8)?
151    return null;
152  }
153}