org.opcfoundation.ua.utils
Class AbstractState<StateType,ErrorType extends Throwable>

java.lang.Object
  extended by org.opcfoundation.ua.utils.AbstractState<StateType,ErrorType>
All Implemented Interfaces:
IStatefulObject<StateType,ErrorType>
Direct Known Subclasses:
AbstractServerConnection, AbstractServerSecureChannel, AsyncRead, AsyncServerSocket, AsyncSocketImpl, AsyncWrite, BufferMonitor, HttpsServer, HttpsServerConnection, OpcTcpServer, State

public abstract class AbstractState<StateType,ErrorType extends Throwable>
extends Object
implements IStatefulObject<StateType,ErrorType>

This is a default implementation to IStatefulObject. This class can be subclassed or used as it. The state type is parametrized (typically an enumeration). TODO Remove locks - use spin set and test

Author:
Toni Kalajainen (toni.kalajainen@vtt.fi)
See Also:
IStatefulObject, Listener for state modifications

Constructor Summary
AbstractState(StateType initialState)
          Constructor for AbstractState.
AbstractState(StateType initialState, StateType errorState)
          Creates a state with a error state.
 
Method Summary
 void addStateListener(StateListener<StateType> listener)
          Add on-event listener.
 void addStateNotifiable(StateListener<StateType> listener)
          Add post-event notification listener.
protected  void assertNoError()
          assertNoError.
protected  StateType attemptSetState(Set<StateType> prerequisiteState, StateType newState)
          Attempts to change the state.
protected  void clearError()
          clearError.
 ErrorType getError()
          Get error state or null
 StateType getState()
          getState.
 boolean hasError()
          hasError.
protected  boolean isStateTransitionAllowed(StateType oldState, StateType newState)
          Checks whether state transition is allowed.
protected  void onListenerException(RuntimeException rte)
          Override this.
protected  void onStateTransition(StateType oldState, StateType newState)
          Override this.
 void removeStateListener(StateListener<StateType> listener)
          removeStateListener.
 void removeStateNotifiable(StateListener<StateType> listener)
          removeStateNotifiable.
protected  void setError(ErrorType error)
          setError.
protected  boolean setState(StateType state)
          Setter for the field state.
protected  StateType setState(StateType state, Executor listenerExecutor, Set<StateType> prerequisiteStates)
          Set state
 StateType waitForState(Set<StateType> set)
          Wait until state changes to one of the given states.
 StateType waitForState(Set<StateType> set, long timeout, TimeUnit unit)
          Wait until state changes to one of the given states or until time out occurs.
 StateType waitForStateUninterruptibly(Set<StateType> set)
          Wait until state changes to one of the given states.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractState

public AbstractState(StateType initialState)

Constructor for AbstractState.

Parameters:
initialState - a StateType object.

AbstractState

public AbstractState(StateType initialState,
                     StateType errorState)
Creates a state with a error state. The state object goes to errorState on setError().

Parameters:
initialState - a StateType object.
errorState - a StateType object.
Method Detail

addStateListener

public void addStateListener(StateListener<StateType> listener)
Add on-event listener. Add on-event listener.

Specified by:
addStateListener in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
listener - a StateListener object.

addStateNotifiable

public void addStateNotifiable(StateListener<StateType> listener)
Add post-event notification listener. The prosessing thread is random. The prosessing order is not guaranteed if the handling is not synchronized. Add post-event notification listener. The prosessing thread is random. The prosessing order is not guaranteed if the handling is not synchronized.

Specified by:
addStateNotifiable in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
listener - a StateListener object.

getError

public ErrorType getError()
Get error state or null

Specified by:
getError in interface IStatefulObject<StateType,ErrorType extends Throwable>
Returns:
error

getState

public StateType getState()

getState.

Specified by:
getState in interface IStatefulObject<StateType,ErrorType extends Throwable>
Returns:
a StateType object.

hasError

public boolean hasError()

hasError.

Returns:
a boolean.

removeStateListener

public void removeStateListener(StateListener<StateType> listener)

removeStateListener.

Specified by:
removeStateListener in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
listener - a StateListener object.

removeStateNotifiable

public void removeStateNotifiable(StateListener<StateType> listener)

removeStateNotifiable.

Specified by:
removeStateNotifiable in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
listener - a StateListener object.

waitForState

public StateType waitForState(Set<StateType> set)
                       throws InterruptedException,
                              ErrorType extends Throwable
Wait until state changes to one of the given states.

Specified by:
waitForState in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
set - states that ends waiting
Returns:
the state in the given set that broke the wait
Throws:
InterruptedException - if any.
ErrorType - if any.
ErrorType extends Throwable

waitForState

public StateType waitForState(Set<StateType> set,
                              long timeout,
                              TimeUnit unit)
                       throws InterruptedException,
                              TimeoutException,
                              ErrorType extends Throwable
Wait until state changes to one of the given states or until time out occurs.

Specified by:
waitForState in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
set - a Set object.
timeout - a long.
unit - a TimeUnit object.
Returns:
state one in set
Throws:
InterruptedException - thread was interrupted
TimeoutException - timeout occured
ErrorType - if any.
ErrorType extends Throwable

waitForStateUninterruptibly

public StateType waitForStateUninterruptibly(Set<StateType> set)
                                      throws ErrorType extends Throwable
Wait until state changes to one of the given states.

Specified by:
waitForStateUninterruptibly in interface IStatefulObject<StateType,ErrorType extends Throwable>
Parameters:
set - states that ends waiting
Returns:
the state in the given set that broke the wait
Throws:
ErrorType - if any.
ErrorType extends Throwable

assertNoError

protected void assertNoError()
                      throws ErrorType extends Throwable

assertNoError.

Throws:
ErrorType - if any.
ErrorType extends Throwable

attemptSetState

protected StateType attemptSetState(Set<StateType> prerequisiteState,
                                    StateType newState)
Attempts to change the state. The state will be changed only if current state is one of the expected states.

Parameters:
prerequisiteState - expected current state
newState - a StateType object.
Returns:
state after attempt

clearError

protected void clearError()

clearError.


isStateTransitionAllowed

protected boolean isStateTransitionAllowed(StateType oldState,
                                           StateType newState)
Checks whether state transition is allowed. Override this

Parameters:
oldState - a StateType object.
newState - a StateType object.
Returns:
true if state transition is allowed

onListenerException

protected void onListenerException(RuntimeException rte)
Override this.

Parameters:
rte - a RuntimeException object.

onStateTransition

protected void onStateTransition(StateType oldState,
                                 StateType newState)
Override this.

Parameters:
oldState - a StateType object.
newState - a StateType object.

setError

protected void setError(ErrorType error)

setError.

Parameters:
error - a ErrorType object.

setState

protected boolean setState(StateType state)

Setter for the field state.

Parameters:
state - a StateType object.
Returns:
a boolean.

setState

protected StateType setState(StateType state,
                             Executor listenerExecutor,
                             Set<StateType> prerequisiteStates)
Set state

Parameters:
state - a StateType object.
listenerExecutor - executor for post listener handling or null for immediate
prerequisiteStates - old state prerequisite or null
Returns:
state after attempt


Copyright © 2018. All rights reserved.