ingo@104: package de.intevation.artifactdatabase.state; ingo@104: ingo@104: import java.util.HashMap; ingo@104: import java.util.List; ingo@104: import java.util.Map; ingo@104: ingo@104: import org.apache.log4j.Logger; ingo@104: ingo@104: /** ingo@104: * The StateEngine stores all states for each Artifact. ingo@104: * ingo@104: * @author Ingo Weinzierl ingo@104: */ ingo@104: public class StateEngine { ingo@104: ingo@104: /** The logger used in this class. */ ingo@104: private static Logger logger = Logger.getLogger(StateEngine.class); ingo@104: ingo@104: /** ingo@104: * A map that contains the states of the artifacts. The key of this map is ingo@104: * the name of an artifact, its value is a list of all states the artifact ingo@104: * can reach. ingo@104: */ ingo@104: protected Map states; ingo@104: ingo@104: ingo@104: /** ingo@108: * A map that contains all existing states. The key of this map is the ID of ingo@108: * the state, its value is the state itself. ingo@108: */ ingo@108: protected Map allStates; ingo@108: ingo@108: ingo@108: /** ingo@104: * The default constructor. ingo@104: */ ingo@104: public StateEngine() { ingo@108: states = new HashMap(); ingo@108: allStates = new HashMap(); ingo@108: } ingo@108: ingo@108: ingo@108: /** ingo@108: * This method adds a state into the map allStates. ingo@108: * ingo@108: * @param state The state to add. ingo@108: */ ingo@108: protected void addState(State state) { ingo@108: allStates.put(state.getID(), state); ingo@108: } ingo@108: ingo@108: ingo@108: /** ingo@108: * Returns the state based on its ID. ingo@108: * ingo@108: * @param stateId The ID of the desired state. ingo@108: * ingo@108: * @return the state. ingo@108: */ ingo@108: public State getState(String stateId) { ingo@108: return allStates.get(stateId); ingo@104: } ingo@104: ingo@104: ingo@104: /** ingo@104: * Add new states for a specific artifact. ingo@104: * ingo@104: * @param artifact The name of the artifact. ingo@104: * @param states A list of states that the artifact can reach. ingo@104: * ingo@104: * @return true, if the states were added, otherwise false. ingo@104: */ ingo@108: public boolean addStates(String artifact, List states) { ingo@104: List tmp = this.states.get(artifact); ingo@104: ingo@104: if (tmp != null) { ingo@104: logger.info( ingo@104: "States for the artifact '" + artifact + "' already stored."); ingo@104: ingo@104: return false; ingo@104: } ingo@104: ingo@108: // add the state to the map with all existing states ingo@108: for (State s: states) { ingo@108: addState(s); ingo@108: } ingo@108: ingo@104: logger.debug("Add new states for the artifact '" + artifact + "'"); ingo@104: return this.states.put(artifact, states) != null; ingo@104: } ingo@106: ingo@106: ingo@106: /** ingo@106: * Returns the state list of an artifact specified by its name. ingo@106: * ingo@106: * @param artifact The name of the artifact. ingo@106: * ingo@106: * @return the list of states of this artifact or null if no states ingo@106: * are existing for this artifact. ingo@106: */ ingo@106: public List getStates(String artifact) { ingo@106: return states.get(artifact); ingo@106: } ingo@104: } ingo@104: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :