
com.github.dakusui.actionunit.actions.When 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.actionunit.actions;
import com.github.dakusui.actionunit.core.Action;
import com.github.dakusui.actionunit.core.ActionSupport;
import com.github.dakusui.actionunit.core.Context;
import java.util.Formatter;
import java.util.function.Predicate;
import static java.util.Objects.requireNonNull;
public interface When extends Action {
Predicate cond();
Action perform();
Action otherwise();
@Override
default void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
default void formatTo(Formatter formatter, int flags, int width, int precision) {
formatter.format("if [%s]", cond());
}
class Builder extends Action.Builder {
private final Predicate cond;
private Action otherwise = Named.of("else", ActionSupport.nop());
private Action perform;
public Builder(Predicate cond) {
this.cond = requireNonNull(cond);
}
public Builder perform(Action perform) {
this.perform = Named.of("then", requireNonNull(perform));
return this;
}
public Action otherwise(Action otherwise) {
this.otherwise = Named.of("else", requireNonNull(otherwise));
return this.$();
}
@Override
public When build() {
requireNonNull(this.perform);
return new When() {
@Override
public Predicate cond() {
return Builder.this.cond;
}
@Override
public Action perform() {
return Builder.this.perform;
}
@Override
public Action otherwise() {
return Builder.this.otherwise;
}
};
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy