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