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