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