Skip to content

Commit d461127

Browse files
committed
Add ThrowingBiObjBooleanConsumer, re #56
1 parent 602dbfc commit d461127

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

src/main/java/org/libj/util/function/Throwing.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ public static <T> ObjBiIntPredicate<T> rethrow(final ThrowingObjBiIntPredicate<T
202202
*
203203
* <pre>
204204
* {@code
205-
* BiObjIntConsumer<Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
205+
* BiObjIntPredicate<Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
206206
* if (i == 0)
207207
* throw new IOException("i=" + i);
208208
* });
209209
* for (int i = 3; i >= 0; --i)
210-
* predicate.accept(i, -i, i / 2, i * 2);
210+
* predicate.test(i, -i, i / 2, i * 2);
211211
* }
212212
* </pre>
213213
*
@@ -220,6 +220,31 @@ public static <T,U> BiObjBiIntPredicate<T,U> rethrow(final ThrowingBiObjBiIntPre
220220
return predicate;
221221
}
222222

223+
/**
224+
* Rethrows the checked exception from the specified {@link ThrowingBiObjBiIntPredicate}.
225+
* <p>
226+
* An example of this pattern:
227+
*
228+
* <pre>
229+
* {@code
230+
* BiObjBooleanConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
231+
* if (i == 0)
232+
* throw new IOException("i=" + i);
233+
* });
234+
* for (int i = 3; i >= 0; --i)
235+
* consumer.accept(i, -i, i / 2, i * 2);
236+
* }
237+
* </pre>
238+
*
239+
* @param <T> The type of the first input to the predicate's operation.
240+
* @param <U> The type of the second input to the predicate's operation.
241+
* @param predicate The {@link ThrowingBiObjBooleanConsumer}.
242+
* @return The specified {@link BiObjBooleanConsumer} instance.
243+
*/
244+
public static <T,U> BiObjBooleanConsumer<T,U> rethrow(final ThrowingBiObjBooleanConsumer<T,U,?> predicate) {
245+
return predicate;
246+
}
247+
223248
/**
224249
* Rethrows the checked exception from the specified {@link ThrowingTriConsumer}.
225250
* <p>

src/main/java/org/libj/util/function/ThrowingBiObjBiIntPredicate.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
*
2828
* <pre>
2929
* {@code
30-
* BiObjBiIntPredicate<Integer,Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
30+
* BiObjBiIntPredicate<Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
3131
* if (i == 0)
3232
* throw new IOException("i=" + i);
3333
* return false;
3434
* });
3535
* for (int i = 3; i >= 0; --i)
36-
* predicate.accept(i, -i, i / 2, i * 2);
36+
* predicate.test(i, -i, i / 2, i * 2);
3737
* }
3838
* </pre>
3939
*
@@ -48,8 +48,8 @@ public interface ThrowingBiObjBiIntPredicate<T,U,E extends Throwable> extends Bi
4848
* Evaluates this predicate on the given arguments.
4949
*
5050
* @param t The first input argument.
51-
* @param u The first input argument.
52-
* @param v1 The second input argument.
51+
* @param u The second input argument.
52+
* @param v1 The third input argument.
5353
* @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
5454
*/
5555
@Override
@@ -70,7 +70,7 @@ default boolean test(final T t, final U u, final int v1, final int v2) {
7070
* @param u The second input argument.
7171
* @param v1 The third input argument.
7272
* @param v2 The fourth input argument.
73-
* @return The function result.
73+
* @return The predicate result.
7474
* @throws E If an exception has occurred.
7575
* @see BiPredicate#test(Object,Object)
7676
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* Copyright (c) 2024 LibJ
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* You should have received a copy of The MIT License (MIT) along with this
14+
* program. If not, see <http://opensource.org/licenses/MIT/>.
15+
*/
16+
17+
package org.libj.util.function;
18+
19+
import java.util.function.BiPredicate;
20+
21+
/**
22+
* Represents a consumer (boolean-valued function) that accepts two object-valued and a {@code boolean}-valued argument.
23+
* <p>
24+
* The {@link ThrowingBiObjBooleanConsumer} distinguishes itself from {@link ObjBiIntPredicate} by allowing the functional interface
25+
* to throw any {@link Throwable}. This can be used to allow lambda expressions to propagate checked exceptions up the expression's
26+
* call stack. An example of this pattern:
27+
*
28+
* <pre>
29+
* {@code
30+
* BiObjBooleanConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, boolean b) -> {
31+
* if (!b)
32+
* throw new IOException("i=" + i);
33+
* return false;
34+
* });
35+
* consumer.accept(i, -i, true);
36+
* consumer.accept(i, -i, false);
37+
* }
38+
* </pre>
39+
*
40+
* @param <T> The type of the first argument to the consumer.
41+
* @param <U> The type of the second argument to the consumer.
42+
* @param <E> The type of {@link Throwable} that can be thrown.
43+
* @see Throwing#rethrow(ThrowingBiObjBooleanConsumer)
44+
*/
45+
@FunctionalInterface
46+
public interface ThrowingBiObjBooleanConsumer<T,U,E extends Throwable> extends BiObjBooleanConsumer<T,U> {
47+
/**
48+
* Evaluates this consumer on the given arguments.
49+
*
50+
* @param t The first input argument.
51+
* @param u The second input argument.
52+
* @param b The third input argument.
53+
*/
54+
@Override
55+
default void accept(final T t, final U u, final boolean b) {
56+
try {
57+
acceptThrows(t, u, b);
58+
}
59+
catch (final Throwable e) {
60+
Throwing.rethrow(e);
61+
}
62+
}
63+
64+
/**
65+
* Evaluates this consumer on the given arguments, allowing a checked exception to be thrown.
66+
*
67+
* @param t The first input argument.
68+
* @param u The second input argument.
69+
* @param b The third input argument.
70+
* @throws E If an exception has occurred.
71+
* @see BiPredicate#test(Object,Object)
72+
*/
73+
void acceptThrows(T t, U u, boolean b) throws E;
74+
}

0 commit comments

Comments
 (0)