1212 */
1313public final class MathBuilder {
1414 private final double result ;
15-
1615 private MathBuilder (Builder builder ) {
1716 this .result = builder .number ;
1817 }
19-
2018 // Returns final result
2119 public double get () {
2220 return result ;
2321 }
24-
2522 // Return result in long
2623 public long toLong () {
2724 try {
@@ -49,27 +46,22 @@ public long toLong() {
4946 public static class Builder {
5047 private double number ;
5148 private double memory = 0 ;
52-
5349 public Builder () {
5450 number = 0 ;
5551 }
56-
5752 public Builder (double num ) {
5853 number = num ;
5954 }
60-
6155 public Builder add (double num ) {
6256 number += num ;
6357 return this ;
6458 }
6559
6660 // Takes a number and a condition, only does the operation if condition is true.
6761 public Builder addIf (double num , BiFunction <Double , Double , Boolean > condition ) {
68-
6962 if (condition .apply (number , num )) {
7063 number += num ;
7164 }
72-
7365 return this ;
7466 }
7567
@@ -83,7 +75,6 @@ public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition
8375 if (condition .apply (number , num )) {
8476 number -= num ;
8577 }
86-
8778 return this ;
8879 }
8980
@@ -92,7 +83,6 @@ public Builder rand(long seed) {
9283 if (number != 0 ) {
9384 throw new RuntimeException ("Number must be zero for random assignment!" );
9485 }
95-
9686 Random random = new Random ();
9787 number = random .nextDouble (seed );
9888 return this ;
@@ -103,7 +93,6 @@ public Builder pi() {
10393 if (number != 0 ) {
10494 throw new RuntimeException ("Number must be zero for PI assignment!" );
10595 }
106-
10796 number = Math .PI ;
10897 return this ;
10998 }
@@ -113,7 +102,6 @@ public Builder e() {
113102 if (number != 0 ) {
114103 throw new RuntimeException ("Number must be zero for E assignment!" );
115104 }
116-
117105 number = Math .E ;
118106 return this ;
119107 }
@@ -123,7 +111,6 @@ public Builder randomInRange(double min, double max) {
123111 if (number != 0 ) {
124112 throw new RuntimeException ("Number must be zero for random assignment!" );
125113 }
126-
127114 Random random = new Random ();
128115 number = min + (max - min ) * random .nextDouble ();
129116 return this ;
@@ -145,173 +132,143 @@ public Builder min(double num) {
145132 }
146133
147134 public Builder multiply (double num ) {
148-
149135 number *= num ;
150136 return this ;
151137 }
152138
153139 // Takes a number and a condition, only does the operation if condition is true.
154140 public Builder multiplyIf (double num , BiFunction <Double , Double , Boolean > condition ) {
155-
156-
157141 if (condition .apply (number , num )) {
158142 number *= num ;
159143 }
160-
161144 return this ;
162145 }
163146
164147 public Builder divide (double num ) {
165-
166148 if (num == 0 ) {
167149 return this ;
168150 }
169-
170151 number /= num ;
171152 return this ;
172153 }
173154
174155 // Takes a number and a condition, only does the operation if condition is true.
175156 public Builder divideIf (double num , BiFunction <Double , Double , Boolean > condition ) {
176-
177157 if (num == 0 ) {
178158 return this ;
179159 }
180-
181160 if (condition .apply (number , num )) {
182161 number /= num ;
183162 }
184163 return this ;
185164 }
186165
187166 public Builder mod (double num ) {
188-
189167 number %= num ;
190168 return this ;
191169 }
192170
193171 // Takes a number and a condition, only does the operation if condition is true.
194172 public Builder modIf (double num , BiFunction <Double , Double , Boolean > condition ) {
195-
196-
197173 if (condition .apply (number , num )) {
198174 number %= num ;
199175 }
200176 return this ;
201177 }
202178
203179 public Builder pow (double num ) {
204-
205180 number = Math .pow (number , num );
206181 return this ;
207182 }
208183
209184 public Builder sqrt () {
210-
211185 number = Math .sqrt (number );
212186 return this ;
213187 }
214188
215189 public Builder round () {
216-
217190 number = Math .round (number );
218191 return this ;
219192 }
220193
221194 public Builder floor () {
222-
223195 number = Math .floor (number );
224196 return this ;
225197 }
226198
227199 public Builder ceil () {
228-
229200 number = Math .ceil (number );
230201 return this ;
231202 }
232203
233204 public Builder abs () {
234-
235205 number = Math .abs (number );
236206 return this ;
237207 }
238208
239209 public Builder cbrt () {
240-
241210 number = Math .cbrt (number );
242211 return this ;
243212 }
244213
245214 public Builder log () {
246-
247215 number = Math .log (number );
248216 return this ;
249217 }
250218
251219 public Builder log10 () {
252-
253220 number = Math .log10 (number );
254221 return this ;
255222 }
256223
257224 public Builder sin () {
258-
259225 number = Math .sin (number );
260226 return this ;
261227 }
262228
263229 public Builder cos () {
264-
265230 number = Math .cos (number );
266231 return this ;
267232 }
268233
269234 public Builder tan () {
270-
271235 number = Math .tan (number );
272236 return this ;
273237 }
274238
275239 public Builder sinh () {
276-
277240 number = Math .sinh (number );
278241 return this ;
279242 }
280243
281244 public Builder cosh () {
282-
283245 number = Math .cosh (number );
284246 return this ;
285247 }
286248
287249 public Builder tanh () {
288-
289250 number = Math .tanh (number );
290251 return this ;
291252 }
292253
293254 public Builder exp () {
294-
295255 number = Math .exp (number );
296256 return this ;
297257 }
298258
299259 public Builder toRadians () {
300-
301260 number = Math .toRadians (number );
302261 return this ;
303262 }
304263
305264 // Remembers the NUMBER
306265 public Builder remember () {
307-
308266 memory = number ;
309267 return this ;
310268 }
311269
312270 // Recalls the NUMBER
313271 public Builder recall (boolean cleanMemory ) {
314-
315272 number = memory ;
316273 if (cleanMemory ) {
317274 memory = 0 ;
@@ -322,11 +279,9 @@ public Builder recall(boolean cleanMemory) {
322279
323280 // Recalls the NUMBER on condition
324281 public Builder recallIf (Function <Double , Boolean > condition , boolean cleanMemory ) {
325-
326282 if (!condition .apply (number )) {
327283 return this ;
328284 }
329-
330285 number = memory ;
331286 if (cleanMemory ) {
332287 memory = 0 ;
@@ -337,26 +292,21 @@ public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory
337292
338293 // Replaces NUMBER with given number
339294 public Builder set (double num ) {
340-
341295 if (number != 0 ) {
342296 throw new RuntimeException ("Number must be zero to set!" );
343297 }
344-
345298 number = num ;
346299 return this ;
347300 }
348301
349302 // Replaces NUMBER with given number on condition
350303 public Builder setIf (double num , BiFunction <Double , Double , Boolean > condition ) {
351-
352304 if (number != 0 ) {
353305 throw new RuntimeException ("Number must be zero to set!" );
354306 }
355-
356307 if (condition .apply (number , num )) {
357308 number = num ;
358309 }
359-
360310 return this ;
361311 }
362312
@@ -367,15 +317,13 @@ public Builder print() {
367317 }
368318
369319 public Builder format (String format ) {
370-
371320 DecimalFormat formater = new DecimalFormat (format );
372321 String num = formater .format (number );
373322 number = Double .parseDouble (num );
374323 return this ;
375324 }
376325
377326 public Builder format (int decimalPlace ) {
378-
379327 String pattern = "." + "#" .repeat (decimalPlace );
380328 DecimalFormat formater = new DecimalFormat (pattern );
381329 String num = formater .format (number );
0 commit comments