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