1 /*
2 * Copyright (C) 2018 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 time value and unit.
22 */
23 public class Time extends Measurement<TimeUnit> {
24
25 /**
26 * Constructor with value and unit.
27 *
28 * @param value time value.
29 * @param unit unit of distance.
30 * @throws IllegalArgumentException if either value or unit is null.
31 */
32 @SuppressWarnings("WeakerAccess")
33 public Time(final Number value, final TimeUnit unit) {
34 super(value, unit);
35 }
36
37 /**
38 * Constructor.
39 */
40 Time() {
41 super();
42 }
43
44 /**
45 * Determines if two time instances are equal up to a certain tolerance.
46 * If needed, this method attempts unit conversion to compare both objects.
47 *
48 * @param other another time to compare.
49 * @param tolerance amount of tolerance to determine whether two time instances are equal or not.
50 * @return true if provided time is assumed to be equal to this instance, false
51 * otherwise.
52 */
53 @Override
54 public boolean equals(final Measurement<TimeUnit> other, final double tolerance) {
55 if (super.equals(other, tolerance)) {
56 return true;
57 }
58
59 //attempt conversion to common units
60 if (other == null) {
61 return false;
62 }
63
64 final var otherValue = TimeConverter.convert(other.getValue().doubleValue(), other.getUnit(), getUnit());
65 return Math.abs(getValue().doubleValue() - otherValue) <= tolerance;
66 }
67
68 /**
69 * Adds two time values and units and returns the result.
70 *
71 * @param value1 1st argument value.
72 * @param unit1 1st argument unit.
73 * @param value2 2nd argument value.
74 * @param unit2 2nd argument unit.
75 * @param resultUnit unit of result to be returned.
76 * @return result of addition.
77 */
78 public static double add(final double value1, final TimeUnit unit1,
79 final double value2, final TimeUnit unit2,
80 final TimeUnit resultUnit) {
81 final var v1 = TimeConverter.convert(value1, unit1, resultUnit);
82 final var v2 = TimeConverter.convert(value2, unit2, resultUnit);
83 return v1 + v2;
84 }
85
86 /**
87 * Adds two time values and units and returns the result.
88 *
89 * @param value1 1st argument value.
90 * @param unit1 1st argument unit.
91 * @param value2 2nd argument value.
92 * @param unit2 2nd argument unit.
93 * @param resultUnit unit of result to be returned.
94 * @return result of addition.
95 */
96 public static Number add(final Number value1, final TimeUnit unit1,
97 final Number value2, final TimeUnit unit2,
98 final TimeUnit resultUnit) {
99 return BigDecimal.valueOf(add(value1.doubleValue(), unit1, value2.doubleValue(), unit2, resultUnit));
100 }
101
102 /**
103 * Adds two time instances and stores the result into provided instance.
104 *
105 * @param arg1 1st argument.
106 * @param arg2 2nd argument.
107 * @param result instance where result will be stored.
108 */
109 public static void add(final Time arg1, final Time arg2, final Time result) {
110 result.setValue(add(arg1.getValue(), arg1.getUnit(), arg2.getValue(), arg2.getUnit(), result.getUnit()));
111 }
112
113 /**
114 * Adds two time instances.
115 *
116 * @param arg1 1st argument.
117 * @param arg2 2nd argument.
118 * @param unit unit of returned time.
119 * @return a new instance containing result.
120 */
121 public static Time addAndReturnNew(final Time arg1, final Time arg2, final TimeUnit unit) {
122 final var result = new Time();
123 result.setUnit(unit);
124 add(arg1, arg2, result);
125 return result;
126 }
127
128 /**
129 * Adds provided time value and unit and returns a new time instance using
130 * provided unit.
131 *
132 * @param value value to be added.
133 * @param unit unit of value to be added.
134 * @param resultUnit unit of returned time.
135 * @return a new time containing result.
136 */
137 public Time addAndReturnNew(
138 final double value, final TimeUnit unit,
139 final TimeUnit resultUnit) {
140 final var result = new Time();
141 result.setUnit(resultUnit);
142 result.setValue(add(getValue().doubleValue(), getUnit(), value, unit, resultUnit));
143 return result;
144 }
145
146 /**
147 * Adds provided time value and unit and returns a new time instance using
148 * provided unit.
149 *
150 * @param value value to be added.
151 * @param unit unit of value to be added.
152 * @param resultUnit unit of returned time.
153 * @return a new time containing result.
154 */
155 public Time addAndReturnNew(
156 final Number value, final TimeUnit unit,
157 final TimeUnit resultUnit) {
158 final var result = new Time();
159 result.setUnit(resultUnit);
160 result.setValue(add(getValue(), getUnit(), value, unit, resultUnit));
161 return result;
162 }
163
164 /**
165 * Adds provided time to current instance and returns a new time
166 * instance.
167 *
168 * @param t time to be added.
169 * @param unit unit of returned time.
170 * @return a new time containing result.
171 */
172 public Time addAndReturnNew(final Time t, final TimeUnit unit) {
173 return addAndReturnNew(this, t, unit);
174 }
175
176 /**
177 * Adds provided time value and unit and updates current time instance.
178 *
179 * @param value time value to be added.
180 * @param unit unit of time value.
181 */
182 public void add(final double value, final TimeUnit unit) {
183 setValue(add(getValue(), getUnit(), value, unit, getUnit()));
184 }
185
186 /**
187 * Adds provided time value and unit and updates current time instance.
188 *
189 * @param value time value to be added.
190 * @param unit unit of time value.
191 */
192 public void add(final Number value, final TimeUnit unit) {
193 setValue(add(getValue(), getUnit(), value, unit, getUnit()));
194 }
195
196 /**
197 * Adds provided time and updates current time instance.
198 *
199 * @param time time to be added.
200 */
201 public void add(final Time time) {
202 add(this, time, this);
203 }
204
205 /**
206 * Adds provided time and stores the result into provided time instance.
207 *
208 * @param t time to be added.
209 * @param result instance where result will be stored.
210 */
211 public void add(final Time t, final Time result) {
212 add(this, t, result);
213 }
214
215 /**
216 * Subtracts two time values and units and returns the result.
217 *
218 * @param value1 1st argument value.
219 * @param unit1 1st argument unit.
220 * @param value2 2nd argument value.
221 * @param unit2 2nd argument unit.
222 * @param resultUnit unit of result to be returned.
223 * @return result of subtraction.
224 */
225 public static double subtract(
226 final double value1, final TimeUnit unit1,
227 final double value2, final TimeUnit unit2,
228 final TimeUnit resultUnit) {
229 final var v1 = TimeConverter.convert(value1, unit1, resultUnit);
230 final var v2 = TimeConverter.convert(value2, unit2, resultUnit);
231 return v1 - v2;
232 }
233
234 /**
235 * Subtracts two time values and units and returns the result.
236 *
237 * @param value1 1st argument value.
238 * @param unit1 1st argument unit.
239 * @param value2 2nd argument value.
240 * @param unit2 2nd argument unit.
241 * @param resultUnit unit of result to be returned.
242 * @return result of subtraction.
243 */
244 public static Number subtract(
245 final Number value1, final TimeUnit unit1,
246 final Number value2, final TimeUnit unit2,
247 final TimeUnit resultUnit) {
248 return BigDecimal.valueOf(subtract(value1.doubleValue(), unit1, value2.doubleValue(), unit2, resultUnit));
249 }
250
251 /**
252 * Subtracts two time instances and stores the result into provided instance.
253 *
254 * @param arg1 1st argument.
255 * @param arg2 2nd argument.
256 * @param result instance where result will be stored.
257 */
258 public static void subtract(final Time arg1, final Time arg2, final Time result) {
259 result.setValue(subtract(arg1.getValue(), arg1.getUnit(), arg2.getValue(), arg2.getUnit(), result.getUnit()));
260 }
261
262 /**
263 * Subtracts two time instances.
264 *
265 * @param arg1 1st argument.
266 * @param arg2 2nd argument.
267 * @param unit unit of returned time.
268 * @return a new instance containing result.
269 */
270 public static Time subtractAndReturnNew(
271 final Time arg1, final Time arg2, final TimeUnit unit) {
272 final var result = new Time();
273 result.setUnit(unit);
274 subtract(arg1, arg2, result);
275 return result;
276 }
277
278 /**
279 * Subtracts provided time value and unit and returns a new time instance using
280 * provided unit.
281 *
282 * @param value value to be subtracted.
283 * @param unit unit of value to be subtracted.
284 * @param resultUnit unit of returned time.
285 * @return a new time containing result.
286 */
287 public Time subtractAndReturnNew(final double value, final TimeUnit unit, final TimeUnit resultUnit) {
288 final var result = new Time();
289 result.setUnit(resultUnit);
290 result.setValue(subtract(getValue().doubleValue(), getUnit(), value, unit, resultUnit));
291 return result;
292 }
293
294 /**
295 * Subtracts provided time value and unit and returns a new time instance using
296 * provided unit.
297 *
298 * @param value value to be subtracted.
299 * @param unit unit of value to be subtracted.
300 * @param resultUnit unit of returned time.
301 * @return a new time containing result.
302 */
303 public Time subtractAndReturnNew(final Number value, final TimeUnit unit, final TimeUnit resultUnit) {
304 final var result = new Time();
305 result.setUnit(resultUnit);
306 result.setValue(subtract(getValue(), getUnit(), value, unit, resultUnit));
307 return result;
308 }
309
310 /**
311 * Subtracts provided time to current instance and returns a new
312 * instance.
313 *
314 * @param t time to be subtracted.
315 * @param unit unit of returned time.
316 * @return a new time containing result.
317 */
318 public Time subtractAndReturnNew(final Time t, final TimeUnit unit) {
319 return subtractAndReturnNew(this, t, unit);
320 }
321
322 /**
323 * Subtracts provided time value and unit and updates current time instance.
324 *
325 * @param value time value to be subtracted.
326 * @param unit unit of time value.
327 */
328 public void subtract(final double value, final TimeUnit unit) {
329 setValue(subtract(getValue(), getUnit(), value, unit, getUnit()));
330 }
331
332 /**
333 * Subtracts provided time value and unit and updates current time instance.
334 *
335 * @param value time value to be subtracted.
336 * @param unit unit of time value.
337 */
338 public void subtract(final Number value, final TimeUnit unit) {
339 setValue(subtract(getValue(), getUnit(), value, unit, getUnit()));
340 }
341
342 /**
343 * Subtract provided time and updates current time.
344 *
345 * @param time time to be subtracted.
346 */
347 public void subtract(final Time time) {
348 subtract(this, time, this);
349 }
350
351 /**
352 * Subtracts provided time and stores the result into provided time.
353 *
354 * @param t time to be subtracted.
355 * @param result instance where result will be stored.
356 */
357 public void subtract(final Time t, final Time result) {
358 subtract(this, t, result);
359 }
360 }