[LANG-1568] More checked andThen() implementations to mirror the JRE

functional interfaces.
This commit is contained in:
Gary Gregory 2020-06-24 20:10:46 -04:00
parent 47fb776792
commit d4ccd259d9
26 changed files with 462 additions and 14 deletions

View File

@ -61,8 +61,8 @@ static <T, U, E extends Throwable> FailableBiConsumer<T, U, E> nop() {
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
* @throws NullPointerException when {@code after} is null.
* @throws E Thrown when a consumer fails.
* @throws NullPointerException if {@code after} is null.
*/
default FailableBiConsumer<T, U, E> andThen(final FailableBiConsumer<? super T, ? super U, E> after) throws E {
Objects.requireNonNull(after);

View File

@ -53,11 +53,11 @@ static <T, U, R, E extends Throwable> FailableBiFunction<T, U, R, E> nop() {
/**
* Returns a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
*
* @param <V> the type of output of the {@code after} function, and of the composed function
* @param <V> the output type of the {@code after} function, and of the composed function.
* @param after the operation to perform after this one.
* @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
* @throws NullPointerException when {@code after} is null.
* @throws E Thrown when a consumer fails.
* @throws NullPointerException if {@code after} is null.
*/
default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E {
Objects.requireNonNull(after);

View File

@ -58,8 +58,8 @@ static <T, E extends Throwable> FailableConsumer<T, E> nop() {
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}.
* @throws NullPointerException when {@code after} is null
* @throws E Thrown when a consumer fails.
* @throws NullPointerException if {@code after} is null
*/
default FailableConsumer<T, E> andThen(final FailableConsumer<? super T, E> after) throws E {
Objects.requireNonNull(after);

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.DoubleConsumer;
/**
@ -28,6 +29,20 @@
@FunctionalInterface
public interface FailableDoubleConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableDoubleConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleConsumer<E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*
@ -35,4 +50,20 @@ public interface FailableDoubleConsumer<E extends Throwable> {
* @throws E Thrown when the consumer fails.
*/
void accept(double value) throws E;
/**
* Returns a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
* @throws NullPointerException when {@code after} is null.
* @throws E Thrown when a consumer fails.
*/
default FailableDoubleConsumer<E> andThen(final FailableDoubleConsumer<E> after) throws E {
Objects.requireNonNull(after);
return (final double t) -> {
accept(t);
after.accept(t);
};
}
}

View File

@ -29,6 +29,21 @@
@FunctionalInterface
public interface FailableDoubleFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableDoubleFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableDoubleFunction<R, E> nop() {
return NOP;
}
/**
* Applies this function.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableDoubleToIntFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableDoubleToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleToIntFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableDoubleToLongFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableDoubleToLongFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleToLongFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.Function;
/**
@ -46,6 +47,20 @@ static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
*
* @param <V> the output type of the {@code after} function, and of the composed function.
* @return a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
* @param after the operation to perform after this one.
* @throws NullPointerException when {@code after} is null.
* @throws E Thrown when a consumer fails.
*/
default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E {
Objects.requireNonNull(after);
return (final T t) -> after.apply(apply(t));
}
/**
* Applies this function.
*
@ -54,4 +69,5 @@ static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
* @throws E Thrown when the function fails.
*/
R apply(T input) throws E;
}

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.IntConsumer;
/**
@ -28,6 +29,20 @@
@FunctionalInterface
public interface FailableIntConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableIntConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntConsumer<E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*
@ -35,4 +50,21 @@ public interface FailableIntConsumer<E extends Throwable> {
* @throws E Thrown when the consumer fails.
*/
void accept(int value) throws E;
/**
* Returns a composed {@code FailableIntConsumer} like {@link IntConsumer#andThen(IntConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableLongConsumer} like {@link IntConsumer#andThen(IntConsumer)}.
* @throws NullPointerException if {@code after} is null
* @throws E Thrown when a consumer fails.
*/
default FailableIntConsumer<E> andThen(final FailableIntConsumer<E> after) throws E {
Objects.requireNonNull(after);
return (final int t) -> {
accept(t);
after.accept(t);
};
}
}

View File

@ -29,6 +29,21 @@
@FunctionalInterface
public interface FailableIntFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableIntFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableIntFunction<R, E> nop() {
return NOP;
}
/**
* Applies this function.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableIntToDoubleFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableIntToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntToDoubleFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableIntToLongFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableIntToLongFunction NOP = t -> 0L;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntToLongFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.LongConsumer;
/**
@ -28,6 +29,20 @@
@FunctionalInterface
public interface FailableLongConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableLongConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongConsumer<E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*
@ -35,4 +50,20 @@ public interface FailableLongConsumer<E extends Throwable> {
* @throws E Thrown when the consumer fails.
*/
void accept(long object) throws E;
/**
* Returns a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}.
* @throws NullPointerException if {@code after} is null
* @throws E Thrown when a consumer fails.
*/
default FailableLongConsumer<E> andThen(final FailableLongConsumer<E> after) throws E {
Objects.requireNonNull(after);
return (final long t) -> {
accept(t);
after.accept(t);
};
}
}

View File

@ -29,6 +29,21 @@
@FunctionalInterface
public interface FailableLongFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableLongFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableLongFunction<R, E> nop() {
return NOP;
}
/**
* Applies this function.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableLongToDoubleFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableLongToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongToDoubleFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -28,6 +28,20 @@
@FunctionalInterface
public interface FailableLongToIntFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableLongToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongToIntFunction<E> nop() {
return NOP;
}
/**
* Applies this function to the given argument.
*

View File

@ -29,11 +29,25 @@
@FunctionalInterface
public interface FailableObjDoubleConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableObjDoubleConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjDoubleConsumer<T, E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*
* @param object the object parameter for the consumable to accept.
* @param value the double parameter for the consumable to accept.
* @param value the double parameter for the consumable to accept.
* @throws E Thrown when the consumer fails.
*/
void accept(T object, double value) throws E;

View File

@ -29,6 +29,20 @@
@FunctionalInterface
public interface FailableObjIntConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableObjIntConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjIntConsumer<T, E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*

View File

@ -29,6 +29,20 @@
@FunctionalInterface
public interface FailableObjLongConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableObjLongConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjLongConsumer<T, E> nop() {
return NOP;
}
/**
* Accepts the consumer.
*

View File

@ -30,6 +30,22 @@
@FunctionalInterface
public interface FailableToDoubleBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToDoubleBiFunction NOP = (t, u) -> 0d;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToDoubleBiFunction<T, U, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -22,13 +22,28 @@
/**
* A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}.
*
* @param <T> the type of the first argument to the function
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableToDoubleFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToDoubleFunction<T, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -30,6 +30,22 @@
@FunctionalInterface
public interface FailableToIntBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToIntBiFunction NOP = (t, u) -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToIntBiFunction<T, U, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -22,13 +22,28 @@
/**
* A functional interface like {@link ToIntFunction} that declares a {@code Throwable}.
*
* @param <T> the type of the first argument to the function
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableToIntFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToIntFunction<T, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -30,6 +30,22 @@
@FunctionalInterface
public interface FailableToLongBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToLongBiFunction NOP = (t, u) -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToLongBiFunction<T, U, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -29,6 +29,21 @@
@FunctionalInterface
public interface FailableToLongFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
final FailableToLongFunction NOP = t -> 0L;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToLongFunction<T, E> nop() {
return NOP;
}
/**
* Applies this function to the given arguments.
*

View File

@ -66,6 +66,7 @@ public void run(final Throwable pTh) throws Throwable {
}
}
}
public static class FailureOnOddInvocations {
private static int invocations;
@ -629,18 +630,19 @@ public void testBiConsumer() throws Throwable {
@Test
public void testBiConsumerAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> {
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failing = (t, th) -> {
t.setThrowable(th);
t.test();
};
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> nop = FailableBiConsumer.nop();
final Throwable e = assertThrows(OutOfMemoryError.class,
() -> nop.andThen(failableBiConsumer).accept(testable, ERROR));
Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(testable, ERROR));
assertSame(ERROR, e);
e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(testable, ERROR));
assertSame(ERROR, e);
// Does not throw
nop.andThen(nop);
// Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> failableBiConsumer.andThen(null));
assertThrows(NullPointerException.class, () -> failing.andThen(null));
}
@Test
@ -680,7 +682,8 @@ public void testBiFunctionAndThen() throws IOException {
throw new IOException();
};
final FailableFunction<Object, Integer, IOException> failingFunction = (t) -> { throw new IOException(); };
final FailableBiFunction<Object, Integer, Integer, IOException> nopFailableBiFunction = FailableBiFunction.nop();
final FailableBiFunction<Object, Integer, Integer, IOException> nopFailableBiFunction = FailableBiFunction
.nop();
final FailableFunction<Object, Integer, IOException> nopFailableFunction = FailableFunction.nop();
//
assertThrows(IOException.class, () -> failingBiFunctionTest.andThen(failingFunction).apply(null, null));
@ -737,8 +740,7 @@ public void testConsumerAndThen() throws Throwable {
testable.test();
};
final FailableConsumer<Throwable, Throwable> nop = FailableConsumer.nop();
final Throwable e = assertThrows(OutOfMemoryError.class,
() -> nop.andThen(failableConsumer).accept(ERROR));
final Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failableConsumer).accept(ERROR));
assertSame(ERROR, e);
// Does not throw
nop.andThen(nop);
@ -746,6 +748,24 @@ public void testConsumerAndThen() throws Throwable {
assertThrows(NullPointerException.class, () -> failableConsumer.andThen(null));
}
@Test
public void testDoubleConsumerAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
final FailableDoubleConsumer<Throwable> failing = t -> {
testable.setThrowable(ERROR);
testable.test();
};
final FailableDoubleConsumer<Throwable> nop = FailableDoubleConsumer.nop();
Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0d));
assertSame(ERROR, e);
e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0d));
assertSame(ERROR, e);
// Does not throw
nop.andThen(nop);
// Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> failing.andThen(null));
}
@Test
public void testDoublePredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;
@ -779,6 +799,24 @@ public void testFunction() {
assertEquals(0, function.apply(null).intValue());
}
@Test
public void testFunctionAndThen() throws IOException {
// Unchecked usage pattern in JRE
final Function<Object, Integer> nopFunction = (t) -> null;
nopFunction.andThen(nopFunction);
// Checked usage pattern
final FailableFunction<Object, Integer, IOException> failingFunction = (t) -> { throw new IOException(); };
final FailableFunction<Object, Integer, IOException> nopFailableFunction = FailableFunction.nop();
//
assertThrows(IOException.class, () -> failingFunction.andThen(failingFunction).apply(null));
assertThrows(IOException.class, () -> failingFunction.andThen(nopFailableFunction).apply(null));
//
assertThrows(IOException.class, () -> nopFailableFunction.andThen(failingFunction).apply(null));
nopFailableFunction.andThen(nopFailableFunction).apply(null);
// Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> failingFunction.andThen(null));
}
@Test
public void testGetAsBooleanSupplier() {
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
@ -904,6 +942,24 @@ public void testGetSupplier() {
assertEquals(0, i.intValue());
}
@Test
public void testIntConsumerAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
final FailableIntConsumer<Throwable> failing = t -> {
testable.setThrowable(ERROR);
testable.test();
};
final FailableIntConsumer<Throwable> nop = FailableIntConsumer.nop();
Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0));
assertSame(ERROR, e);
e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0));
assertSame(ERROR, e);
// Does not throw
nop.andThen(nop);
// Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> failing.andThen(null));
}
@Test
public void testIntPredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;
@ -912,6 +968,24 @@ public void testIntPredicate() throws Throwable {
failablePredicate.test(1);
}
@Test
public void testLongConsumerAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
final FailableLongConsumer<Throwable> failing = t -> {
testable.setThrowable(ERROR);
testable.test();
};
final FailableLongConsumer<Throwable> nop = FailableLongConsumer.nop();
Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0L));
assertSame(ERROR, e);
e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0L));
assertSame(ERROR, e);
// Does not throw
nop.andThen(nop);
// Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> failing.andThen(null));
}
@Test
public void testLongPredicate() throws Throwable {
FailureOnOddInvocations.invocations = 0;