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