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