/*
 * The MIT License (MIT)
 *
 * Copyright © 2015 John H. Uckele
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright
 * notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package io.vivarium.util.concurrency;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * SimpleFuture provides a simple implementation of the Future interface, adding a put(T value) method and allowing
 * threads to wait for the values.
 *
 * @author juckele
 *
 * @param <T>
 */
public class SimpleFuture<T> implements Future<T>
{
    private volatile T _value;
    private volatile boolean _done = false;

    private synchronized T getValue()
    {
        return _value;
    }

    private synchronized void setValue(T value)
    {
        _value = value;
    }

    private synchronized boolean getDone()
    {
        return _done;
    }

    private synchronized void setDone(boolean done)
    {
        _done = done;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning)
    {
        // SimpleFuture does not know how to cancel tasks and always fails to cancel.
        return false;
    }

    @Override
    public boolean isCancelled()
    {
        // SimpleFuture cannot be cancelled, so is never cancelled.
        return false;
    }

    @Override
    public boolean isDone()
    {
        return getDone();
    }

    public void put(T value)
    {
        setValue(value);
        setDone(true);
    }

    @Override
    public T get() throws InterruptedException, ExecutionException
    {
        while (!getDone())
        {
            Thread.sleep(0);
        }
        return getValue();
    }

    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
    {
        long timeoutInMS = unit.toMillis(timeout);
        long startWaitTime = System.currentTimeMillis();
        while (!getDone() && startWaitTime + timeoutInMS < System.currentTimeMillis())
        {
            Thread.sleep(0);
        }
        if (getDone())
        {
            return getValue();
        }
        else
        {
            throw new TimeoutException("Timed out waiting for future. Timeout of " + timeout + " " + unit);
        }
    }
}

@@ -1734,253 +1734,153 @@
     
-private synchronized void setValue(T value)%0A    %7B%0A        _value = value;%0A    %7D%0A%0A    private synchronized boolean getDone()%0A    %7B%0A        return _done;%0A    %7D%0A%0A    private synchronized void setDone(boolean done)%0A    %7B%0A        _done = done;
+/**%0A     * GenericFuture objects cannot be cancelled. Calling this method returns false, because the future will not be%0A     * cancelled.
 %0A    
-%7D%0A
+ */
 %0A   
@@ -2060,32 +2060,177 @@
 n false;%0A    %7D%0A%0A
+    /**%0A     * GenericFuture objects cannot be cancelled. Calling this method returns false, because the future is not%0A     * cancelled.%0A     */%0A
     @Override%0A  
@@ -2380,32 +2380,45 @@
 ride%0A    public 
+synchronized 
 boolean isDone()
@@ -2443,36 +2443,306 @@
 urn 
-getDone();%0A    %7D%0A%0A    public
+_done;%0A    %7D%0A%0A    /**%0A     * The SimpleFuture is an externally controlled future. The SimpleFuture is completed by calling put, thus providing%0A     * the value to any other waiting threads.%0A     *%0A     * @param value%0A     *            The value to set the future to.%0A     */%0A    public synchronized
  voi
@@ -2774,23 +2774,22 @@
     
-setV
+_v
 alue
-(
+ = 
 value
-)
 ;%0A  
@@ -2798,21 +2798,20 @@
     
-setDone(
+_done = 
 true
-)
 ;%0A  
@@ -2914,27 +2914,26 @@
     while (!
-get
+is
 Done())%0A    
@@ -3267,19 +3267,18 @@
 while (!
-get
+is
 Done() &
@@ -3398,19 +3398,18 @@
     if (
-get
+is
 Done())%0A

Refactor of SimpleFuture + adding some comments