package org.jpromise;

import java.util.concurrent.*;

class FuturePromise<V> extends AbstractPromise<V> implements Runnable {
    private final Future<V> future;
    private final Long timeout;
    private final TimeUnit timeUnit;

    FuturePromise(Executor executor, Future<V> future) {
        this.future = future;
        this.timeout = null;
        this.timeUnit = null;
        start(executor);
    }

    FuturePromise(Executor executor, Future<V> future, long timeout, TimeUnit timeUnit) {
        this.future = future;
        this.timeout = timeout;
        this.timeUnit = timeUnit;
        start(executor);
    }

    private void start(Executor executor) {
        if (future.isDone()) {
            this.run();
        }
        else {
            executor.execute(this);
        }
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning)
                && completeWithException(new CancellationException());
    }

    @Override
    public void run() {
        try {
            if (timeout == null) {
                complete(future.get());
            }
            else {
                complete(future.get(timeout, timeUnit));
            }
        }
        catch (ExecutionException exception) {
            Throwable cause = exception.getCause();
            if (cause == null) {
                cause = exception;
            }
            completeWithException(cause);
        }
        catch (Throwable exception) {
            completeWithException(exception);
        }
    }
}

@@ -101,28 +101,8 @@
 e%3CV%3E
- implements Runnable
  %7B%0A 
@@ -642,24 +642,94 @@
 executor) %7B%0A
+        FuturePromiseRunnable runnable = new FuturePromiseRunnable();%0A
         if (
@@ -759,20 +759,24 @@
         
-this
+runnable
 .run();%0A
@@ -829,20 +829,24 @@
 execute(
-this
+runnable
 );%0A     
@@ -1052,32 +1052,98 @@
 tion());%0A    %7D%0A%0A
+    private class FuturePromiseRunnable implements Runnable %7B%0A    
     @Override%0A  
@@ -1140,24 +1140,28 @@
 verride%0A    
+ 
+   
 public void 
@@ -1176,16 +1176,20 @@
         
+    
 try %7B%0A  
@@ -1194,24 +1194,28 @@
             
+  
+  
 if (timeout 
@@ -1217,32 +1217,36 @@
 eout == null) %7B%0A
+    
                 
@@ -1265,24 +1265,28 @@
 ure.get());%0A
+    
             
@@ -1291,39 +1291,47 @@
   %7D%0A            
+    
 else %7B%0A
+    
                 
@@ -1383,26 +1383,32 @@
         
+ 
-%7D%0A
+   %7D%0A  
         
 %7D%0A      
@@ -1395,34 +1395,39 @@
 %7D%0A          
-%7D%0A
+  %7D%0A   
         
 catch (Execu
@@ -1410,24 +1410,25 @@
 %0A           
+ 
 catch (Execu
@@ -1446,32 +1446,36 @@
 on exception) %7B%0A
+    
             Thro
@@ -1506,24 +1506,28 @@
 getCause();%0A
+    
             
@@ -1563,16 +1563,20 @@
         
+    
 cause = 
@@ -1590,34 +1590,40 @@
 on;%0A            
-%7D%0A
+    %7D%0A  
             comp
@@ -1610,32 +1610,34 @@
 %7D%0A              
+  
 completeWithExce
@@ -1658,26 +1658,33 @@
         
-%7D%0A
+    %7D%0A   
         
 catch (T
@@ -1671,24 +1671,25 @@
 %0A           
+ 
 catch (Throw
@@ -1698,32 +1698,36 @@
 le exception) %7B%0A
+    
             comp
@@ -1756,16 +1756,30 @@
 ption);%0A
+            %7D%0A
         

Change FuturePromise to no longer be a Runnable to prevent any accidental casting and running