From 0642bd836e6b898398689d4593c38d952ebf6fc5 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 22 May 2025 11:40:23 +0800 Subject: [PATCH 1/3] feat: refactor BranchUtil to BranchUtil In this update, the generic type `BranchUtil` has been refactored to `BranchUtil`. Additionally, the method `thenSupply` on `BranchUtil` instances has been modified to use the non-generic form `thenSupply` to simplify the method's interface. BREAKING CHANGE: The method `handle(Supplier trueSupplier[, Supplier falseSupplier])` has been refactored to ` thenSupply(Supplier trueSupplier[, Supplier falseSupplier])`; the type signature for the `thenSupply` method has changed; ensure to update any instances where it is invoked. --- .../com/onixbyte/devkit/utils/BranchUtil.java | 91 ++++++++----------- 1 file changed, 37 insertions(+), 54 deletions(-) diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java index 354d7b2..8f11a02 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java @@ -17,9 +17,6 @@ package com.onixbyte.devkit.utils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.Objects; import java.util.function.BooleanSupplier; import java.util.function.Supplier; @@ -66,17 +63,19 @@ import java.util.function.Supplier; * The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean * expressions. * - * @param the type of the result to be handled by the methods * @author zihluwang - * @version 1.6.1 + * @version 2.1.3 * @see java.util.function.Supplier * @see java.util.function.BooleanSupplier * @see java.lang.Runnable * @since 1.0.0 */ -public final class BranchUtil { +public final class BranchUtil { - private final static Logger log = LoggerFactory.getLogger(BranchUtil.class); + /** + * The final result of the boolean expression. + */ + private final boolean result; /** * Create a {@code BranchUtil} instance. @@ -92,11 +91,10 @@ public final class BranchUtil { * boolean expressions. * * @param values the boolean expressions to be evaluated - * @param the type of the result to be handled by the methods * @return a {@code BranchUtil} instance representing the result of the logical OR operation */ - public static BranchUtil or(Boolean... values) { - return new BranchUtil<>(BoolUtil.or(values)); + public static BranchUtil or(Boolean... values) { + return new BranchUtil(BoolUtil.or(values)); } /** @@ -104,11 +102,10 @@ public final class BranchUtil { * boolean expressions. * * @param values the boolean expressions to be evaluated - * @param the type of the result to be handled by the methods * @return a {@code BranchUtil} instance representing the result of the logical AND operation */ - public static BranchUtil and(Boolean... values) { - return new BranchUtil<>(BoolUtil.and(values)); + public static BranchUtil and(Boolean... values) { + return new BranchUtil(BoolUtil.and(values)); } /** @@ -116,12 +113,11 @@ public final class BranchUtil { * boolean suppliers. * * @param valueSuppliers the boolean suppliers to be evaluated - * @param the type of the result to be handled by the methods * @return a {@code BranchUtil} instance representing the result of the * logical OR operation */ - public static BranchUtil or(BooleanSupplier... valueSuppliers) { - return new BranchUtil<>(BoolUtil.or(valueSuppliers)); + public static BranchUtil or(BooleanSupplier... valueSuppliers) { + return new BranchUtil(BoolUtil.or(valueSuppliers)); } /** @@ -129,38 +125,38 @@ public final class BranchUtil { * boolean suppliers. * * @param valueSuppliers the boolean suppliers to be evaluated - * @param the type of the result to be handled by the methods * @return a {@code BranchUtil} instance representing the result of the * logical AND operation */ - public static BranchUtil and(BooleanSupplier... valueSuppliers) { - return new BranchUtil<>(BoolUtil.and(valueSuppliers)); + public static BranchUtil and(BooleanSupplier... valueSuppliers) { + return new BranchUtil(BoolUtil.and(valueSuppliers)); } /** * Handles the result of the boolean expressions by executing the appropriate handler based * on the result. *

- * If the result is {@code true}, the {@code ifHandler} is executed. If the result is + * If the result is {@code true}, the {@code trueHandler} is executed. If the result is * {@code false} and an {@code elseHandler} is provided, it is executed. *

* Returns the result of the executed handler. * - * @param ifHandler the handler to be executed if the result is {@code true} - * @param elseHandler the handler to be executed if the result is {@code false} (optional) + * @param the type of the result to be handled by the methods + * @param trueHandler the handler to be executed if the result is {@code true} + * @param falseSupplier the handler to be executed if the result is {@code false} (optional) * @return the result of the executed handler, or {@code null} if no {@code elseHandler} is * provided and the result of the evaluation is {@code false} */ - public T handle(Supplier ifHandler, Supplier elseHandler) { - if (this.result && Objects.nonNull(ifHandler)) { - return ifHandler.get(); + public T thenSupply(Supplier trueHandler, Supplier falseSupplier) { + if (this.result && Objects.nonNull(trueHandler)) { + return trueHandler.get(); } - if (Objects.isNull(elseHandler)) { + if (Objects.isNull(falseSupplier)) { return null; } - return elseHandler.get(); + return falseSupplier.get(); } /** @@ -169,12 +165,13 @@ public final class BranchUtil { *

* Returns the result of the executed handler. * - * @param ifHandler the handler to be executed if the result is {@code true} + * @param the type of the result to be handled by the methods + * @param trueSupplier the handler to be executed if the result is {@code true} * @return the result of the executed handler, or {@code null} if result of evaluation is * {@code false} */ - public T handle(Supplier ifHandler) { - return handle(ifHandler, null); + public T thenSupply(Supplier trueSupplier) { + return thenSupply(trueSupplier, null); } /** @@ -184,44 +181,30 @@ public final class BranchUtil { * If the result is {@code true}, the {@code ifHandler} is executed. If the result is * {@code false} and an {@code elseHandler} is provided, it is executed. * - * @param ifHandler the handler to be executed if the result is {@code true} - * @param elseHandler the handler to be executed if the result is {@code false} (optional) + * @param trueHandler the handler to be executed if the result is {@code true} + * @param falseHandler the handler to be executed if the result is {@code false} (optional) */ - public void handle(Runnable ifHandler, Runnable elseHandler) { - if (this.result && Objects.nonNull(ifHandler)) { - ifHandler.run(); + public void then(Runnable trueHandler, Runnable falseHandler) { + if (this.result && Objects.nonNull(trueHandler)) { + trueHandler.run(); return; } - if (Objects.isNull(elseHandler)) { + if (Objects.isNull(falseHandler)) { return; } - elseHandler.run(); + falseHandler.run(); } /** * Handles the result of the boolean expressions by executing the provided handler if the * result is {@code true}. * - * @param ifHandler the handler to be executed if the result is {@code true} + * @param trueHandler the handler to be executed if the result is {@code true} */ - public void handle(Runnable ifHandler) { - handle(ifHandler, null); - } - - /** - * The final result of the boolean expression. - */ - private final boolean result; - - /** - * Get the boolean result. - * - * @return the result - */ - public boolean getResult() { - return result; + public void then(Runnable trueHandler) { + then(trueHandler, null); } } From 48714e599af9a73ea0f7d5665da2a881d92ad4e2 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 22 May 2025 11:52:51 +0800 Subject: [PATCH 2/3] feat: marked `BranchUtil#getResult` as deprecated and for removal in future releases --- .../com/onixbyte/devkit/utils/BranchUtil.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java index 8f11a02..158ffe6 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java @@ -137,14 +137,14 @@ public final class BranchUtil { * on the result. *

* If the result is {@code true}, the {@code trueHandler} is executed. If the result is - * {@code false} and an {@code elseHandler} is provided, it is executed. + * {@code false} and an {@code falseSupplier} is provided, it is executed. *

* Returns the result of the executed handler. * * @param the type of the result to be handled by the methods * @param trueHandler the handler to be executed if the result is {@code true} * @param falseSupplier the handler to be executed if the result is {@code false} (optional) - * @return the result of the executed handler, or {@code null} if no {@code elseHandler} is + * @return the result of the executed handler, or {@code null} if no {@code falseSupplier} is * provided and the result of the evaluation is {@code false} */ public T thenSupply(Supplier trueHandler, Supplier falseSupplier) { @@ -167,8 +167,7 @@ public final class BranchUtil { * * @param the type of the result to be handled by the methods * @param trueSupplier the handler to be executed if the result is {@code true} - * @return the result of the executed handler, or {@code null} if result of evaluation is - * {@code false} + * @return the result of the executed handler, or {@code null} if result of evaluation is {@code false} */ public T thenSupply(Supplier trueSupplier) { return thenSupply(trueSupplier, null); @@ -207,4 +206,18 @@ public final class BranchUtil { then(trueHandler, null); } + /** + * Get the boolean result. + *

+ * Note: {@link BranchUtil} is not responsible for getting a raw boolean result, consider use + * {@link BoolUtil} to replace. + * + * @see BoolUtil + * @return the result + */ + @Deprecated(forRemoval = true) + public boolean getResult() { + return result; + } + } From 7abb6954d030418ee569a48d5636c6cdc65cb900 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 22 May 2025 11:57:27 +0800 Subject: [PATCH 3/3] docs: optimised docs --- .../com/onixbyte/devkit/utils/BranchUtil.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java index 158ffe6..6f9f136 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java @@ -136,20 +136,20 @@ public final class BranchUtil { * Handles the result of the boolean expressions by executing the appropriate handler based * on the result. *

- * If the result is {@code true}, the {@code trueHandler} is executed. If the result is + * If the result is {@code true}, the {@code trueSupplier} is executed. If the result is * {@code false} and an {@code falseSupplier} is provided, it is executed. *

- * Returns the result of the executed handler. + * Returns the result of the executed supplier. * * @param the type of the result to be handled by the methods - * @param trueHandler the handler to be executed if the result is {@code true} - * @param falseSupplier the handler to be executed if the result is {@code false} (optional) - * @return the result of the executed handler, or {@code null} if no {@code falseSupplier} is + * @param trueSupplier the supplier to be executed if the result is {@code true} + * @param falseSupplier the supplier to be executed if the result is {@code false} (optional) + * @return the result of the executed supplier, or {@code null} if no {@code falseSupplier} is * provided and the result of the evaluation is {@code false} */ - public T thenSupply(Supplier trueHandler, Supplier falseSupplier) { - if (this.result && Objects.nonNull(trueHandler)) { - return trueHandler.get(); + public T thenSupply(Supplier trueSupplier, Supplier falseSupplier) { + if (this.result && Objects.nonNull(trueSupplier)) { + return trueSupplier.get(); } if (Objects.isNull(falseSupplier)) { @@ -166,7 +166,7 @@ public final class BranchUtil { * Returns the result of the executed handler. * * @param the type of the result to be handled by the methods - * @param trueSupplier the handler to be executed if the result is {@code true} + * @param trueSupplier the supplier to be executed if the result is {@code true} * @return the result of the executed handler, or {@code null} if result of evaluation is {@code false} */ public T thenSupply(Supplier trueSupplier) { @@ -180,7 +180,7 @@ public final class BranchUtil { * If the result is {@code true}, the {@code ifHandler} is executed. If the result is * {@code false} and an {@code elseHandler} is provided, it is executed. * - * @param trueHandler the handler to be executed if the result is {@code true} + * @param trueHandler the handler to be executed if the result is {@code true} * @param falseHandler the handler to be executed if the result is {@code false} (optional) */ public void then(Runnable trueHandler, Runnable falseHandler) { @@ -212,8 +212,8 @@ public final class BranchUtil { * Note: {@link BranchUtil} is not responsible for getting a raw boolean result, consider use * {@link BoolUtil} to replace. * - * @see BoolUtil * @return the result + * @see BoolUtil */ @Deprecated(forRemoval = true) public boolean getResult() {