com.link_intersystems.util.function.FunctionExecutorAdapters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lis-commons-util Show documentation
Show all versions of lis-commons-util Show documentation
Link Intersystems Commons Util (lis-commons-util) is collection of reusable software components
that extend the standard java.util components.
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);
}
}