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

com.github.dakusui.actionunit.actions.When 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.ContextPredicate;

import java.util.Formatter;

import static java.util.Objects.requireNonNull;

public interface When extends Action {
  ContextPredicate 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] is satisfied", cond());
  }

  class Builder extends Action.Builder {
    private final ContextPredicate cond;

    private Action otherwise = Named.of("else", ActionSupport.nop());
    private Action perform;

    public Builder(ContextPredicate 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 ContextPredicate cond() {
          return Builder.this.cond;
        }

        @Override
        public Action perform() {
          return Builder.this.perform;
        }

        @Override
        public Action otherwise() {
          return Builder.this.otherwise;
        }
      };
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy