com.tangosol.util.fsm.SimpleModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.util.fsm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* A {@link SimpleModel} is a basic implementation of a {@link Model} for a {@link FiniteStateMachine}.
*
* @param the type of state
*
* @author Brian Oliver
*/
public class SimpleModel>
implements Model
{
// ----- constructors ---------------------------------------------------
/**
* Constructs a {@link SimpleModel} given an Enum of the possible states
* of a {@link FiniteStateMachine}.
*
* @param stateClass the Enum class containing the possible states
*/
public SimpleModel(Class stateClass)
{
m_stateClass = stateClass;
m_states = stateClass.getEnumConstants();
m_transitions = new ArrayList>();
m_mapStateEntryActions = new HashMap>();
m_mapStateExitActions = new HashMap>();
}
// ----- Model interface ------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public Class getStateClass()
{
return m_stateClass;
}
/**
* {@inheritDoc}
*/
@Override
public S[] getStates()
{
return m_states;
}
/**
* {@inheritDoc}
*/
@Override
public Map> getStateEntryActions()
{
return m_mapStateEntryActions;
}
/**
* Adds/overrides the {@link StateEntryAction} for the specified state in
* the {@link Model}.
*
* @param state the state
* @param stateEntryAction the {@link StateEntryAction} for the state
*/
public void addStateEntryAction(S state, StateEntryAction stateEntryAction)
{
m_mapStateEntryActions.put(state, stateEntryAction);
}
/**
* {@inheritDoc}
*/
@Override
public Map> getStateExitActions()
{
return m_mapStateExitActions;
}
/**
* {@inheritDoc}
*/
@Override
public Iterable> getTransitions()
{
return m_transitions;
}
// ----- SimpleModel methods --------------------------------------------
/**
* Obtains the state with the specified name from the {@link SimpleModel}.
*
* @param sName the name of the state to obtain
*
* @return the state with the specified name or null
if no
* such state is known by the {@link SimpleModel}
*/
public S getState(String sName)
{
sName = sName.trim().toUpperCase();
for (S state : m_states)
{
if (state.name().toUpperCase().equals(sName))
{
return state;
}
}
return null;
}
/**
* Adds/overrides the {@link StateExitAction} for the specified state in the {@link Model}.
*
* @param state the state
* @param stateExitAction the {@link StateExitAction} for the state
*/
public void addStateExitAction(S state, StateExitAction stateExitAction)
{
m_mapStateExitActions.put(state, stateExitAction);
}
/**
* Adds the specified {@link Transition} to the {@link Model}.
*
* @param transition the {@link Transition} to add
*/
public void addTransition(Transition transition)
{
m_transitions.add(transition);
}
// ----- data members ---------------------------------------------------
/**
* The {@link Class} of the state of the {@link Model}.
*/
private Class m_stateClass;
/**
* The array of possible states of a {@link FiniteStateMachine}.
*/
private S[] m_states;
/**
* A list of possible transitions for the {@link FiniteStateMachine}.
*/
private ArrayList> m_transitions;
/**
* A map of states to their {@link StateEntryAction}.
*/
private HashMap> m_mapStateEntryActions;
/**
* A map of states to their {@link StateExitAction}.
*/
private HashMap> m_mapStateExitActions;
}