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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class Temperature extends AbstractDoubleScalarRel<TemperatureUnit, Temperature>
36 {
37
38 private static final long serialVersionUID = 20150901L;
39
40
41 public static final Temperature ZERO = new Temperature(0.0, TemperatureUnit.SI);
42
43
44 @SuppressWarnings("checkstyle:constantname")
45 public static final Temperature NaN = new Temperature(Double.NaN, TemperatureUnit.SI);
46
47
48 public static final Temperature POSITIVE_INFINITY = new Temperature(Double.POSITIVE_INFINITY, TemperatureUnit.SI);
49
50
51 public static final Temperature NEGATIVE_INFINITY = new Temperature(Double.NEGATIVE_INFINITY, TemperatureUnit.SI);
52
53
54 public static final Temperature POS_MAXVALUE = new Temperature(Double.MAX_VALUE, TemperatureUnit.SI);
55
56
57 public static final Temperature NEG_MAXVALUE = new Temperature(-Double.MAX_VALUE, TemperatureUnit.SI);
58
59
60
61
62
63
64 public Temperature(final double value, final TemperatureUnit unit)
65 {
66 super(value, unit);
67 }
68
69
70
71
72
73 public Temperature(final Temperature value)
74 {
75 super(value);
76 }
77
78
79 @Override
80 public final Temperature instantiateRel(final double value, final TemperatureUnit unit)
81 {
82 return new Temperature(value, unit);
83 }
84
85
86
87
88
89
90
91 public final AbsoluteTemperature instantiateAbs(final double value, final AbsoluteTemperatureUnit unit)
92 {
93 return new AbsoluteTemperature(value, unit);
94 }
95
96
97
98
99
100
101 public static final Temperature createSI(final double value)
102 {
103 return new Temperature(value, TemperatureUnit.SI);
104 }
105
106
107
108
109
110
111
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
120
121
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
131
132
133
134
135 public static Temperature max(final Temperature r1, final Temperature r2)
136 {
137 return (r1.gt(r2)) ? r1 : r2;
138 }
139
140
141
142
143
144
145
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
162
163
164
165
166 public static Temperature min(final Temperature r1, final Temperature r2)
167 {
168 return (r1.lt(r2)) ? r1 : r2;
169 }
170
171
172
173
174
175
176
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
193
194
195
196
197
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
232
233
234
235 public final Dimensionless divideBy(final Temperature v)
236 {
237 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
238 }
239
240 }