Skip to content

Commit 4a8eed6

Browse files
committed
Add ThrowingBiObjLongConsumer and ThrowingObjLongConsumer, re #63
1 parent 0c82fd3 commit 4a8eed6

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.Consumer;
2323
import java.util.function.Function;
2424
import java.util.function.ObjIntConsumer;
25+
import java.util.function.ObjLongConsumer;
2526
import java.util.function.Predicate;
2627
import java.util.function.Supplier;
2728

@@ -147,6 +148,30 @@ public static <T> ObjIntConsumer<T> rethrow(final ThrowingObjIntConsumer<T,?> co
147148
return consumer;
148149
}
149150

151+
/**
152+
* Rethrows the checked exception from the specified {@link ThrowingObjLongConsumer}.
153+
* <p>
154+
* An example of this pattern:
155+
*
156+
* <pre>
157+
* {@code
158+
* ObjLongConsumer<Integer> consumer = Throwing.rethrow((Integer s, long i) -> {
159+
* if (i == 0)
160+
* throw new IOException("i=" + i);
161+
* });
162+
* for (int i = 3; i >= 0; --i)
163+
* consumer.accept(i, -(long)i);
164+
* }
165+
* </pre>
166+
*
167+
* @param <T> The type of the first input to the consumer's operation.
168+
* @param consumer The {@link ThrowingObjLongConsumer}.
169+
* @return The specified {@link ObjLongConsumer} instance.
170+
*/
171+
public static <T> ObjLongConsumer<T> rethrow(final ThrowingObjLongConsumer<T,?> consumer) {
172+
return consumer;
173+
}
174+
150175
/**
151176
* Rethrows the checked exception from the specified {@link ThrowingObjBiIntConsumer}.
152177
* <p>
@@ -245,6 +270,31 @@ public static <T,U> BiObjBooleanConsumer<T,U> rethrow(final ThrowingBiObjBoolean
245270
return predicate;
246271
}
247272

273+
/**
274+
* Rethrows the checked exception from the specified {@link ThrowingBiObjBiIntPredicate}.
275+
* <p>
276+
* An example of this pattern:
277+
*
278+
* <pre>
279+
* {@code
280+
* BiObjLongConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
281+
* if (i == 0)
282+
* throw new IOException("i=" + i);
283+
* });
284+
* for (int i = 3; i >= 0; --i)
285+
* consumer.accept(i, -i, i / 2, i * 2);
286+
* }
287+
* </pre>
288+
*
289+
* @param <T> The type of the first input to the predicate's operation.
290+
* @param <U> The type of the second input to the predicate's operation.
291+
* @param predicate The {@link ThrowingBiObjLongConsumer}.
292+
* @return The specified {@link BiObjLongConsumer} instance.
293+
*/
294+
public static <T,U> BiObjLongConsumer<T,U> rethrow(final ThrowingBiObjLongConsumer<T,U,?> predicate) {
295+
return predicate;
296+
}
297+
248298
/**
249299
* Rethrows the checked exception from the specified {@link ThrowingTriConsumer}.
250300
* <p>
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 (long-valued function) that accepts two object-valued and a {@code long}-valued argument.
23+
* <p>
24+
* The {@link ThrowingBiObjLongConsumer} 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+
* BiObjLongConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, long 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(ThrowingBiObjLongConsumer)
44+
*/
45+
@FunctionalInterface
46+
public interface ThrowingBiObjLongConsumer<T,U,E extends Throwable> extends BiObjLongConsumer<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 long 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, long b) throws E;
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright (c) 2021 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.ObjLongConsumer;
20+
21+
/**
22+
* Represents an operation that accepts an object-valued and a {@code long}-valued argument, and returns no result. Unlike most other
23+
* functional interfaces, {@link ObjLongConsumer} is expected to operate via side-effects.
24+
* <p>
25+
* The {@link ThrowingObjLongConsumer} distinguishes itself from {@link ObjLongConsumer} by allowing the functional interface to throw
26+
* any {@link Throwable}. This can be used to allow lambda expressions to propagate checked exceptions up the expression's call
27+
* stack. An example of this pattern:
28+
*
29+
* <pre>
30+
* {@code
31+
* ObjLongConsumer<String> consumer = Throwing.rethrow((String s, long i) -> {
32+
* if (i == 0)
33+
* throw new IOException("i=" + i);
34+
* });
35+
* for (long i = 3; i >= 0; --i)
36+
* consumer.accept(i, -i);
37+
* }
38+
* </pre>
39+
*
40+
* @param <T> The type of the object argument to the operation.
41+
* @param <E> The type of {@link Throwable} that can be thrown.
42+
* @see Throwing#rethrow(ThrowingObjLongConsumer)
43+
*/
44+
@FunctionalInterface
45+
public interface ThrowingObjLongConsumer<T,E extends Throwable> extends ObjLongConsumer<T> {
46+
@Override
47+
default void accept(final T t, final long value) {
48+
try {
49+
acceptThrows(t, value);
50+
}
51+
catch (final Throwable e) {
52+
Throwing.rethrow(e);
53+
}
54+
}
55+
56+
/**
57+
* Performs this operation on the given argument, allowing a checked exception to be thrown.
58+
*
59+
* @param t The first input argument.
60+
* @param value The second input argument.
61+
* @throws E If an exception has occurred.
62+
* @see ObjLongConsumer#accept(Object,long)
63+
*/
64+
void acceptThrows(T t, long value) throws E;
65+
}

0 commit comments

Comments
 (0)