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