12
12
*/
13
13
public final class MathBuilder {
14
14
private final double result ;
15
-
16
15
private MathBuilder (Builder builder ) {
17
16
this .result = builder .number ;
18
17
}
19
-
20
18
// Returns final result
21
19
public double get () {
22
20
return result ;
23
21
}
24
-
25
22
// Return result in long
26
23
public long toLong () {
27
24
try {
@@ -49,27 +46,22 @@ public long toLong() {
49
46
public static class Builder {
50
47
private double number ;
51
48
private double memory = 0 ;
52
-
53
49
public Builder () {
54
50
number = 0 ;
55
51
}
56
-
57
52
public Builder (double num ) {
58
53
number = num ;
59
54
}
60
-
61
55
public Builder add (double num ) {
62
56
number += num ;
63
57
return this ;
64
58
}
65
59
66
60
// Takes a number and a condition, only does the operation if condition is true.
67
61
public Builder addIf (double num , BiFunction <Double , Double , Boolean > condition ) {
68
-
69
62
if (condition .apply (number , num )) {
70
63
number += num ;
71
64
}
72
-
73
65
return this ;
74
66
}
75
67
@@ -83,7 +75,6 @@ public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition
83
75
if (condition .apply (number , num )) {
84
76
number -= num ;
85
77
}
86
-
87
78
return this ;
88
79
}
89
80
@@ -92,7 +83,6 @@ public Builder rand(long seed) {
92
83
if (number != 0 ) {
93
84
throw new RuntimeException ("Number must be zero for random assignment!" );
94
85
}
95
-
96
86
Random random = new Random ();
97
87
number = random .nextDouble (seed );
98
88
return this ;
@@ -103,7 +93,6 @@ public Builder pi() {
103
93
if (number != 0 ) {
104
94
throw new RuntimeException ("Number must be zero for PI assignment!" );
105
95
}
106
-
107
96
number = Math .PI ;
108
97
return this ;
109
98
}
@@ -113,7 +102,6 @@ public Builder e() {
113
102
if (number != 0 ) {
114
103
throw new RuntimeException ("Number must be zero for E assignment!" );
115
104
}
116
-
117
105
number = Math .E ;
118
106
return this ;
119
107
}
@@ -123,7 +111,6 @@ public Builder randomInRange(double min, double max) {
123
111
if (number != 0 ) {
124
112
throw new RuntimeException ("Number must be zero for random assignment!" );
125
113
}
126
-
127
114
Random random = new Random ();
128
115
number = min + (max - min ) * random .nextDouble ();
129
116
return this ;
@@ -145,173 +132,143 @@ public Builder min(double num) {
145
132
}
146
133
147
134
public Builder multiply (double num ) {
148
-
149
135
number *= num ;
150
136
return this ;
151
137
}
152
138
153
139
// Takes a number and a condition, only does the operation if condition is true.
154
140
public Builder multiplyIf (double num , BiFunction <Double , Double , Boolean > condition ) {
155
-
156
-
157
141
if (condition .apply (number , num )) {
158
142
number *= num ;
159
143
}
160
-
161
144
return this ;
162
145
}
163
146
164
147
public Builder divide (double num ) {
165
-
166
148
if (num == 0 ) {
167
149
return this ;
168
150
}
169
-
170
151
number /= num ;
171
152
return this ;
172
153
}
173
154
174
155
// Takes a number and a condition, only does the operation if condition is true.
175
156
public Builder divideIf (double num , BiFunction <Double , Double , Boolean > condition ) {
176
-
177
157
if (num == 0 ) {
178
158
return this ;
179
159
}
180
-
181
160
if (condition .apply (number , num )) {
182
161
number /= num ;
183
162
}
184
163
return this ;
185
164
}
186
165
187
166
public Builder mod (double num ) {
188
-
189
167
number %= num ;
190
168
return this ;
191
169
}
192
170
193
171
// Takes a number and a condition, only does the operation if condition is true.
194
172
public Builder modIf (double num , BiFunction <Double , Double , Boolean > condition ) {
195
-
196
-
197
173
if (condition .apply (number , num )) {
198
174
number %= num ;
199
175
}
200
176
return this ;
201
177
}
202
178
203
179
public Builder pow (double num ) {
204
-
205
180
number = Math .pow (number , num );
206
181
return this ;
207
182
}
208
183
209
184
public Builder sqrt () {
210
-
211
185
number = Math .sqrt (number );
212
186
return this ;
213
187
}
214
188
215
189
public Builder round () {
216
-
217
190
number = Math .round (number );
218
191
return this ;
219
192
}
220
193
221
194
public Builder floor () {
222
-
223
195
number = Math .floor (number );
224
196
return this ;
225
197
}
226
198
227
199
public Builder ceil () {
228
-
229
200
number = Math .ceil (number );
230
201
return this ;
231
202
}
232
203
233
204
public Builder abs () {
234
-
235
205
number = Math .abs (number );
236
206
return this ;
237
207
}
238
208
239
209
public Builder cbrt () {
240
-
241
210
number = Math .cbrt (number );
242
211
return this ;
243
212
}
244
213
245
214
public Builder log () {
246
-
247
215
number = Math .log (number );
248
216
return this ;
249
217
}
250
218
251
219
public Builder log10 () {
252
-
253
220
number = Math .log10 (number );
254
221
return this ;
255
222
}
256
223
257
224
public Builder sin () {
258
-
259
225
number = Math .sin (number );
260
226
return this ;
261
227
}
262
228
263
229
public Builder cos () {
264
-
265
230
number = Math .cos (number );
266
231
return this ;
267
232
}
268
233
269
234
public Builder tan () {
270
-
271
235
number = Math .tan (number );
272
236
return this ;
273
237
}
274
238
275
239
public Builder sinh () {
276
-
277
240
number = Math .sinh (number );
278
241
return this ;
279
242
}
280
243
281
244
public Builder cosh () {
282
-
283
245
number = Math .cosh (number );
284
246
return this ;
285
247
}
286
248
287
249
public Builder tanh () {
288
-
289
250
number = Math .tanh (number );
290
251
return this ;
291
252
}
292
253
293
254
public Builder exp () {
294
-
295
255
number = Math .exp (number );
296
256
return this ;
297
257
}
298
258
299
259
public Builder toRadians () {
300
-
301
260
number = Math .toRadians (number );
302
261
return this ;
303
262
}
304
263
305
264
// Remembers the NUMBER
306
265
public Builder remember () {
307
-
308
266
memory = number ;
309
267
return this ;
310
268
}
311
269
312
270
// Recalls the NUMBER
313
271
public Builder recall (boolean cleanMemory ) {
314
-
315
272
number = memory ;
316
273
if (cleanMemory ) {
317
274
memory = 0 ;
@@ -322,11 +279,9 @@ public Builder recall(boolean cleanMemory) {
322
279
323
280
// Recalls the NUMBER on condition
324
281
public Builder recallIf (Function <Double , Boolean > condition , boolean cleanMemory ) {
325
-
326
282
if (!condition .apply (number )) {
327
283
return this ;
328
284
}
329
-
330
285
number = memory ;
331
286
if (cleanMemory ) {
332
287
memory = 0 ;
@@ -337,26 +292,21 @@ public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory
337
292
338
293
// Replaces NUMBER with given number
339
294
public Builder set (double num ) {
340
-
341
295
if (number != 0 ) {
342
296
throw new RuntimeException ("Number must be zero to set!" );
343
297
}
344
-
345
298
number = num ;
346
299
return this ;
347
300
}
348
301
349
302
// Replaces NUMBER with given number on condition
350
303
public Builder setIf (double num , BiFunction <Double , Double , Boolean > condition ) {
351
-
352
304
if (number != 0 ) {
353
305
throw new RuntimeException ("Number must be zero to set!" );
354
306
}
355
-
356
307
if (condition .apply (number , num )) {
357
308
number = num ;
358
309
}
359
-
360
310
return this ;
361
311
}
362
312
@@ -367,15 +317,13 @@ public Builder print() {
367
317
}
368
318
369
319
public Builder format (String format ) {
370
-
371
320
DecimalFormat formater = new DecimalFormat (format );
372
321
String num = formater .format (number );
373
322
number = Double .parseDouble (num );
374
323
return this ;
375
324
}
376
325
377
326
public Builder format (int decimalPlace ) {
378
-
379
327
String pattern = "." + "#" .repeat (decimalPlace );
380
328
DecimalFormat formater = new DecimalFormat (pattern );
381
329
String num = formater .format (number );
0 commit comments