view src/org/dancres/blitz/notify/BlockingDispatchImpl.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
line wrap: on
line source

package org.dancres.blitz.notify;

class BlockingDispatchImpl implements DispatchTask {
    private EventQueue theQueue;
    private QueueEvent theEvent;

    private boolean isDone = false;
    private boolean isResolvable = false;
    private int myTotalDispatches = 0;
    private int myCompletedDispatches = 0;

    BlockingDispatchImpl(EventQueue aQueue, QueueEvent anEvent) {
        theQueue = aQueue;
        theEvent = anEvent;
    }

    public void run() {
        theQueue.dispatchImpl(this);
    }

    public QueueEvent getEvent() {
        return theEvent;
    }

    public void block() throws InterruptedException {
        synchronized(this) {
            while (! isDone) {
                try {
                    wait();
                } catch (InterruptedException anIE) {
                }
            }
        }
    }

    public void newDispatch() {
        synchronized(this) {
            ++myTotalDispatches;
        }
    }

    public void dispatched() {
        synchronized(this) {
            ++myCompletedDispatches;
        }

        checkAndFire();
    }

    public void enableResolve() {
        synchronized(this) {
            isResolvable = true;
        }

        checkAndFire();
    }

    private void checkAndFire() {
        synchronized(this) {
            if (!isResolvable)
                return;
            
            if (myTotalDispatches == myCompletedDispatches) {
                isDone = true;
                notify();
            }
        }
    }
}