feat: refactor BranchUtil<T> to BranchUtil
In this update, the generic type `BranchUtil<T>` has been refactored to `BranchUtil`. Additionally, the method `thenSupply` on `BranchUtil` instances has been modified to use the non-generic form `<T>thenSupply` to simplify the method's interface. BREAKING CHANGE: The method `handle(Supplier<T> trueSupplier[, Supplier<T> falseSupplier])` has been refactored to `<T> thenSupply(Supplier<T> trueSupplier[, Supplier<T> falseSupplier])`; the type signature for the `thenSupply` method has changed; ensure to update any instances where it is invoked.
This commit is contained in:
@@ -17,9 +17,6 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BooleanSupplier;
|
import java.util.function.BooleanSupplier;
|
||||||
import java.util.function.Supplier;
|
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
|
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean
|
||||||
* expressions.
|
* expressions.
|
||||||
*
|
*
|
||||||
* @param <T> the type of the result to be handled by the methods
|
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.6.1
|
* @version 2.1.3
|
||||||
* @see java.util.function.Supplier
|
* @see java.util.function.Supplier
|
||||||
* @see java.util.function.BooleanSupplier
|
* @see java.util.function.BooleanSupplier
|
||||||
* @see java.lang.Runnable
|
* @see java.lang.Runnable
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public final class BranchUtil<T> {
|
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.
|
* Create a {@code BranchUtil} instance.
|
||||||
@@ -92,11 +91,10 @@ public final class BranchUtil<T> {
|
|||||||
* boolean expressions.
|
* boolean expressions.
|
||||||
*
|
*
|
||||||
* @param values the boolean expressions to be evaluated
|
* @param values the boolean expressions to be evaluated
|
||||||
* @param <T> the type of the result to be handled by the methods
|
|
||||||
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
|
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
|
||||||
*/
|
*/
|
||||||
public static <T> BranchUtil<T> or(Boolean... values) {
|
public static BranchUtil or(Boolean... values) {
|
||||||
return new BranchUtil<>(BoolUtil.or(values));
|
return new BranchUtil(BoolUtil.or(values));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,11 +102,10 @@ public final class BranchUtil<T> {
|
|||||||
* boolean expressions.
|
* boolean expressions.
|
||||||
*
|
*
|
||||||
* @param values the boolean expressions to be evaluated
|
* @param values the boolean expressions to be evaluated
|
||||||
* @param <T> the type of the result to be handled by the methods
|
|
||||||
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
|
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
|
||||||
*/
|
*/
|
||||||
public static <T> BranchUtil<T> and(Boolean... values) {
|
public static BranchUtil and(Boolean... values) {
|
||||||
return new BranchUtil<>(BoolUtil.and(values));
|
return new BranchUtil(BoolUtil.and(values));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,12 +113,11 @@ public final class BranchUtil<T> {
|
|||||||
* boolean suppliers.
|
* boolean suppliers.
|
||||||
*
|
*
|
||||||
* @param valueSuppliers the boolean suppliers to be evaluated
|
* @param valueSuppliers the boolean suppliers to be evaluated
|
||||||
* @param <T> the type of the result to be handled by the methods
|
|
||||||
* @return a {@code BranchUtil} instance representing the result of the
|
* @return a {@code BranchUtil} instance representing the result of the
|
||||||
* logical OR operation
|
* logical OR operation
|
||||||
*/
|
*/
|
||||||
public static <T> BranchUtil<T> or(BooleanSupplier... valueSuppliers) {
|
public static BranchUtil or(BooleanSupplier... valueSuppliers) {
|
||||||
return new BranchUtil<>(BoolUtil.or(valueSuppliers));
|
return new BranchUtil(BoolUtil.or(valueSuppliers));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,38 +125,38 @@ public final class BranchUtil<T> {
|
|||||||
* boolean suppliers.
|
* boolean suppliers.
|
||||||
*
|
*
|
||||||
* @param valueSuppliers the boolean suppliers to be evaluated
|
* @param valueSuppliers the boolean suppliers to be evaluated
|
||||||
* @param <T> the type of the result to be handled by the methods
|
|
||||||
* @return a {@code BranchUtil} instance representing the result of the
|
* @return a {@code BranchUtil} instance representing the result of the
|
||||||
* logical AND operation
|
* logical AND operation
|
||||||
*/
|
*/
|
||||||
public static <T> BranchUtil<T> and(BooleanSupplier... valueSuppliers) {
|
public static BranchUtil and(BooleanSupplier... valueSuppliers) {
|
||||||
return new BranchUtil<>(BoolUtil.and(valueSuppliers));
|
return new BranchUtil(BoolUtil.and(valueSuppliers));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the result of the boolean expressions by executing the appropriate handler based
|
* Handles the result of the boolean expressions by executing the appropriate handler based
|
||||||
* on the result.
|
* on the result.
|
||||||
* <p>
|
* <p>
|
||||||
* 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.
|
* {@code false} and an {@code elseHandler} is provided, it is executed.
|
||||||
* <p>
|
* <p>
|
||||||
* Returns the result of the executed handler.
|
* Returns the result of the executed handler.
|
||||||
*
|
*
|
||||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
* @param <T> the type of the result to be handled by the methods
|
||||||
* @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 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 elseHandler} is
|
||||||
* provided and the result of the evaluation is {@code false}
|
* provided and the result of the evaluation is {@code false}
|
||||||
*/
|
*/
|
||||||
public T handle(Supplier<T> ifHandler, Supplier<T> elseHandler) {
|
public <T> T thenSupply(Supplier<T> trueHandler, Supplier<T> falseSupplier) {
|
||||||
if (this.result && Objects.nonNull(ifHandler)) {
|
if (this.result && Objects.nonNull(trueHandler)) {
|
||||||
return ifHandler.get();
|
return trueHandler.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Objects.isNull(elseHandler)) {
|
if (Objects.isNull(falseSupplier)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return elseHandler.get();
|
return falseSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -169,12 +165,13 @@ public final class BranchUtil<T> {
|
|||||||
* <p>
|
* <p>
|
||||||
* Returns the result of the executed handler.
|
* Returns the result of the executed handler.
|
||||||
*
|
*
|
||||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
* @param <T> 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
|
* @return the result of the executed handler, or {@code null} if result of evaluation is
|
||||||
* {@code false}
|
* {@code false}
|
||||||
*/
|
*/
|
||||||
public T handle(Supplier<T> ifHandler) {
|
public <T> T thenSupply(Supplier<T> trueSupplier) {
|
||||||
return handle(ifHandler, null);
|
return thenSupply(trueSupplier, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,44 +181,30 @@ public final class BranchUtil<T> {
|
|||||||
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
|
* 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.
|
* {@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 trueHandler 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 falseHandler the handler to be executed if the result is {@code false} (optional)
|
||||||
*/
|
*/
|
||||||
public void handle(Runnable ifHandler, Runnable elseHandler) {
|
public void then(Runnable trueHandler, Runnable falseHandler) {
|
||||||
if (this.result && Objects.nonNull(ifHandler)) {
|
if (this.result && Objects.nonNull(trueHandler)) {
|
||||||
ifHandler.run();
|
trueHandler.run();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Objects.isNull(elseHandler)) {
|
if (Objects.isNull(falseHandler)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
elseHandler.run();
|
falseHandler.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the result of the boolean expressions by executing the provided handler if the
|
* Handles the result of the boolean expressions by executing the provided handler if the
|
||||||
* result is {@code true}.
|
* 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) {
|
public void then(Runnable trueHandler) {
|
||||||
handle(ifHandler, null);
|
then(trueHandler, null);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The final result of the boolean expression.
|
|
||||||
*/
|
|
||||||
private final boolean result;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the boolean result.
|
|
||||||
*
|
|
||||||
* @return the result
|
|
||||||
*/
|
|
||||||
public boolean getResult() {
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user