FileDocCategorySizeDatePackage
AutoStateTracker.javaAPI DocphoneME MR2 API (J2ME)4689Wed May 02 18:00:08 BST 2007com.sun.midp.automation

AutoStateTracker

public class AutoStateTracker extends Object
Utility class for tracking states

Fields Summary
private Object
currentState
Current state
private int
totalWaiters
Number of waiters currently waiting
private boolean
interruptWait
flag, indicating that wait must be interrupted
private Object
wakeWaitersLock
Lock used to wait for waiters to awake
private int
waitersToWake
Number of waiters to awake
Constructors Summary
AutoStateTracker(Object initialState)
Constructor

param
initialState initial state

        currentState = initialState;
        totalWaiters = 0;
        waitersToWake = 0;
        interruptWait = false;
        wakeWaitersLock = new Object();
    
Methods Summary
final java.lang.ObjectgetCurrentState()
Returns current state.

return
current state

        synchronized (this) {
            return currentState;
        }
    
final voidinterruptWait()
Interrupts waiters

        synchronized (this) {
            interruptWait = true;
            notifyAll();
        }
    
protected booleanisStateReached(java.lang.Object state)
Checks condition waiters are waiting for: specified state has been reached. Assumption: 'this' lock is already taken.

param
state state to check againts
return
true, if specified state has been reached

        return (currentState == state);
    
final voidsetCurrentState(java.lang.Object state, boolean interrupt)
Sets a state. Assumption: only one thread sets a state, 'this' lock shouldn't be already taken.

param
state state to set as current
param
interrupt if true, interrupt all waiters after setting a state.

        synchronized (this) {
            if (currentState == state) {
                return;
            }

            interruptWait = interrupt;
            currentState = state;
            waitersToWake = totalWaiters;
            notifyAll();
        }
        
        // wait until all waiters have been awaken, so they have 
        // a chance to recheck condition they are waiting for
        synchronized (wakeWaitersLock) {
            while (waitersToWake != 0) {
                try {
                    wakeWaitersLock.wait();
                } catch (InterruptedException e) {
                    // just ignore
                }
            }
        }
    
final voidwaitFor(java.lang.Object state)
Waits until specified state has been reached.

param
state state to wait for

        synchronized (this) {
            while (!isStateReached(state) && !interruptWait) {
                try {
                    totalWaiters++;
                    wait();
                    totalWaiters--;
                
                    synchronized (wakeWaitersLock) {
                        waitersToWake--;
                        // if all threads affected by setCurrentState 
                        // notification have waken up, notify setCurrentState
                        // about it
                        if (waitersToWake == 0) {
                            wakeWaitersLock.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    // just ignore
                }
            }
        }