// Path: src/main/java/us/monoid/psql/async/promise/Promise.java
// abstract class DoneCallback<V> {
//   public abstract void onFulfilled(V value);
// 
//   public void onRejected(Throwable reason) {
//     Promises.propagate(reason);
//   }
// }


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import us.monoid.psql.async.promise.Promise.DoneCallback;
package us.monoid.psql.async.promise;



/** Composing promises etc.
 * 
 * @author beders
 * @author tbroyer
 */
public final class Promises {

  /**
   * This is the same as Guava's Throwables#propagate, but we don't want a
   * mandatory dependency on Guava.
   */
  static RuntimeException propagate(Throwable t) {
    if (t instanceof RuntimeException) {
      throw (RuntimeException) t;
    }
    if (t instanceof Error) {
      throw (Error) t;
    }
    throw new RuntimeException(t);
  }

  public static <V> Promise<V> fulfilled(final V value) {
    return new Promise<V>() {
      @Override
      public <R> Promise<R> then(Callback<? super V, R> callback) {
        try {
          return callback.onFulfilled(value);
        } catch (Throwable t) {
          return rejected(t);
        }
      }
      @Override
      public <R> Promise<R> then(ImmediateCallback<? super V, R> callback) {
        try {
          return fulfilled(callback.onFulfilled(value));
        } catch (Throwable t) {
          return rejected(t);
        }
      }
      @Override
// Path: src/main/java/us/monoid/psql/async/promise/Promise.java
// abstract class DoneCallback<V> {
//   public abstract void onFulfilled(V value);
// 
//   public void onRejected(Throwable reason) {
//     Promises.propagate(reason);
//   }
// }


// Path: src/main/java/us/monoid/psql/async/promise/Promises.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import us.monoid.psql.async.promise.Promise.DoneCallback;

package us.monoid.psql.async.promise;



/** Composing promises etc.
 * 
 * @author beders
 * @author tbroyer
 */
public final class Promises {

  /**
   * This is the same as Guava's Throwables#propagate, but we don't want a
   * mandatory dependency on Guava.
   */
  static RuntimeException propagate(Throwable t) {
    if (t instanceof RuntimeException) {
      throw (RuntimeException) t;
    }
    if (t instanceof Error) {
      throw (Error) t;
    }
    throw new RuntimeException(t);
  }

  public static <V> Promise<V> fulfilled(final V value) {
    return new Promise<V>() {
      @Override
      public <R> Promise<R> then(Callback<? super V, R> callback) {
        try {
          return callback.onFulfilled(value);
        } catch (Throwable t) {
          return rejected(t);
        }
      }
      @Override
      public <R> Promise<R> then(ImmediateCallback<? super V, R> callback) {
        try {
          return fulfilled(callback.onFulfilled(value));
        } catch (Throwable t) {
          return rejected(t);
        }
      }
      @Override
      public void done(DoneCallback<? super V> callback) {