KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > state > StateMachine


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.state;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import org.jboss.logging.Logger;
28
29 /** The representation of a finite state machine.
30  *
31  * @author Scott.Stark@jboss.org
32  * @version $Revision: 2047 $
33  */

34 public class StateMachine implements Cloneable JavaDoc
35 {
36    private static Logger log = Logger.getLogger(StateMachine.class);
37    /** A description of the state machine */
38    private String JavaDoc description;
39    /** The set of states making up the state machine */
40    private HashSet JavaDoc states;
41    /** The starting state */
42    private State startState;
43    /** The current state of the state machine */
44    private State currentState;
45
46    /** Create a state machine given its states and start state.
47     *
48     * @param states - Set<State> for the state machine
49     * @param startState - the starting state
50     */

51    public StateMachine(Set JavaDoc states, State startState)
52    {
53       this(states, startState, null);
54    }
55    /** Create a state machine given its states and start state.
56     *
57     * @param states - Set<State> for the state machine
58     * @param startState - the starting state
59     * @param description - an optional description of the state machine
60     */

61    public StateMachine(Set JavaDoc states, State startState, String JavaDoc description)
62    {
63       this.states = new HashSet JavaDoc(states);
64       this.startState = startState;
65       this.currentState = startState;
66       this.description = description;
67    }
68
69    /** Make a copy of the StateMachine maintaining the current state.
70     *
71     * @return a copy of the StateMachine.
72     */

73    public Object JavaDoc clone()
74    {
75       StateMachine clone = new StateMachine(states, startState, description);
76       clone.currentState = currentState;
77       return clone;
78    }
79
80    /** Get the state machine description.
81     * @return an possibly null description.
82     */

83    public String JavaDoc getDescription()
84    {
85       return description;
86    }
87
88    /** Get the current state of the state machine.
89     * @return the current state.
90     */

91    public State getCurrentState()
92    {
93       return currentState;
94    }
95
96    /** Get the start state of the state machine.
97     * @return the start state.
98     */

99    public State getStartState()
100    {
101       return startState;
102    }
103
104    /** Get the states of the state machine.
105     * @return the machine states.
106     */

107    public Set JavaDoc getStates()
108    {
109       return states;
110    }
111
112    /** Transition to the next state given the name of a valid transition.
113     * @param actionName - the name of transition that is valid for the
114     * current state.
115     * @return
116     * @throws IllegalTransitionException
117     */

118    public State nextState(String JavaDoc actionName)
119       throws IllegalTransitionException
120    {
121       Transition t = currentState.getTransition(actionName);
122       if( t == null )
123       {
124          String JavaDoc msg = "No transition for action: '" + actionName
125             + "' from state: '" + currentState.getName() + "'";
126          throw new IllegalTransitionException(msg);
127       }
128       State nextState = t.getTarget();
129       log.trace("nextState("+actionName+") = "+nextState);
130       currentState = nextState;
131       return currentState;
132    }
133
134    /** Reset the state machine back to the start state
135     *
136     * @return the start state
137     */

138    public State reset()
139    {
140       this.currentState = startState;
141       return currentState;
142    }
143
144    public String JavaDoc toString()
145    {
146       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("StateMachine[:\n");
147       tmp.append("\tCurrentState: "+currentState.getName());
148       Iterator JavaDoc i = states.iterator();
149       while( i.hasNext() )
150       {
151          tmp.append('\n').append(i.next());
152       }
153       tmp.append(']');
154       return tmp.toString();
155    }
156 }
157
Popular Tags