nbbrd.io.function.IOBiConsumer Maven / Gradle / Ivy
package nbbrd.io.function;
import internal.io.JdkWithIO;
import lombok.NonNull;
import nbbrd.design.StaticFactoryMethod;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.BiConsumer;
/**
* Represents an operation that accepts two input arguments and returns no
* result. This is the two-arity specialization of {@link IOConsumer}.
* Unlike most other functional interfaces, {@code IOBiConsumer} is expected
* to operate via side effects.
*
* This is a functional interface
* whose functional method is {@link #acceptWithIO(Object, Object)}.
*
* @param the type of the first argument to the operation
* @param the type of the second argument to the operation
* @see IOConsumer
*/
@FunctionalInterface
public interface IOBiConsumer {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
* @throws java.io.IOException if an I/O error occurs
*/
@JdkWithIO
void acceptWithIO(T t, U u) throws IOException;
/**
* Returns a composed {@code BiConsumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code BiConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
@JdkWithIO
default @NonNull IOBiConsumer andThen(@NonNull IOBiConsumer super T, ? super U> after) {
return (l, r) -> {
acceptWithIO(l, r);
after.acceptWithIO(l, r);
};
}
default @NonNull BiConsumer asUnchecked() {
return (T t, U u) -> {
try {
acceptWithIO(t, u);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
static @NonNull BiConsumer unchecked(@NonNull IOBiConsumer o) {
return o.asUnchecked();
}
@StaticFactoryMethod
static @NonNull IOBiConsumer checked(@NonNull BiConsumer consumer) {
return (t, u) -> {
try {
consumer.accept(t, u);
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
};
}
@StaticFactoryMethod
static @NonNull IOBiConsumer noOp() {
return (t, u) -> {
};
}
}