17
17
18
18
package com .onixbyte .devkit .utils ;
19
19
20
- import org .slf4j .Logger ;
21
- import org .slf4j .LoggerFactory ;
22
-
23
20
import java .util .Objects ;
24
21
import java .util .function .BooleanSupplier ;
25
22
import java .util .function .Supplier ;
66
63
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean
67
64
* expressions.
68
65
*
69
- * @param <T> the type of the result to be handled by the methods
70
66
* @author zihluwang
71
- * @version 1.6.1
67
+ * @version 2.1.3
72
68
* @see java.util.function.Supplier
73
69
* @see java.util.function.BooleanSupplier
74
70
* @see java.lang.Runnable
75
71
* @since 1.0.0
76
72
*/
77
- public final class BranchUtil < T > {
73
+ public final class BranchUtil {
78
74
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 ;
80
79
81
80
/**
82
81
* Create a {@code BranchUtil} instance.
@@ -92,75 +91,72 @@ private BranchUtil(boolean result) {
92
91
* boolean expressions.
93
92
*
94
93
* @param values the boolean expressions to be evaluated
95
- * @param <T> the type of the result to be handled by the methods
96
94
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
97
95
*/
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 ));
100
98
}
101
99
102
100
/**
103
101
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
104
102
* boolean expressions.
105
103
*
106
104
* @param values the boolean expressions to be evaluated
107
- * @param <T> the type of the result to be handled by the methods
108
105
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
109
106
*/
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 ));
112
109
}
113
110
114
111
/**
115
112
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided
116
113
* boolean suppliers.
117
114
*
118
115
* @param valueSuppliers the boolean suppliers to be evaluated
119
- * @param <T> the type of the result to be handled by the methods
120
116
* @return a {@code BranchUtil} instance representing the result of the
121
117
* logical OR operation
122
118
*/
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 ));
125
121
}
126
122
127
123
/**
128
124
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
129
125
* boolean suppliers.
130
126
*
131
127
* @param valueSuppliers the boolean suppliers to be evaluated
132
- * @param <T> the type of the result to be handled by the methods
133
128
* @return a {@code BranchUtil} instance representing the result of the
134
129
* logical AND operation
135
130
*/
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 ));
138
133
}
139
134
140
135
/**
141
136
* Handles the result of the boolean expressions by executing the appropriate handler based
142
137
* on the result.
143
138
* <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.
146
141
* <p>
147
- * Returns the result of the executed handler .
142
+ * Returns the result of the executed supplier .
148
143
*
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
152
148
* provided and the result of the evaluation is {@code false}
153
149
*/
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 ();
157
153
}
158
154
159
- if (Objects .isNull (elseHandler )) {
155
+ if (Objects .isNull (falseSupplier )) {
160
156
return null ;
161
157
}
162
158
163
- return elseHandler .get ();
159
+ return falseSupplier .get ();
164
160
}
165
161
166
162
/**
@@ -169,12 +165,12 @@ public T handle(Supplier<T> ifHandler, Supplier<T> elseHandler) {
169
165
* <p>
170
166
* Returns the result of the executed handler.
171
167
*
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}
175
171
*/
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 );
178
174
}
179
175
180
176
/**
@@ -184,42 +180,42 @@ public T handle(Supplier<T> ifHandler) {
184
180
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
185
181
* {@code false} and an {@code elseHandler} is provided, it is executed.
186
182
*
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)
189
185
*/
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 ();
193
189
return ;
194
190
}
195
191
196
- if (Objects .isNull (elseHandler )) {
192
+ if (Objects .isNull (falseHandler )) {
197
193
return ;
198
194
}
199
195
200
- elseHandler .run ();
196
+ falseHandler .run ();
201
197
}
202
198
203
199
/**
204
200
* Handles the result of the boolean expressions by executing the provided handler if the
205
201
* result is {@code true}.
206
202
*
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}
208
204
*/
209
- public void handle (Runnable ifHandler ) {
210
- handle ( ifHandler , null );
205
+ public void then (Runnable trueHandler ) {
206
+ then ( trueHandler , null );
211
207
}
212
208
213
- /**
214
- * The final result of the boolean expression.
215
- */
216
- private final boolean result ;
217
-
218
209
/**
219
210
* 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.
220
214
*
221
215
* @return the result
216
+ * @see BoolUtil
222
217
*/
218
+ @ Deprecated (forRemoval = true )
223
219
public boolean getResult () {
224
220
return result ;
225
221
}
0 commit comments