com.github.dakusui.printables.PrintableFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of actionunit Show documentation
Show all versions of actionunit Show documentation
A library to build 'action' structure for testing
package com.github.dakusui.printables;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
public class PrintableFunction implements Function {
private final Function super T, ? extends R> function;
private final Supplier formatter;
public PrintableFunction(Supplier formatter, Function super T, ? extends R> function) {
this.formatter = requireNonNull(formatter);
this.function = requireNonNull(function);
}
@Override
public R apply(T t) {
return this.function.apply(t);
}
public Function compose(Function super V, ? extends T> before) {
requireNonNull(before);
return new PrintableFunction<>(() -> String.format("%s(%s)", formatter.get(), before), this.function.compose(before));
}
public Function andThen(Function super R, ? extends V> after) {
requireNonNull(after);
return new PrintableFunction<>(() -> String.format("%s(%s)", after, formatter.get()), this.function.andThen(after));
}
protected Supplier getFormatter() {
return formatter;
}
protected Function super T, ? extends R> getFunction() {
return this.function;
}
@Override
public String toString() {
return formatter.get();
}
public static class Builder {
private final Function function;
public Builder(Function function) {
this.function = requireNonNull(function);
}
public Function describe(Supplier formatter) {
return new PrintableFunction<>(requireNonNull(formatter), function);
}
public Function describe(String description) {
return describe(() -> description);
}
}
}