1 /*
2 * Copyright (C) 2020 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.units;
17
18 import java.math.BigDecimal;
19
20 /**
21 * Contains a temperature value and unit.
22 */
23 public class Temperature extends Measurement<TemperatureUnit> {
24
25 /**
26 * Constructor with value and unit.
27 *
28 * @param value temperature value.
29 * @param unit unit of temperature.
30 * @throws IllegalArgumentException if either value or unit is null.
31 */
32 public Temperature(final Number value, final TemperatureUnit unit) {
33 super(value, unit);
34 }
35
36 /**
37 * Constructor.
38 */
39 Temperature() {
40 super();
41 }
42
43 /**
44 * Determines if two temperatures are equal up to a certain tolerance.
45 * If needed, this method attempts unit conversion to compare both objects.
46 *
47 * @param other another measurement to compare.
48 * @param tolerance amount of tolerance to determine whether two temperature instances are equal or not.
49 * @return true if provided temperature is assumed to be equal to this instance,
50 * false otherwise.
51 */
52 @Override
53 public boolean equals(final Measurement<TemperatureUnit> other, final double tolerance) {
54 if (super.equals(other, tolerance)) {
55 return true;
56 }
57
58 // attempt conversion to common units
59 if (other == null) {
60 return false;
61 }
62
63 final var otherValue = TemperatureConverter.convert(other.getValue().doubleValue(), other.getUnit(), getUnit());
64 return Math.abs(getValue().doubleValue() - otherValue) <= tolerance;
65 }
66
67 /**
68 * Adds two temperature values and units and returns the result.
69 *
70 * @param value1 1st argument value.
71 * @param unit1 1st argument unit.
72 * @param value2 2nd argument value.
73 * @param unit2 2nd argument unit.
74 * @param resultUnit unit of result to be returned.
75 * @return result of addition.
76 */
77 public static double add(
78 final double value1, final TemperatureUnit unit1,
79 final double value2, final TemperatureUnit unit2,
80 final TemperatureUnit resultUnit) {
81 // 1st convert both values to common unit1
82 final var v2 = TemperatureConverter.convert(value2, unit2, unit1);
83
84 // then add and convert to result unit
85 return TemperatureConverter.convert(value1 + v2, unit1, resultUnit);
86 }
87
88 /**
89 * Adds two temperature values and units and returns the result.
90 *
91 * @param value1 1st argument value.
92 * @param unit1 1st argument unit.
93 * @param value2 2nd argument value.
94 * @param unit2 2nd argument unit.
95 * @param resultUnit unit of result to be returned.
96 * @return result of addition.
97 */
98 public static Number add(
99 final Number value1, final TemperatureUnit unit1,
100 final Number value2, final TemperatureUnit unit2,
101 final TemperatureUnit resultUnit) {
102 return BigDecimal.valueOf(add(value1.doubleValue(), unit1, value2.doubleValue(), unit2, resultUnit));
103 }
104
105 /**
106 * Adds two temperature instances and stores the result into provided instance.
107 *
108 * @param arg1 1st argument.
109 * @param arg2 2nd argument.
110 * @param result instance where result will be stored.
111 */
112 public static void add(final Temperature arg1, final Temperature arg2, final Temperature result) {
113 result.setValue(add(arg1.getValue(), arg1.getUnit(), arg2.getValue(), arg2.getUnit(), result.getUnit()));
114 }
115
116 /**
117 * Adds two temperature instances.
118 *
119 * @param arg1 1st argument.
120 * @param arg2 2nd argument.
121 * @param unit unit of returned temperature.
122 * @return a new instance containing result.
123 */
124 public static Temperature addAndReturnNew(final Temperature arg1, final Temperature arg2, final TemperatureUnit unit) {
125 final var result = new Temperature();
126 result.setUnit(unit);
127 add(arg1, arg2, result);
128 return result;
129 }
130
131 /**
132 * Adds provided temperature value and unit and returns a new temperature
133 * instance using provided unit.
134 *
135 * @param value value to be added.
136 * @param unit unit of value to be added.
137 * @param resultUnit unit of returned temperature.
138 * @return a new temperature containing result.
139 */
140 public Temperature addAndReturnNew(
141 final double value, final TemperatureUnit unit, final TemperatureUnit resultUnit) {
142 final var result = new Temperature();
143 result.setUnit(resultUnit);
144 result.setValue(add(getValue().doubleValue(), getUnit(), value, unit, resultUnit));
145 return result;
146 }
147
148 /**
149 * Adds provided temperature value and unit and returns a new temperature
150 * instance using provided unit.
151 *
152 * @param value value to be added.
153 * @param unit unit of value to be added.
154 * @param resultUnit unit of returned temperature.
155 * @return a new temperature containing result.
156 */
157 public Temperature addAndReturnNew(
158 final Number value, final TemperatureUnit unit, final TemperatureUnit resultUnit) {
159 final var result = new Temperature();
160 result.setUnit(resultUnit);
161 result.setValue(add(getValue(), getUnit(), value, unit, resultUnit));
162 return result;
163 }
164
165 /**
166 * Adds provided temperature to current instance and returns a new temperature
167 * instance.
168 *
169 * @param t temperature to be added.
170 * @param unit unit of returned temperature.
171 * @return a new temperature containing result.
172 */
173 public Temperature addAndReturnNew(final Temperature t, final TemperatureUnit unit) {
174 return addAndReturnNew(this, t, unit);
175 }
176
177 /**
178 * Adds provided temperature value and unit and updates current temperature
179 * instance.
180 *
181 * @param value temperature value to be added.
182 * @param unit unit of temperature value.
183 */
184 public void add(final double value, final TemperatureUnit unit) {
185 setValue(add(getValue(), getUnit(), value, unit, getUnit()));
186 }
187
188 /**
189 * Adds provided temperature value and unit and updates current temperature
190 * instance.
191 *
192 * @param value temperature value to be added.
193 * @param unit unit of temperature value.
194 */
195 public void add(final Number value, final TemperatureUnit unit) {
196 setValue(add(getValue(), getUnit(), value, unit, getUnit()));
197 }
198
199 /**
200 * Adds provided temperature and updates current temperature instance.
201 *
202 * @param temperature temperature to be added.
203 */
204 public void add(final Temperature temperature) {
205 add(this, temperature, this);
206 }
207
208 /**
209 * Adds provided temperature and stores the result into provided temperature
210 * instance.
211 *
212 * @param t temperature to be added.
213 * @param result instance where result will be stored.
214 */
215 public void add(final Temperature t, final Temperature result) {
216 add(this, t, result);
217 }
218
219 /**
220 * Subtracts two temperature values and unit and returns the result.
221 *
222 * @param value1 1st argument value.
223 * @param unit1 1st argument unit.
224 * @param value2 2nd argument value.
225 * @param unit2 2nd argument unit.
226 * @param resultUnit unit of result to be returned.
227 * @return result of subtraction.
228 */
229 public static double subtract(
230 final double value1, final TemperatureUnit unit1,
231 final double value2, final TemperatureUnit unit2,
232 final TemperatureUnit resultUnit) {
233 // 1st convert both values to common uni1
234 final var v2 = TemperatureConverter.convert(value2, unit2, unit1);
235
236 // then subtract and convert to result unit
237 return TemperatureConverter.convert(value1 - v2, unit1, resultUnit);
238 }
239
240 /**
241 * Subtracts two temperature values and units and returns the result.
242 *
243 * @param value1 1st argument value.
244 * @param unit1 1st argument unit.
245 * @param value2 2nd argument value.
246 * @param unit2 2nd argument unit.
247 * @param resultUnit unit of result to be returned.
248 * @return result of subtraction.
249 */
250 public static Number subtract(
251 final Number value1, final TemperatureUnit unit1,
252 final Number value2, final TemperatureUnit unit2,
253 final TemperatureUnit resultUnit) {
254 return BigDecimal.valueOf(subtract(value1.doubleValue(), unit1, value2.doubleValue(), unit2, resultUnit));
255 }
256
257 /**
258 * Subtracts two temperature instances and stores the result into provided
259 * instance.
260 *
261 * @param arg1 1st argument.
262 * @param arg2 2nd argument.
263 * @param result instance where result will be stored.
264 */
265 public static void subtract(final Temperature arg1, final Temperature arg2, final Temperature result) {
266 result.setValue(subtract(arg1.getValue(), arg1.getUnit(), arg2.getValue(), arg2.getUnit(), result.getUnit()));
267 }
268
269 /**
270 * Subtracts two temperature instances.
271 *
272 * @param arg1 1st argument.
273 * @param arg2 2nd argument.
274 * @param unit unit of returned temperature.
275 * @return a new instance containing result.
276 */
277 public static Temperature subtractAndReturnNew(
278 final Temperature arg1, final Temperature arg2, final TemperatureUnit unit) {
279 final var result = new Temperature();
280 result.setUnit(unit);
281 subtract(arg1, arg2, result);
282 return result;
283 }
284
285 /**
286 * Subtracts provided temperature value and unit and returns a new temperature
287 * instance using provided unit.
288 *
289 * @param value value to be subtracted.
290 * @param unit unit of value to be subtracted.
291 * @param resultUnit unit of returned temperature.
292 * @return a new temperature containing result.
293 */
294 public Temperature subtractAndReturnNew(
295 final double value, final TemperatureUnit unit, final TemperatureUnit resultUnit) {
296 final var result = new Temperature();
297 result.setUnit(resultUnit);
298 result.setValue(subtract(getValue().doubleValue(), getUnit(), value, unit, resultUnit));
299 return result;
300 }
301
302 /**
303 * Subtracts provided temperature value and unit and returns a new temperature
304 * instance using provided unit.
305 *
306 * @param value value to be subtracted.
307 * @param unit unit of value to be subtracted.
308 * @param resultUnit unit of returned temperature.
309 * @return a new temperature containing result.
310 */
311 public Temperature subtractAndReturnNew(
312 final Number value, final TemperatureUnit unit, final TemperatureUnit resultUnit) {
313 final var result = new Temperature();
314 result.setUnit(resultUnit);
315 result.setValue(subtract(getValue(), getUnit(), value, unit, resultUnit));
316 return result;
317 }
318
319 /**
320 * Subtracts provided temperature from current instance and returns a new
321 * instance.
322 *
323 * @param t temperature to be subtracted.
324 * @param unit unit of returned temperature.
325 * @return a new temperature containing result.
326 */
327 public Temperature subtractAndReturnNew(final Temperature t, final TemperatureUnit unit) {
328 return subtractAndReturnNew(this, t, unit);
329 }
330
331 /**
332 * Subtracts provided temperature value and unit and updates current
333 * temperature instance.
334 *
335 * @param value temperature value to be subtracted.
336 * @param unit unit of temperature value.
337 */
338 public void subtract(final double value, final TemperatureUnit unit) {
339 setValue(subtract(getValue(), getUnit(), value, unit, getUnit()));
340 }
341
342 /**
343 * Subtracts provided temperature value and unit and updates current
344 * temperature instance.
345 *
346 * @param value temperature value to be subtracted.
347 * @param unit unit of temperature value.
348 */
349 public void subtract(final Number value, final TemperatureUnit unit) {
350 setValue(subtract(getValue(), getUnit(), value, unit, getUnit()));
351 }
352
353 /**
354 * Subtracts provided temperature and updates current temperature.
355 *
356 * @param temperature temperature to be subtracted.
357 */
358 public void subtract(final Temperature temperature) {
359 subtract(this, temperature, this);
360 }
361
362 /**
363 * Subtracts provided temperature and stores the result into provided
364 * temperature.
365 *
366 * @param t temperature to be subtracted.
367 * @param result instance where result will be stored.
368 */
369 public void subtract(final Temperature t, final Temperature result) {
370 subtract(this, t, result);
371 }
372 }