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.quantity; 031 032import static javax.measure.Quantity.Scale.*; 033 034import javax.measure.Quantity; 035import javax.measure.UnconvertibleException; 036import javax.measure.Unit; 037import javax.measure.UnitConverter; 038import javax.measure.quantity.Temperature; 039 040import tech.uom.impl.enums.AbstractQuantity; 041import tech.uom.impl.enums.format.UnitStyle; 042import tech.uom.impl.enums.unit.TemperatureUnit; 043 044/** 045 * @author Werner Keil 046 * @version 1.2, $Date: 2018-12-18 $ 047 */ 048public final class TemperatureQuantity extends AbstractQuantity<Temperature> 049 implements Temperature { 050 private final Double scalar; // value in reference unit 051 052 private final double value; // value in unit (Unit unit) 053 054 private final TemperatureUnit unit; 055 056 public TemperatureQuantity(double val, TemperatureUnit un) { 057 super((TemperatureUnit.KELVIN.equals(un) ? 058 ABSOLUTE : RELATIVE)); 059 unit = un; 060 value = val; 061 if (un != null) { 062 scalar = val * un.getFactor(); 063 } 064 else scalar = null; 065 } 066 067 public TemperatureQuantity(Number val, @SuppressWarnings("rawtypes") Unit u) { 068 this(val.doubleValue(), (TemperatureUnit)u); 069 } 070 071 @Override 072 public boolean isZero() { 073 return 0d==(value); 074 } 075 076 public TemperatureQuantity add(TemperatureQuantity d1) { 077 final TemperatureQuantity dn = new TemperatureQuantity(Double.valueOf( 078 this.value + d1.value), 079 this.unit); 080 return dn; 081 } 082 083 public TemperatureQuantity subtract(TemperatureQuantity d1) { 084 final TemperatureQuantity dn = new TemperatureQuantity( 085 this.value- d1.value, this.unit); 086 return dn; 087 } 088 089 protected boolean eq(TemperatureQuantity dq) { 090 return dq!=null && dq.getValue().equals(getValue()) && 091 dq.getUnit().equals(getUnit()) && 092 dq.getScalar().equals(getScalar()); 093 } 094 095 boolean ne(TemperatureQuantity d1) { 096 return ne((TemperatureQuantity) d1); 097 } 098 099 boolean gt(TemperatureQuantity d1) { 100 return gt((TemperatureQuantity) d1); 101 } 102 103 public boolean lt(TemperatureQuantity d1) { 104 return lt((TemperatureQuantity) d1); 105 } 106 107 public boolean ge(TemperatureQuantity d1) { 108 return ge((TemperatureQuantity)d1); 109 } 110 111 public boolean le(TemperatureQuantity d1) { 112 return le((TemperatureQuantity) d1); 113 } 114 115 public TemperatureQuantity divide(double v) { 116 return new TemperatureQuantity(value / v, 117 unit); 118 } 119 120 protected TemperatureQuantity convert(TemperatureUnit newUnit) { 121 return new TemperatureQuantity(value / 122 newUnit.getFactor(), newUnit); 123 } 124 125 @Override 126 public Double getScalar() { 127 return scalar; 128 } 129 130 @Override 131 public String toString(boolean withUnit, boolean withSpace, int precision) { 132 final StringBuilder sb = new StringBuilder(); 133 sb.append(getValue()); 134 if(withUnit) { 135 if(withSpace) sb.append(" "); 136 sb.append(getUnit().getSymbol()); 137 } 138 return sb.toString(); 139 } 140 141 @Override 142 public String showInUnit(Unit<?> u, int precision, 143 UnitStyle style) { 144 return showInUnit(u, value, precision, style); 145 } 146 147 public Number getValue() { 148 return value; 149 } 150 151 public Unit<Temperature> getUnit() { 152 return unit; 153 } 154 155 public Quantity<Temperature> multiply(Number that) { 156 return new TemperatureQuantity(value * that.doubleValue(), unit); 157 } 158 159 public Quantity<?> multiply(Quantity<?> that) { 160 return new TemperatureQuantity(value * that.getValue().doubleValue(), unit); 161 } 162 163 public Quantity<Temperature> inverse() { 164 // TODO Auto-generated method stub 165 return null; 166 } 167 168 /* 169 * (non-Javadoc) 170 * 171 * @see 172 * Measurement#doubleValue(javax.measure.Unit) 173 */ 174 protected double doubleValue(Unit<Temperature> unit) { 175 Unit<Temperature> myUnit = getUnit(); 176 try { 177 UnitConverter converter = unit.getConverterTo(myUnit); 178 return converter.convert(getValue().doubleValue()); 179 } catch (UnconvertibleException e) { 180 throw e; 181 } // catch (IncommensurableException e) { 182 // throw new IllegalArgumentException(e.getMessage()); 183 // } 184 } 185 186 public Quantity<Temperature> to(Unit<Temperature> unit) { 187 if (this.unit.equals(unit)) { 188 return this; 189 } 190 if (unit instanceof TemperatureUnit) { 191// final TemperatureUnit asTU = (TemperatureUnit)unit; 192// for (TemperatureUnit tu : TemperatureUnit.values()) { 193// if (asTU.equals(tu)) { 194// return new TemperatureQuantity( asTU) 195// } 196// } 197 return convert((TemperatureUnit)unit); 198 } else { 199 throw new ArithmeticException("Cannot convert " + this.unit + " to " + unit); 200 } 201 } 202 203 protected boolean eq(AbstractQuantity<Temperature> dq) { 204 return eq((TemperatureQuantity) dq); 205 } 206 207 public Quantity<?> divide(Quantity<?> that) { 208 // TODO Auto-generated method stub 209 return null; 210 } 211 212 public Quantity<Temperature> subtract(Quantity<Temperature> that) { 213 // TODO Auto-generated method stub 214 return null; 215 } 216 217 public Quantity<Temperature> add(Quantity<Temperature> that) { 218 // TODO Auto-generated method stub 219 return null; 220 } 221 222 public Quantity<Temperature> divide(Number that) { 223 // TODO Auto-generated method stub 224 return null; 225 } 226 227 public int compareTo(Quantity<Temperature> o) { 228 // TODO Auto-generated method stub 229 return 0; 230 } 231 232 @Override 233 public Quantity<Temperature> negate() { 234 return new TemperatureQuantity(-value, unit); 235 } 236}