Skip to content

Commit fe4ccca

Browse files
committed
Added Parenthesis in MathBuilder
1 parent 0b66931 commit fe4ccca

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

src/main/java/com/thealgorithms/maths/MathBuilder.java

+63-21
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ public double get() {
2525
// Return result in long
2626
public long toLong() {
2727
try {
28-
if (Double.isNaN(result)) throw new IllegalArgumentException("Cannot convert NaN to long!");
29-
if (result == Double.POSITIVE_INFINITY) return Long.MAX_VALUE;
30-
if (result == Double.NEGATIVE_INFINITY) return Long.MIN_VALUE;
31-
if (result > Long.MAX_VALUE) return Long.MAX_VALUE;
32-
if (result < Long.MIN_VALUE) return Long.MIN_VALUE;
28+
if (Double.isNaN(result)) {
29+
throw new IllegalArgumentException("Cannot convert NaN to long!");
30+
}
31+
if (result == Double.POSITIVE_INFINITY) {
32+
return Long.MAX_VALUE;
33+
}
34+
if (result == Double.NEGATIVE_INFINITY) {
35+
return Long.MIN_VALUE;
36+
}
37+
if (result > Long.MAX_VALUE) {
38+
return Long.MAX_VALUE;
39+
}
40+
if (result < Long.MIN_VALUE) {
41+
return Long.MIN_VALUE;
42+
}
3343
return Math.round(result);
3444
} catch (Exception ex) {
3545
return 0;
@@ -61,7 +71,9 @@ public Builder add(double num) {
6171

6272
// Takes a number and a condition, only does the operation if condition is true.
6373
public Builder addIf(double num, BiFunction<Double, Double, Boolean> condition) {
64-
if (!condition.apply(number, num)) return this;
74+
if (!condition.apply(number, num)) {
75+
return this;
76+
}
6577
if (inParenthesis) {
6678
sideNumber += num;
6779
} else {
@@ -81,7 +93,9 @@ public Builder minus(double num) {
8193

8294
// Takes a number and a condition, only does the operation if condition is true.
8395
public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition) {
84-
if (!condition.apply(number, num)) return this;
96+
if (!condition.apply(number, num)) {
97+
return this;
98+
}
8599
if (inParenthesis) {
86100
sideNumber -= num;
87101
} else {
@@ -92,28 +106,36 @@ public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition
92106

93107
// Generates a random number and sets to NUMBER
94108
public Builder rand(long seed) {
95-
if (number != 0) throw new RuntimeException("Number must be zero for random assignment!");
109+
if (number != 0) {
110+
throw new RuntimeException("Number must be zero for random assignment!");
111+
}
96112
Random random = new Random();
97113
number = random.nextDouble(seed);
98114
return this;
99115
}
100116

101117
// Takes PI value and sets to NUMBER
102118
public Builder pi() {
103-
if (number != 0) throw new RuntimeException("Number must be zero for PI assignment!");
119+
if (number != 0) {
120+
throw new RuntimeException("Number must be zero for PI assignment!");
121+
}
104122
number = Math.PI;
105123
return this;
106124
}
107125

108126
// Takes E value and sets to NUMBER
109127
public Builder e() {
110-
if (number != 0) throw new RuntimeException("Number must be zero for E assignment!");
128+
if (number != 0) {
129+
throw new RuntimeException("Number must be zero for E assignment!");
130+
}
111131
number = Math.E;
112132
return this;
113133
}
114134

115135
public Builder randomInRange(double min, double max) {
116-
if (number != 0) throw new RuntimeException("Number must be zero for random assignment!");
136+
if (number != 0) {
137+
throw new RuntimeException("Number must be zero for random assignment!");
138+
}
117139
Random random = new Random();
118140
number = min + (max - min) * random.nextDouble();
119141
return this;
@@ -157,7 +179,9 @@ public Builder multiply(double num) {
157179

158180
// Takes a number and a condition, only does the operation if condition is true.
159181
public Builder multiplyIf(double num, BiFunction<Double, Double, Boolean> condition) {
160-
if (!condition.apply(number, num)) return this;
182+
if (!condition.apply(number, num)) {
183+
return this;
184+
}
161185
if (inParenthesis) {
162186
sideNumber *= num;
163187
} else {
@@ -167,7 +191,9 @@ public Builder multiplyIf(double num, BiFunction<Double, Double, Boolean> condit
167191
}
168192

169193
public Builder divide(double num) {
170-
if (num == 0) return this;
194+
if (num == 0) {
195+
return this;
196+
}
171197
if (inParenthesis) {
172198
sideNumber /= num;
173199
} else {
@@ -178,8 +204,12 @@ public Builder divide(double num) {
178204

179205
// Takes a number and a condition, only does the operation if condition is true.
180206
public Builder divideIf(double num, BiFunction<Double, Double, Boolean> condition) {
181-
if (num == 0) return this;
182-
if (!condition.apply(number, num)) return this;
207+
if (num == 0) {
208+
return this;
209+
}
210+
if (!condition.apply(number, num)) {
211+
return this;
212+
}
183213
if (inParenthesis) {
184214
sideNumber /= num;
185215
} else {
@@ -199,7 +229,9 @@ public Builder mod(double num) {
199229

200230
// Takes a number and a condition, only does the operation if condition is true.
201231
public Builder modIf(double num, BiFunction<Double, Double, Boolean> condition) {
202-
if (!condition.apply(number, num)) return this;
232+
if (!condition.apply(number, num)) {
233+
return this;
234+
}
203235
if (inParenthesis) {
204236
sideNumber %= num;
205237
} else {
@@ -378,23 +410,33 @@ public Builder recall(boolean cleanMemory) {
378410

379411
// Recalls the NUMBER on condition
380412
public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory) {
381-
if (!condition.apply(number)) return this;
413+
if (!condition.apply(number)) {
414+
return this;
415+
}
382416
number = memory;
383-
if (cleanMemory) memory = 0;
417+
if (cleanMemory) {
418+
memory = 0;
419+
}
384420
return this;
385421
}
386422

387423
// Replaces NUMBER with given number
388424
public Builder set(double num) {
389-
if (number != 0) throw new RuntimeException("Number must be zero to set!");
425+
if (number != 0) {
426+
throw new RuntimeException("Number must be zero to set!");
427+
}
390428
number = num;
391429
return this;
392430
}
393431

394432
// Replaces NUMBER with given number on condition
395433
public Builder setIf(double num, BiFunction<Double, Double, Boolean> condition) {
396-
if (number != 0) throw new RuntimeException("Number must be zero to set!");
397-
if (condition.apply(number, num)) number = num;
434+
if (number != 0) {
435+
throw new RuntimeException("Number must be zero to set!");
436+
}
437+
if (condition.apply(number, num)) {
438+
number = num;
439+
}
398440
return this;
399441
}
400442

0 commit comments

Comments
 (0)