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