Skip to content

Commit

Permalink
Add ThrowingBiObjBooleanConsumer, re #56
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Oct 3, 2024
1 parent 602dbfc commit d461127
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
29 changes: 27 additions & 2 deletions src/main/java/org/libj/util/function/Throwing.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ public static <T> ObjBiIntPredicate<T> rethrow(final ThrowingObjBiIntPredicate<T
*
* <pre>
* {@code
* BiObjIntConsumer<Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
* BiObjIntPredicate<Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
* if (i == 0)
* throw new IOException("i=" + i);
* });
* for (int i = 3; i >= 0; --i)
* predicate.accept(i, -i, i / 2, i * 2);
* predicate.test(i, -i, i / 2, i * 2);
* }
* </pre>
*
Expand All @@ -220,6 +220,31 @@ public static <T,U> BiObjBiIntPredicate<T,U> rethrow(final ThrowingBiObjBiIntPre
return predicate;
}

/**
* Rethrows the checked exception from the specified {@link ThrowingBiObjBiIntPredicate}.
* <p>
* An example of this pattern:
*
* <pre>
* {@code
* BiObjBooleanConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
* if (i == 0)
* throw new IOException("i=" + i);
* });
* for (int i = 3; i >= 0; --i)
* consumer.accept(i, -i, i / 2, i * 2);
* }
* </pre>
*
* @param <T> The type of the first input to the predicate's operation.
* @param <U> The type of the second input to the predicate's operation.
* @param predicate The {@link ThrowingBiObjBooleanConsumer}.
* @return The specified {@link BiObjBooleanConsumer} instance.
*/
public static <T,U> BiObjBooleanConsumer<T,U> rethrow(final ThrowingBiObjBooleanConsumer<T,U,?> predicate) {
return predicate;
}

/**
* Rethrows the checked exception from the specified {@link ThrowingTriConsumer}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
*
* <pre>
* {@code
* BiObjBiIntPredicate<Integer,Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
* BiObjBiIntPredicate<Integer,Integer> predicate = Throwing.rethrow((Integer s, Integer t, int i, int j) -> {
* if (i == 0)
* throw new IOException("i=" + i);
* return false;
* });
* for (int i = 3; i >= 0; --i)
* predicate.accept(i, -i, i / 2, i * 2);
* predicate.test(i, -i, i / 2, i * 2);
* }
* </pre>
*
Expand All @@ -48,8 +48,8 @@ public interface ThrowingBiObjBiIntPredicate<T,U,E extends Throwable> extends Bi
* Evaluates this predicate on the given arguments.
*
* @param t The first input argument.
* @param u The first input argument.
* @param v1 The second input argument.
* @param u The second input argument.
* @param v1 The third input argument.
* @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
*/
@Override
Expand All @@ -70,7 +70,7 @@ default boolean test(final T t, final U u, final int v1, final int v2) {
* @param u The second input argument.
* @param v1 The third input argument.
* @param v2 The fourth input argument.
* @return The function result.
* @return The predicate result.
* @throws E If an exception has occurred.
* @see BiPredicate#test(Object,Object)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) 2024 LibJ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* You should have received a copy of The MIT License (MIT) along with this
* program. If not, see <http://opensource.org/licenses/MIT/>.
*/

package org.libj.util.function;

import java.util.function.BiPredicate;

/**
* Represents a consumer (boolean-valued function) that accepts two object-valued and a {@code boolean}-valued argument.
* <p>
* The {@link ThrowingBiObjBooleanConsumer} distinguishes itself from {@link ObjBiIntPredicate} by allowing the functional interface
* to throw any {@link Throwable}. This can be used to allow lambda expressions to propagate checked exceptions up the expression's
* call stack. An example of this pattern:
*
* <pre>
* {@code
* BiObjBooleanConsumer<Integer,Integer> consumer = Throwing.rethrow((Integer s, Integer t, boolean b) -> {
* if (!b)
* throw new IOException("i=" + i);
* return false;
* });
* consumer.accept(i, -i, true);
* consumer.accept(i, -i, false);
* }
* </pre>
*
* @param <T> The type of the first argument to the consumer.
* @param <U> The type of the second argument to the consumer.
* @param <E> The type of {@link Throwable} that can be thrown.
* @see Throwing#rethrow(ThrowingBiObjBooleanConsumer)
*/
@FunctionalInterface
public interface ThrowingBiObjBooleanConsumer<T,U,E extends Throwable> extends BiObjBooleanConsumer<T,U> {
/**
* Evaluates this consumer on the given arguments.
*
* @param t The first input argument.
* @param u The second input argument.
* @param b The third input argument.
*/
@Override
default void accept(final T t, final U u, final boolean b) {
try {
acceptThrows(t, u, b);
}
catch (final Throwable e) {
Throwing.rethrow(e);
}
}

/**
* Evaluates this consumer on the given arguments, allowing a checked exception to be thrown.
*
* @param t The first input argument.
* @param u The second input argument.
* @param b The third input argument.
* @throws E If an exception has occurred.
* @see BiPredicate#test(Object,Object)
*/
void acceptThrows(T t, U u, boolean b) throws E;
}

0 comments on commit d461127

Please sign in to comment.