[LANG-1568]
org.apache.commons.lang3.function.FailableBiFunction.andThen(FailableFunction<? super R, ? extends V, E>)
This commit is contained in:
parent
4badcde7f7
commit
c7aea90a7a
|
@ -37,7 +37,7 @@ public interface FailableBiConsumer<T, U, E extends Throwable> {
|
|||
|
||||
/**
|
||||
* Returns The NOP singleton.
|
||||
*
|
||||
*
|
||||
* @param <T> Consumed type 1.
|
||||
* @param <U> Consumed type 2.
|
||||
* @param <E> Thrown exception.
|
||||
|
@ -62,9 +62,9 @@ public interface FailableBiConsumer<T, U, E extends Throwable> {
|
|||
* @param after the operation to perform after this one.
|
||||
* @return a composed {@code FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
|
||||
* @throws E Thrown when a consumer fails.
|
||||
* @throws NullPointerException if {@code after} is null
|
||||
* @throws NullPointerException if {@code after} is null.
|
||||
*/
|
||||
default FailableBiConsumer<T, U, E> andThen(FailableBiConsumer<? super T, ? super U, E> after) throws E {
|
||||
default FailableBiConsumer<T, U, E> andThen(final FailableBiConsumer<? super T, ? super U, E> after) throws E {
|
||||
Objects.requireNonNull(after);
|
||||
|
||||
return (t, u) -> {
|
||||
|
|
|
@ -17,7 +17,9 @@
|
|||
|
||||
package org.apache.commons.lang3.function;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A functional interface like {@link BiFunction} that declares a {@code Throwable}.
|
||||
|
@ -31,6 +33,37 @@ import java.util.function.BiFunction;
|
|||
@FunctionalInterface
|
||||
public interface FailableBiFunction<T, U, R, E extends Throwable> {
|
||||
|
||||
/** NOP singleton */
|
||||
@SuppressWarnings("rawtypes")
|
||||
final FailableBiFunction NOP = (t, u) -> null;
|
||||
|
||||
/**
|
||||
* Returns The NOP singleton.
|
||||
*
|
||||
* @param <T> Consumed type 1.
|
||||
* @param <U> Consumed type 2.
|
||||
* @param <R> Return type.
|
||||
* @param <E> Thrown exception.
|
||||
* @return The NOP singleton.
|
||||
*/
|
||||
static <T, U, R, E extends Throwable> FailableBiFunction<T, U, R, E> nop() {
|
||||
return 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 after the operation to perform after this one.
|
||||
* @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
|
||||
* @throws E Thrown when a consumer fails.
|
||||
* @throws NullPointerException if after is null.
|
||||
*/
|
||||
default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E {
|
||||
Objects.requireNonNull(after);
|
||||
return (final T t, final U u) -> after.apply(apply(t, u));
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies this function.
|
||||
*
|
||||
|
|
|
@ -30,6 +30,22 @@ import java.util.function.Function;
|
|||
@FunctionalInterface
|
||||
public interface FailableFunction<T, R, E extends Throwable> {
|
||||
|
||||
/** NOP singleton */
|
||||
@SuppressWarnings("rawtypes")
|
||||
final FailableFunction NOP = t -> null;
|
||||
|
||||
/**
|
||||
* Returns The NOP singleton.
|
||||
*
|
||||
* @param <T> Consumed type 1.
|
||||
* @param <R> Return type.
|
||||
* @param <E> Thrown exception.
|
||||
* @return The NOP singleton.
|
||||
*/
|
||||
static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
|
||||
return NOP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies this function.
|
||||
*
|
||||
|
|
|
@ -45,9 +45,6 @@ import org.junit.jupiter.api.Test;
|
|||
*/
|
||||
public class FailableFunctionsTest {
|
||||
|
||||
private static final IllegalStateException ILLEGAL_STATE_EXCEPTION = new IllegalStateException();
|
||||
private static final OutOfMemoryError ERROR = new OutOfMemoryError();
|
||||
|
||||
public static class CloseableObject {
|
||||
private boolean closed;
|
||||
|
||||
|
@ -69,7 +66,6 @@ public class FailableFunctionsTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class FailureOnOddInvocations {
|
||||
private static int invocations;
|
||||
|
||||
|
@ -268,10 +264,15 @@ public class FailableFunctionsTest {
|
|||
}
|
||||
}
|
||||
|
||||
private static final OutOfMemoryError ERROR = new OutOfMemoryError();
|
||||
|
||||
private static final IllegalStateException ILLEGAL_STATE_EXCEPTION = new IllegalStateException();
|
||||
|
||||
@Test
|
||||
void testAcceptBiConsumer() {
|
||||
final Testable<?, ?> testable = new Testable<>(null);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(Testable::test, testable, ILLEGAL_STATE_EXCEPTION));
|
||||
Throwable e = assertThrows(IllegalStateException.class,
|
||||
() -> Failable.accept(Testable::test, testable, ILLEGAL_STATE_EXCEPTION));
|
||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Failable.accept(Testable::test, testable, ERROR));
|
||||
|
@ -632,28 +633,27 @@ public class FailableFunctionsTest {
|
|||
t.setThrowable(th);
|
||||
t.test();
|
||||
};
|
||||
Throwable e;
|
||||
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> nop = FailableBiConsumer.nop();
|
||||
e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failableBiConsumer).accept(testable, ERROR));
|
||||
final Throwable e = assertThrows(OutOfMemoryError.class,
|
||||
() -> nop.andThen(failableBiConsumer).accept(testable, ERROR));
|
||||
assertSame(ERROR, e);
|
||||
// Does not throw
|
||||
nop.andThen(nop);
|
||||
// Documented in Javadoc edge-case.
|
||||
assertThrows(NullPointerException.class, () -> failableBiConsumer.andThen(null));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiFunction() {
|
||||
final IllegalStateException ise = ILLEGAL_STATE_EXCEPTION;
|
||||
final Testable<?, ?> testable = new Testable<>(ise);
|
||||
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||
final FailableBiFunction<Testable<?, ?>, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
|
||||
t.setThrowable(th);
|
||||
return Integer.valueOf(t.testAsInteger());
|
||||
return t.testAsInteger();
|
||||
};
|
||||
final BiFunction<Testable<?, ?>, Throwable, Integer> biFunction = Failable.asBiFunction(failableBiFunction);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise));
|
||||
assertSame(ise, e);
|
||||
Throwable e = assertThrows(IllegalStateException.class,
|
||||
() -> biFunction.apply(testable, ILLEGAL_STATE_EXCEPTION));
|
||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||
|
||||
testable.setThrowable(ERROR);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> biFunction.apply(testable, ERROR));
|
||||
|
@ -669,6 +669,29 @@ public class FailableFunctionsTest {
|
|||
assertEquals(0, biFunction.apply(testable, null).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiFunctionAndThen() throws IOException {
|
||||
// Unchecked usage pattern in JRE
|
||||
final BiFunction<Object, Integer, Integer> nopBiFunction = (t, u) -> null;
|
||||
final Function<Object, Integer> nopFunction = (t) -> null;
|
||||
nopBiFunction.andThen(nopFunction);
|
||||
// Checked usage pattern
|
||||
final FailableBiFunction<Object, Integer, Integer, IOException> failingBiFunctionTest = (t, u) -> {
|
||||
throw new IOException();
|
||||
};
|
||||
final FailableFunction<Object, Integer, IOException> failingFunction = (t) -> { throw new IOException(); };
|
||||
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));
|
||||
assertThrows(IOException.class, () -> failingBiFunctionTest.andThen(nopFailableFunction).apply(null, null));
|
||||
//
|
||||
assertThrows(IOException.class, () -> nopFailableBiFunction.andThen(failingFunction).apply(null, null));
|
||||
nopFailableBiFunction.andThen(nopFailableFunction).apply(null, null);
|
||||
// Documented in Javadoc edge-case.
|
||||
assertThrows(NullPointerException.class, () -> failingBiFunctionTest.andThen(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
|
||||
public void testBiPredicate() {
|
||||
|
|
Loading…
Reference in New Issue