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