@@ -75,71 +75,21 @@ public void negate() {
75
75
}
76
76
77
77
public void add (Units units ) {
78
- if (units .isZero ()) {
79
- return ;
80
- }
81
- if (evaluateToNaNIfTrue (units , u -> isNaN () || u .isNaN ())) {
82
- return ;
83
- }
84
- if (evaluateToNegInfIfTrue (units ,
85
- u -> isNegInfinity () && !u .isPosInfinity () || !isPosInfinity () && u .isNegInfinity ())) {
86
- return ;
87
- }
88
- if (evaluateToPosInfIfTrue (units ,
89
- u -> isPosInfinity () && !u .isNegInfinity () || !isNegInfinity () && u .isPosInfinity ())) {
90
- return ;
91
- }
92
- if (evaluateToZeroIfTrue (units , u -> isInfinity () && u .isInfinity ())) {
93
- return ;
94
- }
95
- if (isZero ()) {
96
- value = units .getValue ();
97
- scale = units .getScale ();
98
- return ;
99
- }
100
- if (scale > units .scale ) {
101
- value += rescaleValue (units .value , scale , units .scale );
102
- } else if (scale < units .scale ) {
103
- value = units .value + rescaleValue (value , scale , units .scale );
104
- scale = units .scale ;
105
- } else {
106
- value += units .value ;
78
+ boolean isUnneededAddition = checkIfUnneededAddition (units );
79
+
80
+ if (!isUnneededAddition && !checkAndExecuteZeroAddition (units )) {
81
+ executeAddition (units );
82
+ normalize ();
107
83
}
108
- normalize ();
109
84
}
110
85
111
86
public void subtract (Units units ) {
112
- if (units .isZero ()) {
113
- return ;
114
- }
115
- if (evaluateToNaNIfTrue (units , u -> isNaN () || u .isNaN ())) {
116
- return ;
117
- }
118
- if (evaluateToNegInfIfTrue (units ,
119
- u -> isNegInfinity () && !u .isNegInfinity () || !isPosInfinity () && u .isPosInfinity ())) {
120
- return ;
121
- }
122
- if (evaluateToPosInfIfTrue (units ,
123
- u -> isPosInfinity () && !u .isPosInfinity () || !isNegInfinity () && u .isNegInfinity ())) {
124
- return ;
125
- }
126
- if (evaluateToZeroIfTrue (units , u -> isInfinity () && u .isInfinity ())) {
127
- return ;
128
- }
129
- if (isZero ()) {
130
- value = -units .getValue ();
131
- scale = units .getScale ();
132
- return ;
133
- }
134
- if (scale > units .scale ) {
135
- value -= rescaleValue (units .value , scale , units .scale );
136
- } else if (scale < units .scale ) {
137
- value = units .value + rescaleValue (value , scale , units .scale );
138
- scale = units .scale ;
139
- } else {
140
- value -= units .value ;
87
+ boolean isUnneededSubtraction = checkIfUnneededSubtraction (units );
88
+
89
+ if (!isUnneededSubtraction && !checkAndExecuteZeroSubtraction (units )) {
90
+ executeSubtraction (units );
91
+ normalize ();
141
92
}
142
- normalize ();
143
93
}
144
94
145
95
public void multiply (Units units ) {
@@ -150,8 +100,7 @@ public void multiply(Units units) {
150
100
return ;
151
101
}
152
102
if (evaluateToNegInfIfTrue (units ,
153
- u -> isNegInfinity () && u .isBiggerThan (Zero ) || isPosInfinity () && u .isSmallerThan (Zero )
154
- || isBiggerThan (Zero ) && u .isNegInfinity () || isSmallerThan (Zero ) && u .isPosInfinity ())) {
103
+ u -> checkForMultiplicationNegativeInfinity (u ))) {
155
104
return ;
156
105
}
157
106
if (evaluateToPosInfIfTrue (units , u -> isInfinity () || u .isInfinity ())) {
@@ -413,6 +362,133 @@ private int calcScaleDiff(double rawValueDiff) {
413
362
return (int ) (scaleDiffRaw + (scaleDiffRaw >= 0 ? 0 : -1 ));
414
363
}
415
364
365
+ private boolean checkIfUnneededAddition (Units unit ) {
366
+ boolean isUnneededAddition = false ;
367
+
368
+ isUnneededAddition = unit .isZero ();
369
+
370
+ isUnneededAddition = evaluateToNaNIfTrue (unit , u -> isNaN () || u .isNaN ());
371
+
372
+ isUnneededAddition = evaluateToNegInfIfTrue (unit ,
373
+ u -> checkAdditionUnitsForNegativeInfinity (u ));
374
+
375
+ isUnneededAddition = evaluateToPosInfIfTrue (unit ,
376
+ u -> checkAdditionUnitsForPositiveInfinity (u ));
377
+
378
+ isUnneededAddition = evaluateToZeroIfTrue (unit , u -> isInfinity () && u .isInfinity ());
379
+
380
+ return isUnneededAddition ;
381
+ }
382
+
383
+ private boolean checkAdditionUnitsForPositiveInfinity (Units otherUnit ) {
384
+ boolean isPrimaryUnitPositiveInfinity = isPosInfinity () && !otherUnit .isNegInfinity ();
385
+ boolean isSecondaryUnitPositiveInfinity = !isNegInfinity () && otherUnit .isPosInfinity ();
386
+
387
+ return isPrimaryUnitPositiveInfinity || isSecondaryUnitPositiveInfinity ;
388
+ }
389
+
390
+ private boolean checkAdditionUnitsForNegativeInfinity (Units otherUnit ) {
391
+ boolean isPrimaryUnitNegativeInfinity = isNegInfinity () && !otherUnit .isPosInfinity ();
392
+ boolean isSecondaryUnitNegativeInfinity = !isPosInfinity () && otherUnit .isNegInfinity ();
393
+
394
+ return isPrimaryUnitNegativeInfinity || isSecondaryUnitNegativeInfinity ;
395
+ }
396
+
397
+ private boolean checkIfUnneededSubtraction (Units unit ) {
398
+ boolean isUnneededSubtraction = false ;
399
+
400
+ isUnneededSubtraction = unit .isZero ();
401
+
402
+ boolean finalIsUnneededSubtraction = isUnneededSubtraction ;
403
+ isUnneededSubtraction = evaluateToNaNIfTrue (unit , u -> checkForNaNEvaluation (u , finalIsUnneededSubtraction ));
404
+
405
+ boolean finalIsUnneededSubtraction1 = isUnneededSubtraction ;
406
+ isUnneededSubtraction = evaluateToNegInfIfTrue (unit , u -> checkSubtractionUnitsForNegativeInfinity (u ) && !finalIsUnneededSubtraction1 );
407
+
408
+ boolean finalIsUnneededSubtraction2 = isUnneededSubtraction ;
409
+ isUnneededSubtraction = evaluateToPosInfIfTrue (unit , u -> checkSubtractionUnitsForPositiveInfinity (u ) && !finalIsUnneededSubtraction2 );
410
+
411
+ boolean finalIsUnneededSubtraction3 = isUnneededSubtraction ;
412
+ isUnneededSubtraction = evaluateToZeroIfTrue (unit , u -> checkForInfinityEvaluation (u , finalIsUnneededSubtraction3 ));
413
+
414
+ return isUnneededSubtraction ;
415
+ }
416
+
417
+ private boolean checkForNaNEvaluation (Units unit , boolean enableEvaluation ) {
418
+ return (isNaN () || unit .isNaN ()) && !enableEvaluation ;
419
+ }
420
+
421
+ private boolean checkForInfinityEvaluation (Units unit , boolean enableEvaluation ) {
422
+ return isInfinity () && unit .isInfinity () && !enableEvaluation ;
423
+ }
424
+
425
+ private boolean checkSubtractionUnitsForPositiveInfinity (Units otherUnit ) {
426
+ boolean isPrimaryUnitPositiveInfinity = isPosInfinity () && !otherUnit .isPosInfinity ();
427
+ boolean isSecondaryUnitPositiveInfinity = !isNegInfinity () && otherUnit .isNegInfinity ();
428
+
429
+ return isPrimaryUnitPositiveInfinity || isSecondaryUnitPositiveInfinity ;
430
+ }
431
+
432
+ private boolean checkSubtractionUnitsForNegativeInfinity (Units otherUnit ) {
433
+ boolean isPrimaryUnitNegativeInfinity = isNegInfinity () && !otherUnit .isNegInfinity ();
434
+ boolean isSecondaryUnitNegativeInfinity = !isPosInfinity () && otherUnit .isPosInfinity ();
435
+
436
+ return isPrimaryUnitNegativeInfinity || isSecondaryUnitNegativeInfinity ;
437
+ }
438
+
439
+ private boolean checkAndExecuteZeroAddition (Units addedUnit ) {
440
+ boolean isZero = isZero ();
441
+
442
+ if (isZero ) {
443
+ value = addedUnit .getValue ();
444
+ scale = addedUnit .getScale ();
445
+ }
446
+
447
+ return isZero ;
448
+ }
449
+
450
+ private void executeAddition (Units addedUnit ) {
451
+ if (scale > addedUnit .scale ) {
452
+ value += rescaleValue (addedUnit .value , scale , addedUnit .scale );
453
+ } else if (scale < addedUnit .scale ) {
454
+ value = addedUnit .value + rescaleValue (value , scale , addedUnit .scale );
455
+ scale = addedUnit .scale ;
456
+ } else {
457
+ value += addedUnit .value ;
458
+ }
459
+ }
460
+
461
+ private boolean checkAndExecuteZeroSubtraction (Units subtractedValue ) {
462
+ boolean isZero = isZero ();
463
+
464
+ if (isZero ) {
465
+ value = -subtractedValue .getValue ();
466
+ scale = subtractedValue .getScale ();
467
+ }
468
+
469
+ return isZero ;
470
+ }
471
+
472
+ private void executeSubtraction (Units subtractedUnit ) {
473
+ if (scale > subtractedUnit .scale ) {
474
+ value -= rescaleValue (subtractedUnit .value , scale , subtractedUnit .scale );
475
+ } else if (scale < subtractedUnit .scale ) {
476
+ value = subtractedUnit .value + rescaleValue (value , scale , subtractedUnit .scale );
477
+ scale = subtractedUnit .scale ;
478
+ } else {
479
+ value -= subtractedUnit .value ;
480
+ }
481
+ }
482
+
483
+ private boolean checkForMultiplicationNegativeInfinity (Units unit ) {
484
+ boolean isNegInfTimesPositiveValue = isNegInfinity () && unit .isBiggerThan (Zero );
485
+ boolean isPosInfTimesNegativeValue = isPosInfinity () && unit .isSmallerThan (Zero );
486
+ boolean isPositiveValueTimesNegInf = isBiggerThan (Zero ) && unit .isNegInfinity ();
487
+ boolean isNegativeValueTimesPosInf = isSmallerThan (Zero ) && unit .isPosInfinity ();
488
+
489
+ return isNegInfTimesPositiveValue || isPosInfTimesNegativeValue || isPositiveValueTimesNegInf || isNegativeValueTimesPosInf ;
490
+ }
491
+
416
492
public static Units parse (String string ) throws UnitsFormatException {
417
493
ParseResult <Units > result = ParseResult .failResult ();
418
494
for (char c : string .toCharArray ()) {
0 commit comments