All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dakusui.actionunit.actions.ForEach Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
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;
        }
      };
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy