## com.sun.grizzly.util
Class StateHolder<E>

    java.lang.Object
      com.sun.grizzly.util.StateHolder<E>

    public class StateHolder<E>

extends Object

Class, which holds the state. Provides API for state change notification, state read/write access locking.

Author:
  Alexey Stashok

Nested Class Summary
`static class` `StateHolder.ConditionListener<E>` Common ConditionListener class, which could be used with StateHolder, to register custom conditions.
`static class` `StateHolder.EqualConditionListener<E>` Equal ConditionListener implementation
`static class` `StateHolder.EventListener` EventListener class, which is a part of `EventConditionListener`, and implements notificatation logic, when condition becomes true.
`static class` `StateHolder.NotEqualConditionListener<E>` Not equal ConditionListener implementation

Constructor Summary
`StateHolder()` Constructs `StateHolder`.
`StateHolder(boolean isLockEnabled)` Constructs `StateHolder`.

Method Summary
`protected void` `checkConditionListeners(E state)`
`E` `getState()` Gets current state Current StateHolder locking mode will be used
`E` `getState(boolean locked)` Gets current state
`ReentrantReadWriteLock` `getStateLocker()` Gets Read/Write locker, which is used by this `StateHolder`
`boolean` `isLockEnabled()` Gets current locking mode
`void` `notifyWhenConditionMatchState(StateHolder.ConditionListener<E> conditionListener)` Register custom condition listener, which will be notified, when listener's condition will become true.
`StateHolder.ConditionListener<E>` `notifyWhenStateIsEqual(E state, Object listener)` Register listener, which will be notified, when state will be equal to passed one.
`StateHolder.ConditionListener<E>` `notifyWhenStateIsNotEqual(E state, Object listener)` Register listener, which will be notified, when state will become not equal to passed one.
`void` `removeConditionListener(StateHolder.ConditionListener<E> conditionListener)`
`void` `setLockEnabled(boolean isLockEnabled)` Setss current locking mode
`void` `setState(E state)` Sets current state Current StateHolder locking mode will be used
`void` `setState(E state, boolean locked)` Sets current state

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### StateHolder

    public StateHolder()

  Constructs `StateHolder`. StateHolder will work in not-locking mode.

### StateHolder

    public StateHolder(boolean isLockEnabled)

  Constructs `StateHolder`.

  Parameters:
    `isLockEnabled` - locking mode

Method Detail

### getState

    public E getState()

  Gets current state Current StateHolder locking mode will be used

  Returns:
    state

### getState

    public E getState(boolean locked)

  Gets current state

  Parameters:
    `locked` - if true, get will be invoked in locking mode, false - non-locked
  Returns:
    state

### setState

    public void setState(E state)

  Sets current state Current StateHolder locking mode will be used

  Parameters:
    `state` -

### setState

    public void setState(E state,
                         boolean locked)

  Sets current state

  Parameters:
    `state` -
    `locked` - if true, set will be invoked in locking mode, false - non-locking

### getStateLocker

    public ReentrantReadWriteLock getStateLocker()

  Gets Read/Write locker, which is used by this `StateHolder`

  Returns:
    locker

### isLockEnabled

    public boolean isLockEnabled()

  Gets current locking mode

  Returns:
    true, if mode is set to locking, false otherwise

### setLockEnabled

    public void setLockEnabled(boolean isLockEnabled)

  Setss current locking mode

  Parameters:
    `isLockEnabled` - true, if mode will be set to locking, false otherwise

### notifyWhenStateIsEqual

    public StateHolder.ConditionListener<E> notifyWhenStateIsEqual(E state,
                                                                   Object listener)

  Register listener, which will be notified, when state will be equal to passed one. Once listener will be notified - it will be removed from this `StateHolder`'s listener set.

  Parameters:
    `state` - State, listener is interested in
    `listener` - Object, which will be notified. This `StateHolder` implementation works with Runnable, Callable, CountDownLatch, Object listeners
  Returns:
    `ConditionListener`, if current state is not equal to required and listener was registered, null if current state is equal to required. In both cases listener will be notified

### notifyWhenStateIsNotEqual

    public StateHolder.ConditionListener<E> notifyWhenStateIsNotEqual(E state,
                                                                      Object listener)

  Register listener, which will be notified, when state will become not equal to passed one. Once listener will be notified - it will be removed from this `StateHolder`'s listener set.

  Parameters:
    `state` - State, listener is interested in
    `listener` - Object, which will be notified. This `StateHolder` implementation works with Runnable, Callable, CountDownLatch, Object listeners
  Returns:
    `ConditionListener`, if current state is equal to required and listener was registered, null if current state is not equal to required. In both cases listener will be notified

### notifyWhenConditionMatchState

    public void notifyWhenConditionMatchState(StateHolder.ConditionListener<E> conditionListener)

  Register custom condition listener, which will be notified, when listener's condition will become true. Once listener will be notified - it will be removed from this `StateHolder`'s listener set.

  Parameters:
    `conditionListener` - contains both condition and listener, which will be called, when condition become true

### removeConditionListener

    public void removeConditionListener(StateHolder.ConditionListener<E> conditionListener)

### checkConditionListeners

    protected void checkConditionListeners(E state)

Copyright © 2008 SUN Microsystems. All Rights Reserved.