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