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