com.github.dakusui.printables.PrintableConsumer 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.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class PrintableConsumer implements Consumer {
private final Consumer super T> consumer;
private final Supplier formatter;
public PrintableConsumer(Supplier formatter, Consumer super T> consumer) {
this.consumer = requireNonNull(consumer);
this.formatter = requireNonNull(formatter);
}
@Override
public void accept(T t) {
consumer.accept(t);
}
public Consumer andThen(Consumer super T> after) {
Objects.requireNonNull(after);
return createConsumer(
() -> format("%s;%s", formatter.get(), after),
(T t) -> {
accept(t);
after.accept(t);
}
);
}
@Override
public String toString() {
return formatter.get();
}
protected PrintableConsumer createConsumer(Supplier formatter, Consumer super T> consumer) {
return new PrintableConsumer<>(formatter, consumer);
}
public static class Builder {
private final Consumer consumer;
public Builder(Consumer predicate) {
this.consumer = requireNonNull(predicate);
}
public Consumer describe(Supplier formatter) {
return new PrintableConsumer<>(requireNonNull(formatter), consumer);
}
public Consumer describe(String description) {
return describe(() -> description);
}
}
}