[LANG-1568]
org.apache.commons.lang3.function.FailableConsumer.andThen(FailableConsumer<? super T, E>)
This commit is contained in:
parent
c7aea90a7a
commit
47fb776792
|
@ -57,7 +57,7 @@ public interface FailableBiFunction<T, U, R, E extends Throwable> {
|
||||||
* @param after the operation to perform after this one.
|
* @param after the operation to perform after this one.
|
||||||
* @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
|
* @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
|
||||||
* @throws E Thrown when a consumer fails.
|
* @throws E Thrown when a consumer fails.
|
||||||
* @throws NullPointerException if after is null.
|
* @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 {
|
default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E {
|
||||||
Objects.requireNonNull(after);
|
Objects.requireNonNull(after);
|
||||||
|
|
|
@ -17,17 +17,33 @@
|
||||||
|
|
||||||
package org.apache.commons.lang3.function;
|
package org.apache.commons.lang3.function;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A functional interface like {@link Consumer} that declares a {@code Throwable}.
|
* A functional interface like {@link Consumer} that declares a {@code Throwable}.
|
||||||
*
|
*
|
||||||
* @param <O> Consumed type 1.
|
* @param <T> Consumed type 1.
|
||||||
* @param <E> Thrown exception.
|
* @param <E> Thrown exception.
|
||||||
* @since 3.11
|
* @since 3.11
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface FailableConsumer<O, E extends Throwable> {
|
public interface FailableConsumer<T, E extends Throwable> {
|
||||||
|
|
||||||
|
/** NOP singleton */
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
final FailableConsumer NOP = t -> {/* NOP */};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns The NOP singleton.
|
||||||
|
*
|
||||||
|
* @param <T> Consumed type 1.
|
||||||
|
* @param <E> Thrown exception.
|
||||||
|
* @return The NOP singleton.
|
||||||
|
*/
|
||||||
|
static <T, E extends Throwable> FailableConsumer<T, E> nop() {
|
||||||
|
return NOP;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts the consumer.
|
* Accepts the consumer.
|
||||||
|
@ -35,5 +51,21 @@ public interface FailableConsumer<O, E extends Throwable> {
|
||||||
* @param object the parameter for the consumable to accept
|
* @param object the parameter for the consumable to accept
|
||||||
* @throws E Thrown when the consumer fails.
|
* @throws E Thrown when the consumer fails.
|
||||||
*/
|
*/
|
||||||
void accept(O object) throws E;
|
void accept(T object) throws E;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}.
|
||||||
|
*
|
||||||
|
* @param after the operation to perform after this operation
|
||||||
|
* @return a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}.
|
||||||
|
* @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);
|
||||||
|
return (final T t) -> {
|
||||||
|
accept(t);
|
||||||
|
after.accept(t);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,7 +269,7 @@ public class FailableFunctionsTest {
|
||||||
private static final IllegalStateException ILLEGAL_STATE_EXCEPTION = new IllegalStateException();
|
private static final IllegalStateException ILLEGAL_STATE_EXCEPTION = new IllegalStateException();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptBiConsumer() {
|
public void testAcceptBiConsumer() {
|
||||||
final Testable<?, ?> testable = new Testable<>(null);
|
final Testable<?, ?> testable = new Testable<>(null);
|
||||||
Throwable e = assertThrows(IllegalStateException.class,
|
Throwable e = assertThrows(IllegalStateException.class,
|
||||||
() -> Failable.accept(Testable::test, testable, ILLEGAL_STATE_EXCEPTION));
|
() -> Failable.accept(Testable::test, testable, ILLEGAL_STATE_EXCEPTION));
|
||||||
|
@ -290,7 +290,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptConsumer() {
|
public void testAcceptConsumer() {
|
||||||
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(Testable::test, testable));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(Testable::test, testable));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -311,7 +311,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptDoubleConsumer() {
|
public void testAcceptDoubleConsumer() {
|
||||||
final Testable<?, Double> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<?, Double> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testDouble, 1d));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testDouble, 1d));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -336,7 +336,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptIntConsumer() {
|
public void testAcceptIntConsumer() {
|
||||||
final Testable<?, Integer> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<?, Integer> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testInt, 1));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testInt, 1));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -361,7 +361,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptLongConsumer() {
|
public void testAcceptLongConsumer() {
|
||||||
final Testable<?, Long> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<?, Long> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testLong, 1L));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testLong, 1L));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -386,7 +386,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptObjDoubleConsumer() {
|
public void testAcceptObjDoubleConsumer() {
|
||||||
final Testable<String, Double> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<String, Double> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class,
|
Throwable e = assertThrows(IllegalStateException.class,
|
||||||
() -> Failable.accept(testable::testObjDouble, "X", 1d));
|
() -> Failable.accept(testable::testObjDouble, "X", 1d));
|
||||||
|
@ -416,7 +416,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptObjIntConsumer() {
|
public void testAcceptObjIntConsumer() {
|
||||||
final Testable<String, Integer> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<String, Integer> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testObjInt, "X", 1));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testObjInt, "X", 1));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -445,7 +445,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptObjLongConsumer() {
|
public void testAcceptObjLongConsumer() {
|
||||||
final Testable<String, Long> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<String, Long> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testObjLong, "X", 1L));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Failable.accept(testable::testObjLong, "X", 1L));
|
||||||
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
assertSame(ILLEGAL_STATE_EXCEPTION, e);
|
||||||
|
@ -531,7 +531,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAsCallable() {
|
public void testAsCallable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
|
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
|
||||||
final Callable<FailureOnOddInvocations> callable = Failable.asCallable(failableCallable);
|
final Callable<FailureOnOddInvocations> callable = Failable.asCallable(failableCallable);
|
||||||
|
@ -550,7 +550,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAsConsumer() {
|
public void testAsConsumer() {
|
||||||
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
|
||||||
final Consumer<Testable<?, ?>> consumer = Failable.asConsumer(Testable::test);
|
final Consumer<Testable<?, ?>> consumer = Failable.asConsumer(Testable::test);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable));
|
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable));
|
||||||
|
@ -572,7 +572,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAsRunnable() {
|
public void testAsRunnable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final Runnable runnable = Failable.asRunnable(FailureOnOddInvocations::new);
|
final Runnable runnable = Failable.asRunnable(FailureOnOddInvocations::new);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, runnable::run);
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, runnable::run);
|
||||||
|
@ -599,7 +599,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBiConsumer() throws Throwable {
|
public void testBiConsumer() throws Throwable {
|
||||||
final Testable<?, ?> testable = new Testable<>(null);
|
final Testable<?, ?> testable = new Testable<>(null);
|
||||||
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
||||||
t.setThrowable(th);
|
t.setThrowable(th);
|
||||||
|
@ -627,7 +627,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBiConsumerAndThen() throws Throwable {
|
public void testBiConsumerAndThen() throws Throwable {
|
||||||
final Testable<?, ?> testable = new Testable<>(null);
|
final Testable<?, ?> testable = new Testable<>(null);
|
||||||
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
||||||
t.setThrowable(th);
|
t.setThrowable(th);
|
||||||
|
@ -710,7 +710,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCallable() {
|
public void testCallable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class,
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class,
|
||||||
() -> Failable.run(FailureOnOddInvocations::new));
|
() -> Failable.run(FailureOnOddInvocations::new));
|
||||||
|
@ -729,6 +729,23 @@ public class FailableFunctionsTest {
|
||||||
new Functions();
|
new Functions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsumerAndThen() throws Throwable {
|
||||||
|
final Testable<?, ?> testable = new Testable<>(null);
|
||||||
|
final FailableConsumer<Throwable, Throwable> failableConsumer = (th) -> {
|
||||||
|
testable.setThrowable(th);
|
||||||
|
testable.test();
|
||||||
|
};
|
||||||
|
final FailableConsumer<Throwable, Throwable> nop = FailableConsumer.nop();
|
||||||
|
final Throwable e = assertThrows(OutOfMemoryError.class,
|
||||||
|
() -> nop.andThen(failableConsumer).accept(ERROR));
|
||||||
|
assertSame(ERROR, e);
|
||||||
|
// Does not throw
|
||||||
|
nop.andThen(nop);
|
||||||
|
// Documented in Javadoc edge-case.
|
||||||
|
assertThrows(NullPointerException.class, () -> failableConsumer.andThen(null));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoublePredicate() throws Throwable {
|
public void testDoublePredicate() throws Throwable {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
|
@ -920,7 +937,7 @@ public class FailableFunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRunnable() {
|
public void testRunnable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class,
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class,
|
||||||
() -> Failable.run(FailureOnOddInvocations::new));
|
() -> Failable.run(FailureOnOddInvocations::new));
|
||||||
|
@ -938,7 +955,7 @@ public class FailableFunctionsTest {
|
||||||
* Object and Throwable.
|
* Object and Throwable.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiConsumer_Object_Throwable() {
|
public void testThrows_FailableBiConsumer_Object_Throwable() {
|
||||||
new FailableBiConsumer<Object, Object, Throwable>() {
|
new FailableBiConsumer<Object, Object, Throwable>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -953,7 +970,7 @@ public class FailableFunctionsTest {
|
||||||
* generic test types.
|
* generic test types.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiConsumer_String_IOException() {
|
public void testThrows_FailableBiConsumer_String_IOException() {
|
||||||
new FailableBiConsumer<String, String, IOException>() {
|
new FailableBiConsumer<String, String, IOException>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -969,7 +986,7 @@ public class FailableFunctionsTest {
|
||||||
* Object and Throwable.
|
* Object and Throwable.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiFunction_Object_Throwable() {
|
public void testThrows_FailableBiFunction_Object_Throwable() {
|
||||||
new FailableBiFunction<Object, Object, Object, Throwable>() {
|
new FailableBiFunction<Object, Object, Object, Throwable>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -984,7 +1001,7 @@ public class FailableFunctionsTest {
|
||||||
* generic test types.
|
* generic test types.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiFunction_String_IOException() {
|
public void testThrows_FailableBiFunction_String_IOException() {
|
||||||
new FailableBiFunction<String, String, String, IOException>() {
|
new FailableBiFunction<String, String, String, IOException>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -999,7 +1016,7 @@ public class FailableFunctionsTest {
|
||||||
* Object and Throwable.
|
* Object and Throwable.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiPredicate_Object_Throwable() {
|
public void testThrows_FailableBiPredicate_Object_Throwable() {
|
||||||
new FailableBiPredicate<Object, Object, Throwable>() {
|
new FailableBiPredicate<Object, Object, Throwable>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1014,7 +1031,7 @@ public class FailableFunctionsTest {
|
||||||
* generic test types.
|
* generic test types.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableBiPredicate_String_IOException() {
|
public void testThrows_FailableBiPredicate_String_IOException() {
|
||||||
new FailableBiPredicate<String, String, IOException>() {
|
new FailableBiPredicate<String, String, IOException>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1061,7 +1078,7 @@ public class FailableFunctionsTest {
|
||||||
* Object and Throwable.
|
* Object and Throwable.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableCallable_Object_Throwable() {
|
public void testThrows_FailableCallable_Object_Throwable() {
|
||||||
new FailableCallable<Object, Throwable>() {
|
new FailableCallable<Object, Throwable>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1076,7 +1093,7 @@ public class FailableFunctionsTest {
|
||||||
* generic test types.
|
* generic test types.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableCallable_String_IOException() {
|
public void testThrows_FailableCallable_String_IOException() {
|
||||||
new FailableCallable<String, IOException>() {
|
new FailableCallable<String, IOException>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1091,7 +1108,7 @@ public class FailableFunctionsTest {
|
||||||
* Object and Throwable.
|
* Object and Throwable.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableConsumer_Object_Throwable() {
|
public void testThrows_FailableConsumer_Object_Throwable() {
|
||||||
new FailableConsumer<Object, Throwable>() {
|
new FailableConsumer<Object, Throwable>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1107,7 +1124,7 @@ public class FailableFunctionsTest {
|
||||||
* generic test types.
|
* generic test types.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testThrows_FailableConsumer_String_IOException() {
|
public void testThrows_FailableConsumer_String_IOException() {
|
||||||
new FailableConsumer<String, IOException>() {
|
new FailableConsumer<String, IOException>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue