Skip to content

Commit 0642bd8

Browse files
committed
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.
1 parent be38668 commit 0642bd8

File tree

1 file changed

+37
-54
lines changed

1 file changed

+37
-54
lines changed

devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import java.util.Objects;
2421
import java.util.function.BooleanSupplier;
2522
import java.util.function.Supplier;
@@ -66,17 +63,19 @@
6663
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean
6764
* expressions.
6865
*
69-
* @param <T> the type of the result to be handled by the methods
7066
* @author zihluwang
71-
* @version 1.6.1
67+
* @version 2.1.3
7268
* @see java.util.function.Supplier
7369
* @see java.util.function.BooleanSupplier
7470
* @see java.lang.Runnable
7571
* @since 1.0.0
7672
*/
77-
public final class BranchUtil<T> {
73+
public final class BranchUtil {
7874

79-
private final static Logger log = LoggerFactory.getLogger(BranchUtil.class);
75+
/**
76+
* The final result of the boolean expression.
77+
*/
78+
private final boolean result;
8079

8180
/**
8281
* Create a {@code BranchUtil} instance.
@@ -92,75 +91,72 @@ private BranchUtil(boolean result) {
9291
* boolean expressions.
9392
*
9493
* @param values the boolean expressions to be evaluated
95-
* @param <T> the type of the result to be handled by the methods
9694
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
9795
*/
98-
public static <T> BranchUtil<T> or(Boolean... values) {
99-
return new BranchUtil<>(BoolUtil.or(values));
96+
public static BranchUtil or(Boolean... values) {
97+
return new BranchUtil(BoolUtil.or(values));
10098
}
10199

102100
/**
103101
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
104102
* boolean expressions.
105103
*
106104
* @param values the boolean expressions to be evaluated
107-
* @param <T> the type of the result to be handled by the methods
108105
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
109106
*/
110-
public static <T> BranchUtil<T> and(Boolean... values) {
111-
return new BranchUtil<>(BoolUtil.and(values));
107+
public static BranchUtil and(Boolean... values) {
108+
return new BranchUtil(BoolUtil.and(values));
112109
}
113110

114111
/**
115112
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided
116113
* boolean suppliers.
117114
*
118115
* @param valueSuppliers the boolean suppliers to be evaluated
119-
* @param <T> the type of the result to be handled by the methods
120116
* @return a {@code BranchUtil} instance representing the result of the
121117
* logical OR operation
122118
*/
123-
public static <T> BranchUtil<T> or(BooleanSupplier... valueSuppliers) {
124-
return new BranchUtil<>(BoolUtil.or(valueSuppliers));
119+
public static BranchUtil or(BooleanSupplier... valueSuppliers) {
120+
return new BranchUtil(BoolUtil.or(valueSuppliers));
125121
}
126122

127123
/**
128124
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
129125
* boolean suppliers.
130126
*
131127
* @param valueSuppliers the boolean suppliers to be evaluated
132-
* @param <T> the type of the result to be handled by the methods
133128
* @return a {@code BranchUtil} instance representing the result of the
134129
* logical AND operation
135130
*/
136-
public static <T> BranchUtil<T> and(BooleanSupplier... valueSuppliers) {
137-
return new BranchUtil<>(BoolUtil.and(valueSuppliers));
131+
public static BranchUtil and(BooleanSupplier... valueSuppliers) {
132+
return new BranchUtil(BoolUtil.and(valueSuppliers));
138133
}
139134

140135
/**
141136
* Handles the result of the boolean expressions by executing the appropriate handler based
142137
* on the result.
143138
* <p>
144-
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
139+
* If the result is {@code true}, the {@code trueHandler} is executed. If the result is
145140
* {@code false} and an {@code elseHandler} is provided, it is executed.
146141
* <p>
147142
* Returns the result of the executed handler.
148143
*
149-
* @param ifHandler the handler to be executed if the result is {@code true}
150-
* @param elseHandler the handler to be executed if the result is {@code false} (optional)
144+
* @param <T> the type of the result to be handled by the methods
145+
* @param trueHandler the handler to be executed if the result is {@code true}
146+
* @param falseSupplier the handler to be executed if the result is {@code false} (optional)
151147
* @return the result of the executed handler, or {@code null} if no {@code elseHandler} is
152148
* provided and the result of the evaluation is {@code false}
153149
*/
154-
public T handle(Supplier<T> ifHandler, Supplier<T> elseHandler) {
155-
if (this.result && Objects.nonNull(ifHandler)) {
156-
return ifHandler.get();
150+
public <T> T thenSupply(Supplier<T> trueHandler, Supplier<T> falseSupplier) {
151+
if (this.result && Objects.nonNull(trueHandler)) {
152+
return trueHandler.get();
157153
}
158154

159-
if (Objects.isNull(elseHandler)) {
155+
if (Objects.isNull(falseSupplier)) {
160156
return null;
161157
}
162158

163-
return elseHandler.get();
159+
return falseSupplier.get();
164160
}
165161

166162
/**
@@ -169,12 +165,13 @@ public T handle(Supplier<T> ifHandler, Supplier<T> elseHandler) {
169165
* <p>
170166
* Returns the result of the executed handler.
171167
*
172-
* @param ifHandler the handler to be executed if the result is {@code true}
168+
* @param <T> the type of the result to be handled by the methods
169+
* @param trueSupplier the handler to be executed if the result is {@code true}
173170
* @return the result of the executed handler, or {@code null} if result of evaluation is
174171
* {@code false}
175172
*/
176-
public T handle(Supplier<T> ifHandler) {
177-
return handle(ifHandler, null);
173+
public <T> T thenSupply(Supplier<T> trueSupplier) {
174+
return thenSupply(trueSupplier, null);
178175
}
179176

180177
/**
@@ -184,44 +181,30 @@ public T handle(Supplier<T> ifHandler) {
184181
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
185182
* {@code false} and an {@code elseHandler} is provided, it is executed.
186183
*
187-
* @param ifHandler the handler to be executed if the result is {@code true}
188-
* @param elseHandler the handler to be executed if the result is {@code false} (optional)
184+
* @param trueHandler the handler to be executed if the result is {@code true}
185+
* @param falseHandler the handler to be executed if the result is {@code false} (optional)
189186
*/
190-
public void handle(Runnable ifHandler, Runnable elseHandler) {
191-
if (this.result && Objects.nonNull(ifHandler)) {
192-
ifHandler.run();
187+
public void then(Runnable trueHandler, Runnable falseHandler) {
188+
if (this.result && Objects.nonNull(trueHandler)) {
189+
trueHandler.run();
193190
return;
194191
}
195192

196-
if (Objects.isNull(elseHandler)) {
193+
if (Objects.isNull(falseHandler)) {
197194
return;
198195
}
199196

200-
elseHandler.run();
197+
falseHandler.run();
201198
}
202199

203200
/**
204201
* Handles the result of the boolean expressions by executing the provided handler if the
205202
* result is {@code true}.
206203
*
207-
* @param ifHandler the handler to be executed if the result is {@code true}
208-
*/
209-
public void handle(Runnable ifHandler) {
210-
handle(ifHandler, null);
211-
}
212-
213-
/**
214-
* The final result of the boolean expression.
215-
*/
216-
private final boolean result;
217-
218-
/**
219-
* Get the boolean result.
220-
*
221-
* @return the result
204+
* @param trueHandler the handler to be executed if the result is {@code true}
222205
*/
223-
public boolean getResult() {
224-
return result;
206+
public void then(Runnable trueHandler) {
207+
then(trueHandler, null);
225208
}
226209

227210
}

0 commit comments

Comments
 (0)