1
+ /* Copyright (c) 2019 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
+ /**
20
+ * Represents a predicate (boolean-valued function) that accepts three arguments.
21
+ * <p>
22
+ * The {@link ThrowingTriPredicate} distinguishes itself from {@link TriPredicate} by allowing the functional interface to throw any
23
+ * {@link Throwable}. This can be used to allow lambda expressions to propagate checked exceptions up the expression's call stack.
24
+ * An example of this pattern:
25
+ *
26
+ * <pre>
27
+ * {@code
28
+ * TriPredicate<Integer,Integer,Integer> predicate = Throwing.rethrow((Integer i, Integer j, Integer k) -> {
29
+ * if (i == 0)
30
+ * throw new IOException("i=" + i);
31
+ * return false;
32
+ * });
33
+ * for (int i = 3; i >= 0; --i)
34
+ * predicate.accept(i, -i, i);
35
+ * }
36
+ * </pre>
37
+ *
38
+ * @param <T> The type of the first argument to the predicate.
39
+ * @param <U> The type of the second argument to the predicate.
40
+ * @param <V> The type of the third argument to the predicate.
41
+ * @param <E> The type of {@link Throwable} that can be thrown.
42
+ * @see Throwing#rethrow(ThrowingTriPredicate)
43
+ */
44
+ @ FunctionalInterface
45
+ public interface ThrowingTriPredicate <T ,U ,V ,E extends Throwable > extends TriPredicate <T ,U ,V > {
46
+ /**
47
+ * Evaluates this predicate on the given arguments.
48
+ *
49
+ * @param t The first input argument.
50
+ * @param u The second input argument.
51
+ * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
52
+ */
53
+ @ Override
54
+ default boolean test (final T t , final U u , final V v ) {
55
+ try {
56
+ return testThrows (t , u , v );
57
+ }
58
+ catch (final Throwable e ) {
59
+ Throwing .rethrow (e );
60
+ return false ;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Evaluates this predicate 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 v The third input argument.
70
+ * @return The function result.
71
+ * @throws E If an exception has occurred.
72
+ * @see TriPredicate#test(Object,Object,Object)
73
+ */
74
+ boolean testThrows (T t , U u , V v ) throws E ;
75
+ }
0 commit comments