com.link_intersystems.util.function.FunctionExecutorAdapters Maven / Gradle / Ivy
package com.link_intersystems.util.function;
import java.util.function.*;
/**
* Provides utility methods to adapt {@link Function}, {@link Consumer}, {@link BiConsumer} and {@link Runnable}s
* to {@link FunctionExecutor#exec(Runnable)} calls.
*/
public class FunctionExecutorAdapters {
private FunctionExecutor functionExecutor;
public FunctionExecutorAdapters(FunctionExecutor functionExecutor) {
this.functionExecutor = functionExecutor;
}
public Function adapter(Function function) {
return t -> {
FunctionRunnable functionRunnable = new FunctionRunnable<>(function, t);
functionExecutor.exec(functionRunnable);
return functionRunnable.getResult();
};
}
public BiFunction adapter(BiFunction function) {
return (t, u) -> {
BiFunctionRunnable functionRunnable = new BiFunctionRunnable<>(function, t, u);
functionExecutor.exec(functionRunnable);
return functionRunnable.getResult();
};
}
public BiConsumer adapter(BiConsumer consumer) {
return (t, u) -> functionExecutor.exec(consumer, t, u);
}
public Consumer adapter(BiConsumer consumer, U arg) {
return adapter(consumer, (Supplier) () -> arg);
}
public Consumer adapter(BiConsumer consumer, Supplier argSupplier) {
return t -> functionExecutor.exec(consumer, t, argSupplier.get());
}
public Consumer adapter(Consumer consumer) {
return t -> functionExecutor.exec(consumer, t);
}
public Runnable adapter(Runnable runnable) {
return () -> functionExecutor.exec(runnable::run);
}
}