com.github.dakusui.printables.PrintablePredicate 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.Predicate;
import java.util.function.Supplier;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class PrintablePredicate implements Predicate {
private final Predicate super T> predicate;
private final Supplier formatter;
public PrintablePredicate(Supplier formatter, Predicate super T> predicate) {
this.predicate = requireNonNull(predicate);
this.formatter = requireNonNull(formatter);
}
@Override
public boolean test(T t) {
return predicate.test(t);
}
@Override
public Predicate and(Predicate super T> other) {
requireNonNull(other);
return createPredicate(() -> format("(%s&&%s)", formatter.get(), other), t -> predicate.test(t) && other.test(t));
}
@Override
public Predicate negate() {
return createPredicate(() -> format("!%s", formatter.get()), predicate.negate());
}
@Override
public Predicate or(Predicate super T> other) {
requireNonNull(other);
return createPredicate(() -> format("(%s||%s)", formatter.get(), other), t -> predicate.test(t) || other.test(t));
}
@Override
public String toString() {
return formatter.get();
}
protected PrintablePredicate createPredicate(Supplier formatter, Predicate super T> predicate) {
return new PrintablePredicate<>(formatter, predicate);
}
public static class Builder {
private final Predicate predicate;
public Builder(Predicate predicate) {
this.predicate = requireNonNull(predicate);
}
public Predicate describe(Supplier formatter) {
return new PrintablePredicate<>(requireNonNull(formatter), predicate);
}
public Predicate describe(String description) {
return describe(() -> description);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy