KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /** The respresentation of a state in a state machine.
29  *
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 2047 $
32  */

33 public class State
34 {
35    /** The name of the state */
36    private String JavaDoc name;
37    /** HashMap<String, Transition> */
38    private HashMap JavaDoc allowedTransitions = new HashMap JavaDoc();
39    /** Arbitrary state data */
40    private Object JavaDoc data;
41
42    public State(String JavaDoc name)
43    {
44       this(name, null);
45    }
46    public State(String JavaDoc name, Map JavaDoc transitions)
47    {
48       this.name = name;
49       if( transitions != null )
50       {
51          allowedTransitions.putAll(transitions);
52       }
53    }
54
55    /** Get the state name.
56     * @return the name of the state.
57     */

58    public String JavaDoc getName()
59    {
60       return name;
61    }
62
63    public Object JavaDoc getData()
64    {
65       return data;
66    }
67    public void setData(Object JavaDoc data)
68    {
69       this.data = data;
70    }
71
72    /** An accept state is indicated by no transitions
73     * @return true if this is an accept state, false otherwise.
74     */

75    public boolean isAcceptState()
76    {
77       return allowedTransitions.size() == 0;
78    }
79
80    /** Add a transition to the allowed transition map.
81     *
82     * @param transition
83     * @return this to allow chained addTransition calls
84     */

85    public State addTransition(Transition transition)
86    {
87       allowedTransitions.put(transition.getName(), transition);
88       return this;
89    }
90    
91    /** Lookup an allowed transition given its name.
92     *
93     * @param name - the name of a valid transition from this state.
94     * @return the valid transition if it exists, null otherwise.
95     */

96    public Transition getTransition(String JavaDoc name)
97    {
98       Transition t = (Transition) allowedTransitions.get(name);
99       return t;
100    }
101
102    /** Get the Map<String, Transition> of allowed transitions for this state.
103     * @return the allowed transitions map.
104     */

105    public Map JavaDoc getTransitions()
106    {
107       return allowedTransitions;
108    }
109
110    public String JavaDoc toString()
111    {
112       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("State(name=");
113       tmp.append(name);
114       Iterator JavaDoc i = allowedTransitions.entrySet().iterator();
115       while( i.hasNext() )
116       {
117          Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
118          tmp.append("\n\t on: ");
119          tmp.append(e.getKey());
120          Transition t = (Transition) e.getValue();
121          tmp.append(" go to: ");
122          tmp.append(t.getTarget().getName());
123       }
124       tmp.append(')');
125       return tmp.toString();
126    }
127 }
128
Popular Tags