001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
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-385, Indriya nor the names of their 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 tech.units.indriya.unit;
031
032import javax.measure.Dimension;
033import javax.measure.Quantity;
034import javax.measure.Unit;
035import javax.measure.UnitConverter;
036
037import tech.units.indriya.AbstractUnit;
038import tech.units.indriya.function.AbstractConverter;
039import tech.units.indriya.unit.UnitDimension;
040
041import java.util.Map;
042import java.util.Objects;
043
044/**
045 * <p>
046 * This class represents the building blocks on top of which all others physical
047 * units are created. Base units are always unscaled SI units.
048 * </p>
049 * 
050 * <p>
051 * When using the {@link tech.units.indriya.spi.StandardModel standard model},
052 * all seven <b>SI</b> base units are dimensionally independent.
053 * </p>
054 *
055 * @see <a href="http://en.wikipedia.org/wiki/SI_base_unit"> Wikipedia: SI base
056 *      unit</a>
057 *
058 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
059 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
060 * @version 2.0, February 7, 2020
061 * @since 1.0
062 */
063public final class BaseUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
064
065        /**
066         * 
067         */
068        private static final long serialVersionUID = 1721629233768215930L;
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 the symbol of this base unit.
079         */
080        public BaseUnit(String symbol, Dimension dimension) {
081                super(symbol);
082                this.dimension = dimension;
083        }
084
085        /**
086         * Creates a base unit having the specified symbol.
087         *
088         * @param symbol the symbol of this base unit.
089         */
090        public BaseUnit(String symbol) {
091                super(symbol);
092                this.dimension = UnitDimension.NONE;
093        }
094
095        /**
096         * Creates a base unit having the specified symbol and name.
097         *
098         * @param symbol the symbol of this base unit.
099         * @param name   the name of this base unit.
100         * @throws IllegalArgumentException if the specified symbol is associated to a
101         *                                  different unit.
102         */
103        public BaseUnit(String symbol, String name) {
104                this(symbol);
105                this.name = name;
106        }
107
108        /**
109         * Creates a base unit having the specified symbol, name and dimension.
110         *
111         * @param symbol the symbol of this base unit.
112         * @param name   the name of this base unit.
113         * @throws IllegalArgumentException if the specified symbol is associated to a
114         *                                  different unit.
115         * @since 2.0
116         */
117        public BaseUnit(String symbol, String name, Dimension dimension) {
118                super(symbol);
119                this.name = name;
120                this.dimension = dimension;
121        }
122
123        @Override
124        public Unit<Q> toSystemUnit() {
125                return this;
126        }
127
128        @Override
129        public UnitConverter getSystemConverter() throws UnsupportedOperationException {
130                return AbstractConverter.IDENTITY;
131        }
132
133        @Override
134        public Dimension getDimension() {
135                return dimension;
136        }
137
138        @Override
139        public int hashCode() {
140                final int prime = 31;
141                int result = 1;
142                result = prime * result + ((getSymbol() == null) ? 0 : getSymbol().hashCode());
143                // result = result + prime * result + ((name == null) ? 0 : name.hashCode());
144                result = result + prime * result + ((dimension == null) ? 0 : dimension.hashCode());
145                return result;
146        }
147
148        @Override
149        public boolean equals(Object obj) {
150                if (this == obj)
151                        return true;
152                if (obj == null)
153                        return false;
154                if (getClass() != obj.getClass())
155                        return false;
156                @SuppressWarnings("rawtypes")
157                BaseUnit other = (BaseUnit) obj;
158                return Objects.equals(dimension, other.dimension) && Objects.equals(getSymbol(), other.getSymbol());
159        }
160
161        @Override
162        public Map<? extends AbstractUnit<Q>, Integer> getBaseUnits() {
163                // TODO Shall we return null, empty list or what (e.g. Optional from Java 8)?
164                return null;
165        }
166}