
package uk.co.wingpath.util;

/**
* Simple implementation of {@link Waiter} that uses {@link Object#notifyAll}
* to inform waiting threads.
* Typical use would be:
*   NotifyWaiter nw = new NotifyWaiter ();
*   action (..., nw);
*   nw.waitUntilDone ();
*/
public class NotifyWaiter
    implements Waiter
{
    private boolean done = false;

    /**
    * Called to inform the waiter that the task has been done.
    */
    @Override
    public synchronized void inform ()
    {
        done = true;
        notifyAll ();
    }

    /**
    * Waits until informed that the task has been done.
    */
    public synchronized void waitUntilDone ()
        throws InterruptedException
    {
        while (!done)
            wait ();
    }

    /**
    * Resets the waiter so that it can be used for waiting again.
    */
    public synchronized void reset ()
    {
        done = false;
    }
}

