Class StateHolder<E>


  • public final class StateHolder<E>
    extends java.lang.Object
    Class, which holds the state. Provides API for state change notification, state read/write access locking.
    Author:
    Alexey Stashok
    • Constructor Summary

      Constructors 
      Constructor Description
      StateHolder()
      Constructs StateHolder.
      StateHolder​(E initialState)
      Constructs StateHolder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      E getState()
      Gets current state Current StateHolder locking mode will be used
      java.util.concurrent.locks.ReentrantReadWriteLock getStateLocker()
      Gets Read/Write locker, which is used by this StateHolder
      protected void notifyConditionListeners()  
      java.util.concurrent.Future<E> notifyWhenConditionMatchState​(Condition condition, CompletionHandler<E> completionHandler)
      Register listener, which will be notified, when state will match the condition.
      java.util.concurrent.Future<E> notifyWhenStateIsEqual​(E state, CompletionHandler<E> completionHandler)
      Register listener, which will be notified, when state will be equal to passed one.
      java.util.concurrent.Future<E> notifyWhenStateIsNotEqual​(E state, CompletionHandler<E> completionHandler)
      Register listener, which will be notified, when state will become not equal to passed one.
      void setState​(E state)
      Sets current state Current StateHolder locking mode will be used
      • 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

        public StateHolder​(E initialState)
        Constructs StateHolder.
    • Method Detail

      • getState

        public E getState()
        Gets current state Current StateHolder locking mode will be used
        Returns:
        state
      • setState

        public void setState​(E state)
        Sets current state Current StateHolder locking mode will be used
        Parameters:
        state -
      • getStateLocker

        public java.util.concurrent.locks.ReentrantReadWriteLock getStateLocker()
        Gets Read/Write locker, which is used by this StateHolder
        Returns:
        locker
      • notifyWhenStateIsEqual

        public java.util.concurrent.Future<E> notifyWhenStateIsEqual​(E state,
                                                                     CompletionHandler<E> completionHandler)
        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
        completionHandler - that 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 java.util.concurrent.Future<E> notifyWhenStateIsNotEqual​(E state,
                                                                        CompletionHandler<E> completionHandler)
        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
        completionHandler - that 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 java.util.concurrent.Future<E> notifyWhenConditionMatchState​(Condition condition,
                                                                            CompletionHandler<E> completionHandler)
        Register listener, which will be notified, when state will match the condition. Once listener will be notified - it will be removed from this StateHolder's listener set.
        Parameters:
        condition - Condition, the listener is interested in
        completionHandler - that will be notified. This StateHolder implementation works with Runnable, Callable, CountDownLatch, Object listeners
        Returns:
        ConditionListener, if current state doesn't match the condition and listener was registered, null if current state matches the condition. In both cases listener will be notified
      • notifyConditionListeners

        protected void notifyConditionListeners()