Skip to content

Commit 9ca983c

Browse files
committed
Add ThrowingTriPredicate
1 parent 56bb5d3 commit 9ca983c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,33 @@ public static <T,U> BiPredicate<T,U> rethrow(final ThrowingBiPredicate<T,U,?> pr
320320
return predicate;
321321
}
322322

323+
/**
324+
* Rethrows the checked exception from the specified {@link ThrowingTriPredicate}.
325+
* <p>
326+
* An example of this pattern:
327+
*
328+
* <pre>
329+
* {@code
330+
* TriPredicate<Integer,Integer,Integer> predicate = Throwing.rethrow((Integer i, Integer j, Integer k) -> {
331+
* if (i == 0)
332+
* throw new IOException("i=" + i);
333+
* return false;
334+
* });
335+
* for (int i = 3; i >= 0; --i)
336+
* predicate.accept(i, -i, i);
337+
* }
338+
* </pre>
339+
*
340+
* @param <T> The type of the first input to the predicate's operation.
341+
* @param <U> The type of the second input to the predicate's operation.
342+
* @param <V> The type of the third input to the predicate's operation.
343+
* @param predicate The {@link ThrowingTriPredicate}.
344+
* @return The specified {@link TriPredicate} instance.
345+
*/
346+
public static <T,U,V> TriPredicate<T,U,V> rethrow(final ThrowingTriPredicate<T,U,V,?> predicate) {
347+
return predicate;
348+
}
349+
323350
/**
324351
* Rethrows the checked exception from the specified {@link ThrowingFunction}.
325352
* <p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)