[LANG-1568] More failable functional interfaces to match JRE functional

interfaces.
This commit is contained in:
Gary Gregory 2020-06-17 09:30:21 -04:00
parent 8a2d7afd9f
commit 36111ba582
2 changed files with 112 additions and 9 deletions

View File

@ -30,6 +30,7 @@
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
@ -37,12 +38,14 @@
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.LongBinaryOperator;
import java.util.function.LongConsumer;
import java.util.function.LongFunction;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
@ -111,7 +114,7 @@ public interface FailableBiConsumer<O1, O2, T extends Throwable> {
*
* @param object1 the first parameter for the consumable to accept
* @param object2 the second parameter for the consumable to accept
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(O1 object1, O2 object2) throws T;
}
@ -208,7 +211,7 @@ public interface FailableConsumer<O, T extends Throwable> {
* Accepts the consumer.
*
* @param object the parameter for the consumable to accept
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(O object) throws T;
}
@ -246,7 +249,7 @@ public interface FailableDoubleConsumer<T extends Throwable> {
* Accepts the consumer.
*
* @param value the parameter for the consumable to accept
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(double value) throws T;
}
@ -270,6 +273,25 @@ public interface FailableDoubleFunction<R, T extends Throwable> {
R apply(double input) throws T;
}
/**
* A functional interface like {@link DoublePredicate} that declares a {@code Throwable}.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableDoublePredicate<T extends Throwable> {
/**
* Tests the predicate.
*
* @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails.
*/
boolean test(double value) throws T;
}
/**
* A functional interface like {@link DoubleSupplier} that declares a {@code Throwable}.
*
@ -379,7 +401,7 @@ public interface FailableIntConsumer<T extends Throwable> {
* Accepts the consumer.
*
* @param value the parameter for the consumable to accept
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(int value) throws T;
}
@ -403,6 +425,25 @@ public interface FailableIntFunction<R, T extends Throwable> {
R apply(int input) throws T;
}
/**
* A functional interface like {@link IntPredicate} that declares a {@code Throwable}.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableIntPredicate<T extends Throwable> {
/**
* Tests the predicate.
*
* @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails.
*/
boolean test(int value) throws T;
}
/**
* A functional interface like {@link IntSupplier} that declares a {@code Throwable}.
*
@ -492,7 +533,7 @@ public interface FailableLongConsumer<T extends Throwable> {
* Accepts the consumer.
*
* @param object the parameter for the consumable to accept
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(long object) throws T;
}
@ -516,6 +557,25 @@ public interface FailableLongFunction<R, T extends Throwable> {
R apply(long input) throws T;
}
/**
* A functional interface like {@link LongPredicate} that declares a {@code Throwable}.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableLongPredicate<T extends Throwable> {
/**
* Tests the predicate.
*
* @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails.
*/
boolean test(long value) throws T;
}
/**
* A functional interface like {@link LongSupplier} that declares a {@code Throwable}.
*
@ -587,7 +647,7 @@ public interface FailableObjDoubleConsumer<O, T extends Throwable> {
*
* @param object the object parameter for the consumable to accept.
* @param value the double parameter for the consumable to accept.
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(O object, double value) throws T;
}
@ -607,7 +667,7 @@ public interface FailableObjIntConsumer<O, T extends Throwable> {
*
* @param object the object parameter for the consumable to accept.
* @param value the int parameter for the consumable to accept.
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(O object, int value) throws T;
}
@ -627,7 +687,7 @@ public interface FailableObjLongConsumer<O, T extends Throwable> {
*
* @param object the object parameter for the consumable to accept.
* @param value the long parameter for the consumable to accept.
* @throws T if the consumer fails
* @throws T Thrown when the consumer fails.
*/
void accept(O object, long value) throws T;
}

View File

@ -77,6 +77,21 @@ static boolean failingBool() throws SomeException {
return true;
}
static boolean testDouble(double value) throws SomeException {
throwOnOdd();
return true;
}
static boolean testInt(int value) throws SomeException {
throwOnOdd();
return true;
}
static boolean testLong(long value) throws SomeException {
throwOnOdd();
return true;
}
private static void throwOnOdd() throws SomeException {
final int i = ++invocations;
if (i % 2 == 1) {
@ -689,10 +704,20 @@ void testCallable() {
@Test
public void testConstructor() {
// We allow this, which must be an omission to make the ctor private.
// We allow this, which must have been an omission to make the ctor private.
// We could make the ctor private in 4.0.
new Functions();
}
@Test
public void testDoublePredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;
final Functions.FailableDoublePredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations
.testDouble(t1);
assertThrows(SomeException.class, () -> failablePredicate.test(1d));
failablePredicate.test(1d);
}
@Test
public void testFunction() {
final IllegalStateException ise = new IllegalStateException();
@ -855,6 +880,24 @@ public void testGetSupplier() {
assertEquals(0, i.intValue());
}
@Test
public void testIntPredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;
final Functions.FailableIntPredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations
.testInt(t1);
assertThrows(SomeException.class, () -> failablePredicate.test(1));
failablePredicate.test(1);
}
@Test
public void testLongPredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;
final Functions.FailableLongPredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations
.testLong(t1);
assertThrows(SomeException.class, () -> failablePredicate.test(1l));
failablePredicate.test(1l);
}
@Test
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
public void testPredicate() {