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