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