// Path: src/main/java/org/simondean/vertx/async/DefaultAsyncResult.java
// public class DefaultAsyncResult<T> implements AsyncResult<T> {
//   private Throwable cause;
//   private T result;
// 
//   public DefaultAsyncResult(Throwable cause, T result) {
//     this.cause = cause;
//     this.result = result;
//   }
// 
//   public static <T> AsyncResult<T> succeed(T result) {
//     return new DefaultAsyncResult<>(null, result);
//   }
// 
//   public static AsyncResult<Void> succeed() {
//     return succeed(null);
//   }
// 
//   public static <T> AsyncResult<T> fail(Throwable cause) {
//     if (cause == null) {
//       throw new IllegalArgumentException("cause argument cannot be null");
//     }
// 
//     return new DefaultAsyncResult<>(cause, null);
//   }
// 
//   public static <T> AsyncResult<T> fail(AsyncResult<?> result) {
//     return fail(result.cause());
//   }
// 
//   @Override
//   public T result() {
//     return result;
//   }
// 
//   @Override
//   public Throwable cause() {
//     return cause;
//   }
// 
//   @Override
//   public boolean succeeded() {
//     return cause == null;
//   }
// 
//   @Override
//   public boolean failed() {
//     return cause != null;
//   }
// }
// 
// Path: src/main/java/org/simondean/vertx/async/Waterfall.java
// public interface Waterfall<T> {
//   public <R> Waterfall<R> task(BiConsumer<T, AsyncResultHandler<R>> task);
// 
//   public void run(AsyncResultHandler<T> handler);
// }


import org.simondean.vertx.async.DefaultAsyncResult;
import org.simondean.vertx.async.Waterfall;
import org.vertx.java.core.AsyncResultHandler;
import java.util.function.BiConsumer;
package org.simondean.vertx.async.internal;



public class NestedWaterfall<T, R> implements Waterfall<R> {
  private final Waterfall<T> parentWaterfall;
  private BiConsumer<T, AsyncResultHandler<R>> task;

  public NestedWaterfall(Waterfall<T> parentWaterfall, BiConsumer<T, AsyncResultHandler<R>> task) {
    this.parentWaterfall = parentWaterfall;
    this.task = task;
  }

  @Override
  public <S> Waterfall<S> task(BiConsumer<R, AsyncResultHandler<S>> task) {
    return new NestedWaterfall<>(this, task);
  }

  @Override
  public void run(AsyncResultHandler<R> handler) {
    parentWaterfall.run(result -> {
      if (result.failed()) {
// Path: src/main/java/org/simondean/vertx/async/DefaultAsyncResult.java
// public class DefaultAsyncResult<T> implements AsyncResult<T> {
//   private Throwable cause;
//   private T result;
// 
//   public DefaultAsyncResult(Throwable cause, T result) {
//     this.cause = cause;
//     this.result = result;
//   }
// 
//   public static <T> AsyncResult<T> succeed(T result) {
//     return new DefaultAsyncResult<>(null, result);
//   }
// 
//   public static AsyncResult<Void> succeed() {
//     return succeed(null);
//   }
// 
//   public static <T> AsyncResult<T> fail(Throwable cause) {
//     if (cause == null) {
//       throw new IllegalArgumentException("cause argument cannot be null");
//     }
// 
//     return new DefaultAsyncResult<>(cause, null);
//   }
// 
//   public static <T> AsyncResult<T> fail(AsyncResult<?> result) {
//     return fail(result.cause());
//   }
// 
//   @Override
//   public T result() {
//     return result;
//   }
// 
//   @Override
//   public Throwable cause() {
//     return cause;
//   }
// 
//   @Override
//   public boolean succeeded() {
//     return cause == null;
//   }
// 
//   @Override
//   public boolean failed() {
//     return cause != null;
//   }
// }
// 
// Path: src/main/java/org/simondean/vertx/async/Waterfall.java
// public interface Waterfall<T> {
//   public <R> Waterfall<R> task(BiConsumer<T, AsyncResultHandler<R>> task);
// 
//   public void run(AsyncResultHandler<T> handler);
// }


// Path: src/main/java/org/simondean/vertx/async/internal/NestedWaterfall.java
import org.simondean.vertx.async.DefaultAsyncResult;
import org.simondean.vertx.async.Waterfall;
import org.vertx.java.core.AsyncResultHandler;
import java.util.function.BiConsumer;

package org.simondean.vertx.async.internal;



public class NestedWaterfall<T, R> implements Waterfall<R> {
  private final Waterfall<T> parentWaterfall;
  private BiConsumer<T, AsyncResultHandler<R>> task;

  public NestedWaterfall(Waterfall<T> parentWaterfall, BiConsumer<T, AsyncResultHandler<R>> task) {
    this.parentWaterfall = parentWaterfall;
    this.task = task;
  }

  @Override
  public <S> Waterfall<S> task(BiConsumer<R, AsyncResultHandler<S>> task) {
    return new NestedWaterfall<>(this, task);
  }

  @Override
  public void run(AsyncResultHandler<R> handler) {
    parentWaterfall.run(result -> {
      if (result.failed()) {
        handler.handle(DefaultAsyncResult.fail(result));