001/*
002 * Units of Measurement Enum Implementation
003 * Copyright © 2005-2021, Werner Keil and others.
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, Unit-API 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 */
030/**
031 *
032 */
033package tech.uom.impl.enums.unit;
034
035import tech.uom.lib.common.function.DoubleFactorSupplier;
036
037import java.util.HashMap;
038import java.util.Map;
039
040import javax.measure.Dimension;
041import javax.measure.IncommensurableException;
042import javax.measure.Prefix;
043import javax.measure.Quantity;
044import javax.measure.UnconvertibleException;
045import javax.measure.Unit;
046import javax.measure.UnitConverter;
047import javax.measure.quantity.Dimensionless;
048
049/**
050 * @author Werner Keil
051 * @version 1.4, $Date: 2019-02-04 $
052 */
053public enum DimensionlessUnit implements Unit<Dimensionless>, DoubleFactorSupplier {
054        ONE("", 1.0); // reference Unit
055
056        private final String description;
057        private final double multFactor;
058
059        private DimensionlessUnit(String name, double multF) {
060                this.description = name;
061                this.multFactor = multF;
062        }
063
064        public String getSymbol() {
065                return description;
066        }
067
068        public double getFactor() {
069                return multFactor;
070        }
071
072        public Unit<Dimensionless> getSystemUnit() {
073                return ONE;
074        }
075
076        public Map<? extends Unit<?>, Integer> getBaseUnits() {
077                Map<Unit<Dimensionless>, Integer> prodUnits = new HashMap<Unit<Dimensionless>, Integer>();
078                prodUnits.put(ONE, Integer.valueOf(1));
079                return prodUnits;
080        }
081
082        public static DimensionlessUnit getByName(String symbol) {
083                return ONE;
084        }
085
086        public UnitConverter getConverterTo(Unit<Dimensionless> that) throws UnconvertibleException {
087                // currently unused
088                return null;
089        }
090
091        public UnitConverter getConverterToAny(Unit<?> that) throws IncommensurableException, UnconvertibleException {
092                // currently unused
093                return null;
094        }
095
096        public Unit<Dimensionless> alternate(String s) {
097                return null; // To change body of implemented methods use File |
098                // Settings | File TemplateBuilder.
099        }
100
101        public String getName() {
102                return name();
103        }
104
105        public Dimension getDimension() {
106                return SimpleDimension.INSTANCE;
107        }
108
109        public Unit<?> inverse() {
110                return this;
111        }
112
113        public Unit<Dimensionless> divide(double v) {
114                return null; // To change body of implemented methods use File |
115                // Settings | File TemplateBuilder.
116        }
117
118        public Unit<?> divide(Unit<?> unit) {
119                return null; // To change body of implemented methods use File |
120                // Settings | File TemplateBuilder.
121        }
122
123        public boolean isCompatible(Unit<?> that) {
124                if (that instanceof DimensionlessUnit)
125                        return true;
126                return false;
127        }
128
129        @SuppressWarnings("unchecked")
130        public <T extends Quantity<T>> Unit<T> asType(Class<T> tClass) {
131                Unit<T> metricUnit = (Unit<T>) ONE;
132                if ((metricUnit == null) || metricUnit.isCompatible(this))
133                        return (Unit<T>) this;
134                throw new ClassCastException("The unit: " + this //$NON-NLS-1$
135                                + " is not of parameterized type " + tClass); //$NON-NLS-1$
136        }
137
138        public Unit<Dimensionless> multiply(double factor) {
139                return this;
140        }
141
142        public Unit<?> multiply(Unit<?> that) {
143                return this;
144        }
145
146        public Unit<?> pow(int n) {
147                return this;
148        }
149
150        public Unit<?> root(int n) {
151                return this;
152        }
153
154        public Unit<Dimensionless> transform(UnitConverter operation) {
155                return this;
156        }
157
158        public Unit<Dimensionless> shift(double v) {
159                return this;
160        }
161
162        @Override
163        public Unit<Dimensionless> prefix(Prefix prefix) {
164                return this.multiply(Math.pow(prefix.getValue().doubleValue(), prefix.getExponent()));
165        }
166
167        public Unit<Dimensionless> shift(Number v) {
168                return this;
169        }
170
171        @Override
172        public Unit<Dimensionless> multiply(Number multiplier) {
173                return this;
174        }
175
176        @Override
177        public Unit<Dimensionless> divide(Number divisor) {
178                return this;
179        }
180
181        @Override
182        public boolean isEquivalentTo(Unit<Dimensionless> that) {
183                return equals(that);
184        }
185}