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

com.github.dakusui.actionunit.actions.Composite 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 java.util.Collections;
import java.util.Formatter;
import java.util.List;

import static java.util.Objects.requireNonNull;

public interface Composite extends Action {
  List children();

  boolean isParallel();

  @Override
  default void formatTo(Formatter formatter, int flags, int width, int precision) {
    formatter.format(
        "do %s",
        isParallel()
            ? "parallelly"
            : "sequentially"
    );
  }

  class Builder {
    private       boolean      parallel;
    private final List actions;

    public Builder(List actions) {
      this.actions = actions;
      this.sequential();
    }

    public Builder parallel() {
      this.parallel = true;
      return this;
    }

    public Builder sequential() {
      this.parallel = false;
      return this;
    }

    @SuppressWarnings("unchecked")
    public Composite build() {
      return new Impl(actions, parallel);
    }
  }

  class Impl implements Composite {
    private final List actions;
    private final boolean      parallel;

    protected Impl(List actions, boolean parallel) {
      this.actions = requireNonNull(actions);
      this.parallel = parallel;
    }

    @Override
    public List children() {
      return Collections.unmodifiableList(actions);
    }

    @Override
    public void accept(Visitor visitor) {
      visitor.visit(this);
    }

    @Override
    public boolean isParallel() {
      return this.parallel;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy