Skip to content

Commit 81eee46

Browse files
authored
Merge pull request #61 from onixbyte/feature/optimised-generic-type
feat: refactor BranchUtil<T> to BranchUtil
2 parents be38668 + 7abb695 commit 81eee46

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

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

Lines changed: 46 additions & 50 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
145-
* {@code false} and an {@code elseHandler} is provided, it is executed.
139+
* If the result is {@code true}, the {@code trueSupplier} is executed. If the result is
140+
* {@code false} and an {@code falseSupplier} is provided, it is executed.
146141
* <p>
147-
* Returns the result of the executed handler.
142+
* Returns the result of the executed supplier.
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)
151-
* @return the result of the executed handler, or {@code null} if no {@code elseHandler} is
144+
* @param <T> the type of the result to be handled by the methods
145+
* @param trueSupplier the supplier to be executed if the result is {@code true}
146+
* @param falseSupplier the supplier to be executed if the result is {@code false} (optional)
147+
* @return the result of the executed supplier, or {@code null} if no {@code falseSupplier} 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> trueSupplier, Supplier<T> falseSupplier) {
151+
if (this.result && Objects.nonNull(trueSupplier)) {
152+
return trueSupplier.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,12 @@ 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}
173-
* @return the result of the executed handler, or {@code null} if result of evaluation is
174-
* {@code false}
168+
* @param <T> the type of the result to be handled by the methods
169+
* @param trueSupplier the supplier to be executed if the result is {@code true}
170+
* @return the result of the executed handler, or {@code null} if result of evaluation is {@code false}
175171
*/
176-
public T handle(Supplier<T> ifHandler) {
177-
return handle(ifHandler, null);
172+
public <T> T thenSupply(Supplier<T> trueSupplier) {
173+
return thenSupply(trueSupplier, null);
178174
}
179175

180176
/**
@@ -184,42 +180,42 @@ public T handle(Supplier<T> ifHandler) {
184180
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
185181
* {@code false} and an {@code elseHandler} is provided, it is executed.
186182
*
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)
183+
* @param trueHandler the handler to be executed if the result is {@code true}
184+
* @param falseHandler the handler to be executed if the result is {@code false} (optional)
189185
*/
190-
public void handle(Runnable ifHandler, Runnable elseHandler) {
191-
if (this.result && Objects.nonNull(ifHandler)) {
192-
ifHandler.run();
186+
public void then(Runnable trueHandler, Runnable falseHandler) {
187+
if (this.result && Objects.nonNull(trueHandler)) {
188+
trueHandler.run();
193189
return;
194190
}
195191

196-
if (Objects.isNull(elseHandler)) {
192+
if (Objects.isNull(falseHandler)) {
197193
return;
198194
}
199195

200-
elseHandler.run();
196+
falseHandler.run();
201197
}
202198

203199
/**
204200
* Handles the result of the boolean expressions by executing the provided handler if the
205201
* result is {@code true}.
206202
*
207-
* @param ifHandler the handler to be executed if the result is {@code true}
203+
* @param trueHandler the handler to be executed if the result is {@code true}
208204
*/
209-
public void handle(Runnable ifHandler) {
210-
handle(ifHandler, null);
205+
public void then(Runnable trueHandler) {
206+
then(trueHandler, null);
211207
}
212208

213-
/**
214-
* The final result of the boolean expression.
215-
*/
216-
private final boolean result;
217-
218209
/**
219210
* Get the boolean result.
211+
* <p>
212+
* <b>Note:</b> {@link BranchUtil} is not responsible for getting a raw boolean result, consider use
213+
* {@link BoolUtil} to replace.
220214
*
221215
* @return the result
216+
* @see BoolUtil
222217
*/
218+
@Deprecated(forRemoval = true)
223219
public boolean getResult() {
224220
return result;
225221
}

0 commit comments

Comments
 (0)