com.github.dakusui.actionunit.actions.ForEach 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.StreamGenerator;
import java.util.Formatter;
import static java.util.Objects.requireNonNull;
public interface ForEach extends Action {
String loopVariableName();
StreamGenerator data();
Action perform();
boolean isParallel();
default void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
default void formatTo(Formatter formatter, int flags, int width, int precision) {
formatter.format("for each of %s %s", data(), isParallel() ? "parallely" : "sequentially");
}
class Builder extends Action.Builder> {
private final StreamGenerator streamGenerator;
private final String loopVariableName;
private Action perform = ActionSupport.nop();
private boolean parallel;
public Builder(String loopVariableName, StreamGenerator streamGenerator) {
this.loopVariableName = requireNonNull(loopVariableName);
this.streamGenerator = requireNonNull(streamGenerator);
this.sequentially();
}
public Action perform(Action perform) {
this.perform = requireNonNull(perform);
return this.$();
}
public Builder parallelly() {
this.parallel = true;
return this;
}
public Builder sequentially() {
this.parallel = false;
return this;
}
public ForEach build() {
return new ForEach() {
@Override
public String loopVariableName() {
return Builder.this.loopVariableName;
}
@Override
public StreamGenerator data() {
return Builder.this.streamGenerator;
}
@Override
public Action perform() {
return Builder.this.perform;
}
@Override
public boolean isParallel() {
return Builder.this.parallel;
}
};
}
}
}