1 package org.djunits.value.vdouble.scalar;
2
3 import java.util.regex.Matcher;
4
5 import org.djunits.unit.AbsoluteTemperatureUnit;
6 import org.djunits.unit.DimensionlessUnit;
7 import org.djunits.unit.TemperatureUnit;
8 import org.djunits.unit.Unit;
9
10 /**
11 * Easy access methods for the Relative Temperature DoubleScalar. Instead of:
12 *
13 * <pre>
14 * DoubleScalar<TemperatureUnit> value = new DoubleScalar<TemperatureUnit>(100.0, TemperatureUnit.SI);
15 * </pre>
16 *
17 * we can now write:
18 *
19 * <pre>
20 * Temperature value = new Temperature(100.0, TemperatureUnit.SI);
21 * </pre>
22 *
23 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
24 * used are compatible.
25 * <p>
26 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
27 * All rights reserved. <br>
28 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29 * <p>
30 * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
31 * initial version Sep 1, 2015 <br>
32 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34 */
35 public class Temperature extends AbstractDoubleScalarRel<TemperatureUnit, Temperature>
36 {
37 /** */
38 private static final long serialVersionUID = 20150901L;
39
40 /** constant with value zero. */
41 public static final Temperature ZERO = new Temperature(0.0, TemperatureUnit.SI);
42
43 /** constant with value NaN. */
44 @SuppressWarnings("checkstyle:constantname")
45 public static final Temperature NaN = new Temperature(Double.NaN, TemperatureUnit.SI);
46
47 /** constant with value POSITIVE_INFINITY. */
48 public static final Temperature POSITIVE_INFINITY = new Temperature(Double.POSITIVE_INFINITY, TemperatureUnit.SI);
49
50 /** constant with value NEGATIVE_INFINITY. */
51 public static final Temperature NEGATIVE_INFINITY = new Temperature(Double.NEGATIVE_INFINITY, TemperatureUnit.SI);
52
53 /** constant with value MAX_VALUE. */
54 public static final Temperature POS_MAXVALUE = new Temperature(Double.MAX_VALUE, TemperatureUnit.SI);
55
56 /** constant with value -MAX_VALUE. */
57 public static final Temperature NEG_MAXVALUE = new Temperature(-Double.MAX_VALUE, TemperatureUnit.SI);
58
59 /**
60 * Construct Temperature scalar.
61 * @param value double value
62 * @param unit unit for the double value
63 */
64 public Temperature(final double value, final TemperatureUnit unit)
65 {
66 super(value, unit);
67 }
68
69 /**
70 * Construct Temperature scalar.
71 * @param value Scalar from which to construct this instance
72 */
73 public Temperature(final Temperature value)
74 {
75 super(value);
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public final Temperature instantiateRel(final double value, final TemperatureUnit unit)
81 {
82 return new Temperature(value, unit);
83 }
84
85 /**
86 * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method.
87 * @param value the double value
88 * @param unit the unit
89 * @return A a new absolute instance of the DoubleScalar of the right type
90 */
91 public final AbsoluteTemperature instantiateAbs(final double value, final AbsoluteTemperatureUnit unit)
92 {
93 return new AbsoluteTemperature(value, unit);
94 }
95
96 /**
97 * Construct Temperature scalar.
98 * @param value double value in SI units
99 * @return the new scalar with the SI value
100 */
101 public static final Temperature createSI(final double value)
102 {
103 return new Temperature(value, TemperatureUnit.SI);
104 }
105
106 /**
107 * Interpolate between two values.
108 * @param zero the low value
109 * @param one the high value
110 * @param ratio the ratio between 0 and 1, inclusive
111 * @return a Scalar at the ratio between
112 */
113 public static Temperature interpolate(final Temperature zero, final Temperature one, final double ratio)
114 {
115 return new Temperature(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
116 }
117
118 /**
119 * Relative scalar plus Absolute scalar = Absolute scalar.
120 * @param v the value to add
121 * @return sum of this value and v as a new object
122 */
123 public final AbsoluteTemperature plus(final AbsoluteTemperature v)
124 {
125 AbsoluteTemperatureUnit targetUnit = v.getUnit();
126 return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
127 }
128
129 /**
130 * Return the maximum value of two relative scalars.
131 * @param r1 the first scalar
132 * @param r2 the second scalar
133 * @return the maximum value of two relative scalars
134 */
135 public static Temperature max(final Temperature r1, final Temperature r2)
136 {
137 return (r1.gt(r2)) ? r1 : r2;
138 }
139
140 /**
141 * Return the maximum value of more than two relative scalars.
142 * @param r1 the first scalar
143 * @param r2 the second scalar
144 * @param rn the other scalars
145 * @return the maximum value of more than two relative scalars
146 */
147 public static Temperature max(final Temperature r1, final Temperature r2, final Temperature... rn)
148 {
149 Temperature maxr = (r1.gt(r2)) ? r1 : r2;
150 for (Temperature r : rn)
151 {
152 if (r.gt(maxr))
153 {
154 maxr = r;
155 }
156 }
157 return maxr;
158 }
159
160 /**
161 * Return the minimum value of two relative scalars.
162 * @param r1 the first scalar
163 * @param r2 the second scalar
164 * @return the minimum value of two relative scalars
165 */
166 public static Temperature min(final Temperature r1, final Temperature r2)
167 {
168 return (r1.lt(r2)) ? r1 : r2;
169 }
170
171 /**
172 * Return the minimum value of more than two relative scalars.
173 * @param r1 the first scalar
174 * @param r2 the second scalar
175 * @param rn the other scalars
176 * @return the minimum value of more than two relative scalars
177 */
178 public static Temperature min(final Temperature r1, final Temperature r2, final Temperature... rn)
179 {
180 Temperature minr = (r1.lt(r2)) ? r1 : r2;
181 for (Temperature r : rn)
182 {
183 if (r.lt(minr))
184 {
185 minr = r;
186 }
187 }
188 return minr;
189 }
190
191 /**
192 * Returns a Temperature representation of a textual representation of a value with a unit. The String representation that
193 * can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but
194 * not necessary, between the value and the unit.
195 * @param text String; the textual representation to parse into a Temperature
196 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
197 * @throws IllegalArgumentException when the text cannot be parsed
198 */
199 public static Temperature valueOf(final String text) throws IllegalArgumentException
200 {
201 if (text == null || text.length() == 0)
202 {
203 throw new IllegalArgumentException("Error parsing Temperature -- null or empty argument");
204 }
205 Matcher matcher = NUMBER_PATTERN.matcher(text);
206 if (matcher.find())
207 {
208 int index = matcher.end();
209 try
210 {
211 String unitString = text.substring(index).trim();
212 String valueString = text.substring(0, index).trim();
213 for (TemperatureUnit unit : Unit.getUnits(TemperatureUnit.class))
214 {
215 if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
216 {
217 double d = Double.parseDouble(valueString);
218 return new Temperature(d, unit);
219 }
220 }
221 }
222 catch (Exception exception)
223 {
224 throw new IllegalArgumentException("Error parsing Temperature from " + text, exception);
225 }
226 }
227 throw new IllegalArgumentException("Error parsing Temperature from " + text);
228 }
229
230 /**
231 * Calculate the division of Temperature and Temperature, which results in a Dimensionless scalar.
232 * @param v Temperature scalar
233 * @return Dimensionless scalar as a division of Temperature and Temperature
234 */
235 public final Dimensionless divideBy(final Temperature v)
236 {
237 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
238 }
239
240 }