package pulley;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import pulley.actions.A0;
import pulley.functions.F;

public final class Schedulers {

    private static final Scheduler TRAMPOLINE = new SchedulerTrampoline();
    private static final Scheduler IMMEDIATE = new SchedulerImmediate();
    private static final Scheduler COMPUTATION = new SchedulerComputation();

    public static Scheduler trampoline() {
        return TRAMPOLINE;
    }

    public static Scheduler immediate() {
        return IMMEDIATE;
    }

    public static Scheduler computation() {
        return COMPUTATION;
    }

    public static <T> Result<T> getAndJoin(final Promise<T> promise) {
        final AtomicReference<Result<T>> ref = new AtomicReference<Result<T>>();
        final CountDownLatch latch = new CountDownLatch(1);
        promise.scheduler().schedule(new A0() {

            @Override
            public void call() {
                ref.set(Promises.result(promise).get());
                latch.countDown();
            }
        });
        try {
            latch.await();
            return ref.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -58,112 +58,33 @@
 ch;%0A
-import java.util.concurrent.atomic.AtomicReference;%0A%0Aimport pulley.actions.A0;%0Aimport pulley.functions.F
+%0Aimport pulley.actions.A0
 ;%0A%0Ap
@@ -611,15 +611,8 @@
  get
-AndJoin
 (fin
@@ -648,197 +648,509 @@
     
-final AtomicReference%3CResult%3CT%3E%3E ref = new AtomicReference%3CResult%3CT%3E%3E();%0A        final CountDownLatch latch = new CountDownLatch(1);%0A        promise.scheduler().schedule(new A0() %7B%0A
+PromiseGettingAction%3CT%3E action = new PromiseGettingAction%3CT%3E(promise);%0A        promise.scheduler().schedule(action);%0A        return action.get();%0A    %7D%0A%0A    private static class PromiseGettingAction%3CT%3E implements A0 %7B%0A%0A        volatile Result%3CT%3E result = Result.absent();%0A        private final Promise%3CT%3E promise;%0A        private final CountDownLatch latch;%0A%0A        PromiseGettingAction(Promise%3CT%3E promise) %7B%0A            this.promise = promise;%0A            this.latch = new CountDownLatch(1);
 %0A       
     
@@ -1145,21 +1145,28 @@
 %0A       
+ %7D%0A%0A
      
+   
 @Overrid
@@ -1167,20 +1167,16 @@
 verride%0A
-    
         
@@ -1212,20 +1212,17 @@
     
-  
-  ref.set(
+result = 
 Prom
@@ -1247,23 +1247,18 @@
 e).get()
-)
 ;%0A
-    
         
@@ -1284,29 +1284,26 @@
 ();%0A        
-    
 %7D
+%0A
 %0A        %7D);
@@ -1303,12 +1303,37 @@
     
-%7D);%0A
+public Result%3CT%3E get() %7B%0A    
     
@@ -1342,16 +1342,20 @@
   try %7B%0A
+    
         
@@ -1381,34 +1381,8 @@
     
-        return ref.get();%0A
     
@@ -1416,24 +1416,28 @@
 eption e) %7B%0A
+    
             
@@ -1475,17 +1475,59 @@
         
-%7D
+    %7D%0A            return result;%0A        %7D%0A
 %0A    %7D%0A%7D

add get for promises