feat: rename ChainedCalcUtil to Calculator

This commit is contained in:
2025-09-12 21:52:32 +08:00
parent d67927ba67
commit 06594e1b77
2 changed files with 50 additions and 43 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
# SOFTWARE. # SOFTWARE.
# #
artefactVersion=3.2.0 artefactVersion=3.3.0
projectUrl=https://onixbyte.com/projects/onixbyte-toolbox projectUrl=https://onixbyte.com/projects/onixbyte-toolbox
projectGithubUrl=https://github.com/onixbyte/onixbyte-toolbox projectGithubUrl=https://github.com/onixbyte/onixbyte-toolbox
licenseName=MIT licenseName=MIT
@@ -38,45 +38,45 @@ import java.util.function.Function;
* <b>Usage:</b> * <b>Usage:</b>
* <pre> * <pre>
* // Perform addition: 3 + 4 * // Perform addition: 3 + 4
* BigDecimal result1 = ChainedCalcUtil.startWith(3) * BigDecimal result1 = Calculator.startWith(3)
* .add(4) * .add(4)
* .getValue(); * .getValue();
* *
* // Perform subtraction: 4 - 2 * // Perform subtraction: 4 - 2
* BigDecimal result2 = ChainedCalcUtil.startWith(4) * BigDecimal result2 = Calculator.startWith(4)
* .subtract(2) * .subtract(2)
* .getValue(); * .getValue();
* *
* // Perform multiplication: 3 * 6 * // Perform multiplication: 3 * 6
* BigDecimal result3 = ChainedCalcUtil.startWith(3) * BigDecimal result3 = Calculator.startWith(3)
* .multiply(6) * .multiply(6)
* .getValue(); * .getValue();
* *
* // Perform division: 6 ÷ 2 * // Perform division: 6 ÷ 2
* BigDecimal result4 = ChainedCalcUtil.startWith(6) * BigDecimal result4 = Calculator.startWith(6)
* .divide(2) * .divide(2)
* .getValue(); * .getValue();
* *
* // Perform division with specified scale: 13 ÷ 7 with a scale of 2 * // Perform division with specified scale: 13 ÷ 7 with a scale of 2
* BigDecimal result5 = ChainedCalcUtil.startWith(13) * BigDecimal result5 = Calculator.startWith(13)
* .divideWithScale(7, 2) * .divideWithScale(7, 2)
* .getValue(); * .getValue();
* *
* // Get int, long, or double results * // Get int, long, or double results
* int intResult = ChainedCalcUtil.startWith(3) * int intResult = Calculator.startWith(3)
* .add(4) * .add(4)
* .getInteger(); * .getInteger();
* *
* long longResult = ChainedCalcUtil.startWith(4) * long longResult = Calculator.startWith(4)
* .subtract(2) * .subtract(2)
* .getLong(); * .getLong();
* *
* double doubleResult = ChainedCalcUtil.startWith(6) * double doubleResult = Calculator.startWith(6)
* .divide(2) * .divide(2)
* .getDouble(); * .getDouble();
* *
* // Get BigDecimal result with specified scale * // Get BigDecimal result with specified scale
* BigDecimal result6 = ChainedCalcUtil.startWith(13) * BigDecimal result6 = Calculator.startWith(13)
* .divide(7) * .divide(7)
* .getValue(2); * .getValue(2);
* </pre> * </pre>
@@ -89,18 +89,18 @@ import java.util.function.Function;
* and may have performance implications for extremely large numbers or complex calculations. * and may have performance implications for extremely large numbers or complex calculations.
* *
* @author sunzsh * @author sunzsh
* @version 3.0.0 * @version 3.3.0
* @see BigDecimal * @see BigDecimal
* @since 1.0.0 * @since 1.0.0
*/ */
public final class ChainedCalcUtil { public final class Calculator {
/** /**
* Creates a {@code ChainedCalcUtil} instance with the specified initial value. * Creates a {@code ChainedCalcUtil} instance with the specified initial value.
* *
* @param value the initial value for the calculation * @param value the initial value for the calculation
*/ */
private ChainedCalcUtil(Number value) { private Calculator(Number value) {
this.value = convertBigDecimal(value, null); this.value = convertBigDecimal(value, null);
} }
@@ -110,8 +110,8 @@ public final class ChainedCalcUtil {
* @param value the initial value for the calculation * @param value the initial value for the calculation
* @return a {@code ChainedCalcUtil} instance for performing chained calculations * @return a {@code ChainedCalcUtil} instance for performing chained calculations
*/ */
public static ChainedCalcUtil startWith(Number value) { public static Calculator startWith(Number value) {
return new ChainedCalcUtil(value); return new Calculator(value);
} }
/** /**
@@ -120,7 +120,7 @@ public final class ChainedCalcUtil {
* @param other the value to be added * @param other the value to be added
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil add(Number other) { public Calculator add(Number other) {
return operator(BigDecimal::add, other); return operator(BigDecimal::add, other);
} }
@@ -131,7 +131,7 @@ public final class ChainedCalcUtil {
* @param beforeOperateScale the scale to be applied before the operation * @param beforeOperateScale the scale to be applied before the operation
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil add(Number other, Integer beforeOperateScale) { public Calculator add(Number other, Integer beforeOperateScale) {
return operator(BigDecimal::add, other, beforeOperateScale); return operator(BigDecimal::add, other, beforeOperateScale);
} }
@@ -141,7 +141,7 @@ public final class ChainedCalcUtil {
* @param other the value to be subtracted * @param other the value to be subtracted
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil subtract(Number other) { public Calculator subtract(Number other) {
return operator(BigDecimal::subtract, other); return operator(BigDecimal::subtract, other);
} }
@@ -153,7 +153,7 @@ public final class ChainedCalcUtil {
* @param beforeOperateScale the scale to be applied before the operation * @param beforeOperateScale the scale to be applied before the operation
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil subtract(Number other, Integer beforeOperateScale) { public Calculator subtract(Number other, Integer beforeOperateScale) {
return operator(BigDecimal::subtract, other, beforeOperateScale); return operator(BigDecimal::subtract, other, beforeOperateScale);
} }
@@ -163,7 +163,7 @@ public final class ChainedCalcUtil {
* @param other the value to be multiplied by * @param other the value to be multiplied by
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil multiply(Number other) { public Calculator multiply(Number other) {
return operator(BigDecimal::multiply, other); return operator(BigDecimal::multiply, other);
} }
@@ -175,7 +175,7 @@ public final class ChainedCalcUtil {
* @param beforeOperateScale the scale to be applied before the operation * @param beforeOperateScale the scale to be applied before the operation
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil multiply(Number other, Integer beforeOperateScale) { public Calculator multiply(Number other, Integer beforeOperateScale) {
return operator(BigDecimal::multiply, other, beforeOperateScale); return operator(BigDecimal::multiply, other, beforeOperateScale);
} }
@@ -185,7 +185,7 @@ public final class ChainedCalcUtil {
* @param other the value to divide by * @param other the value to divide by
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil divide(Number other) { public Calculator divide(Number other) {
return operator(BigDecimal::divide, other); return operator(BigDecimal::divide, other);
} }
@@ -196,7 +196,7 @@ public final class ChainedCalcUtil {
* @param beforeOperateScale the scale to be applied before the operation * @param beforeOperateScale the scale to be applied before the operation
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil divide(Number other, Integer beforeOperateScale) { public Calculator divide(Number other, Integer beforeOperateScale) {
return operator(BigDecimal::divide, other, beforeOperateScale); return operator(BigDecimal::divide, other, beforeOperateScale);
} }
@@ -207,9 +207,9 @@ public final class ChainedCalcUtil {
* @param scale the scale for the result * @param scale the scale for the result
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil divideWithScale(Number other, Integer scale) { public Calculator divideWithScale(Number other, Integer scale) {
return baseOperator(otherValue -> return baseOperator(otherValue ->
this.value.divide(otherValue, scale, RoundingMode.HALF_UP), other, null); this.value.divide(otherValue, scale, RoundingMode.HALF_UP), other, null);
} }
/** /**
@@ -221,10 +221,10 @@ public final class ChainedCalcUtil {
* @param beforeOperateScale the scale to be applied before the operation * @param beforeOperateScale the scale to be applied before the operation
* @return a {@code ChainedCalcUtil} instance with the updated value * @return a {@code ChainedCalcUtil} instance with the updated value
*/ */
public ChainedCalcUtil divideWithScale(Number other, Integer scale, Integer beforeOperateScale) { public Calculator divideWithScale(Number other, Integer scale, Integer beforeOperateScale) {
return baseOperator((otherValue) -> return baseOperator((otherValue) ->
this.value.divide(otherValue, scale, RoundingMode.HALF_UP), this.value.divide(otherValue, scale, RoundingMode.HALF_UP),
other, beforeOperateScale); other, beforeOperateScale);
} }
/** /**
@@ -290,7 +290,10 @@ public final class ChainedCalcUtil {
* @param otherValue the value to apply the operator with * @param otherValue the value to apply the operator with
* @return a ChainedCalcUtil instance with the updated value * @return a ChainedCalcUtil instance with the updated value
*/ */
private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, Object otherValue) { private Calculator operator(
BiFunction<BigDecimal, BigDecimal, BigDecimal> operator,
Object otherValue
) {
return operator(operator, otherValue, 9); return operator(operator, otherValue, 9);
} }
@@ -304,13 +307,16 @@ public final class ChainedCalcUtil {
* or null if not applicable * or null if not applicable
* @return a ChainedCalcUtil instance with the updated value * @return a ChainedCalcUtil instance with the updated value
*/ */
private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, private Calculator operator(
Object other, BiFunction<BigDecimal, BigDecimal, BigDecimal> operator,
Integer beforeOperateScale) { Object other,
return baseOperator((otherValue) -> Integer beforeOperateScale
operator.apply(this.value, otherValue), ) {
other, return baseOperator(
beforeOperateScale); (otherValue) -> operator.apply(this.value, otherValue),
other,
beforeOperateScale
);
} }
/** /**
@@ -323,14 +329,16 @@ public final class ChainedCalcUtil {
* or null if not applicable * or null if not applicable
* @return a ChainedCalcUtil instance with the updated value * @return a ChainedCalcUtil instance with the updated value
*/ */
private synchronized ChainedCalcUtil baseOperator(Function<BigDecimal, BigDecimal> operatorFunction, private synchronized Calculator baseOperator(
Object anotherValue, Function<BigDecimal, BigDecimal> operatorFunction,
Integer beforeOperateScale) { Object anotherValue,
Integer beforeOperateScale
) {
if (Objects.isNull(anotherValue)) { if (Objects.isNull(anotherValue)) {
return this; return this;
} }
if (anotherValue instanceof ChainedCalcUtil) { if (anotherValue instanceof Calculator) {
this.value = operatorFunction.apply(((ChainedCalcUtil) anotherValue).getValue()); this.value = operatorFunction.apply(((Calculator) anotherValue).getValue());
return this; return this;
} }
this.value = operatorFunction.apply(convertBigDecimal(anotherValue, beforeOperateScale)); this.value = operatorFunction.apply(convertBigDecimal(anotherValue, beforeOperateScale));
@@ -366,8 +374,7 @@ public final class ChainedCalcUtil {
} }
/** /**
* -- GETTER -- * Final result.
* Returns the current value as a BigDecimal.
*/ */
private BigDecimal value; private BigDecimal value;