Fix boxing/unboxing.

This commit is contained in:
Gary Gregory 2020-06-24 10:47:00 -04:00
parent 94d5208dd9
commit c10c62ed73
1 changed files with 19 additions and 3 deletions

View File

@ -32,6 +32,7 @@ import java.util.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.commons.lang3.Streams.FailableStream; import org.apache.commons.lang3.Streams.FailableStream;
import org.apache.commons.lang3.function.FailableBooleanSupplier;
/** /**
* This class provides utility functions, and classes for working with the {@code java.util.function} package, or more * This class provides utility functions, and classes for working with the {@code java.util.function} package, or more
@ -477,6 +478,21 @@ public class Functions {
} }
} }
/**
* Invokes a boolean supplier, and returns the result.
*
* @param supplier The boolean supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier
*/
private static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<T> supplier) {
try {
return supplier.getAsBoolean();
} catch (final Throwable t) {
throw rethrow(t);
}
}
/** /**
* <p> * <p>
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a * Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
@ -574,7 +590,7 @@ public class Functions {
*/ */
public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate, public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate,
final O1 object1, final O2 object2) { final O1 object1, final O2 object2) {
return get(() -> predicate.test(object1, object2)); return getAsBoolean(() -> predicate.test(object1, object2));
} }
/** /**
@ -587,7 +603,7 @@ public class Functions {
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) { public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) {
return get(() -> predicate.test(object)); return getAsBoolean(() -> predicate.test(object));
} }
/** /**