Attempt.java

  1. package com.github.dakusui.actionunit.actions;

  2. import com.github.dakusui.actionunit.core.Action;
  3. import com.github.dakusui.actionunit.core.ActionSupport;
  4. import com.github.dakusui.actionunit.core.Context;
  5. import com.github.dakusui.actionunit.exceptions.ActionException;

  6. import java.util.Formatter;
  7. import java.util.function.Consumer;

  8. import static com.github.dakusui.actionunit.core.ActionSupport.leaf;
  9. import static com.github.dakusui.actionunit.core.ActionSupport.named;
  10. import static java.util.Objects.requireNonNull;

  11. public interface Attempt extends Action {
  12.   @Override
  13.   default void accept(Visitor visitor) {
  14.     visitor.visit(this);
  15.   }

  16.   Action perform();

  17.   Action recover();

  18.   Action ensure();

  19.   Class<? extends Throwable> targetExceptionClass();

  20.   @Override
  21.   default void formatTo(Formatter formatter, int flags, int width, int precision) {
  22.     formatter.format("attempt");
  23.   }

  24.   class Builder extends Action.Builder<Attempt> {
  25.     private Class<? extends Throwable> targetExceptionClass = Exception.class;
  26.     private final Action perform;
  27.     private Action recover = Named.of("recover",
  28.         Leaf.of(
  29.             (Context $) -> {
  30.               if ($.wasExceptionThrown()) {
  31.                 throw ActionException.wrap($.thrownException());
  32.               }
  33.             }));
  34.     private Action ensure = Named.of("ensure",
  35.         ActionSupport.nop()
  36.     );

  37.     public Builder(Action perform) {
  38.       this.perform = requireNonNull(perform);
  39.     }

  40.     public Builder recover(Class<? extends Throwable> targetExceptionClass, Consumer<Throwable> exceptionHandler) {
  41.       this.recover = Named.of("recover", leaf(c -> {
  42.         exceptionHandler.accept(c.thrownException());
  43.       }));
  44.       this.targetExceptionClass = requireNonNull(targetExceptionClass);
  45.       return this;
  46.     }

  47.     public Builder recover(Class<? extends Throwable> targetExceptionClass, Action recover) {
  48.       this.recover = Named.of("recover", requireNonNull(recover));
  49.       this.targetExceptionClass = requireNonNull(targetExceptionClass);
  50.       return this;
  51.     }

  52.     public Action ensure(Action ensure) {
  53.       this.ensure = named("ensure", requireNonNull(ensure));
  54.       return this.$();
  55.     }

  56.     public Attempt build() {
  57.       return new Attempt() {

  58.         @Override
  59.         public Action perform() {
  60.           return perform;
  61.         }

  62.         @Override
  63.         public Action recover() {
  64.           return recover;
  65.         }

  66.         public Class<? extends Throwable> targetExceptionClass() {
  67.           return targetExceptionClass;
  68.         }

  69.         @Override
  70.         public Action ensure() {
  71.           return ensure;
  72.         }
  73.       };
  74.     }
  75.   }
  76. }