<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FCollections_Data_Structure%2FState_Machine</id>
		<title>Java/Collections Data Structure/State Machine - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FCollections_Data_Structure%2FState_Machine"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&amp;action=history"/>
		<updated>2026-04-19T08:36:33Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&amp;diff=9061&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&amp;diff=9061&amp;oldid=prev"/>
				<updated>2010-06-01T07:23:50Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:23, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&amp;diff=9060&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&amp;diff=9060&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== The representation of a finite state machine ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source&lt;br /&gt;
 * Copyright 2005, JBoss Inc., and individual contributors as indicated&lt;br /&gt;
 * by the @authors tag. See the copyright.txt in the distribution for a&lt;br /&gt;
 * full listing of individual contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it&lt;br /&gt;
 * under the terms of the GNU Lesser General Public License as&lt;br /&gt;
 * published by the Free Software Foundation; either version 2.1 of&lt;br /&gt;
 * the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This software is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this software; if not, write to the Free&lt;br /&gt;
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA&lt;br /&gt;
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
import java.util.HashMap;&lt;br /&gt;
import java.util.HashSet;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
/**&lt;br /&gt;
 * The representation of a finite state machine.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Scott.Stark@jboss.org&lt;br /&gt;
 * @version $Revision: 2787 $&lt;br /&gt;
 */&lt;br /&gt;
@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
public class StateMachine implements Cloneable {&lt;br /&gt;
  /** A description of the state machine */&lt;br /&gt;
  private String description;&lt;br /&gt;
  /** The set of states making up the state machine */&lt;br /&gt;
  private HashSet states;&lt;br /&gt;
  /** The starting state */&lt;br /&gt;
  private State startState;&lt;br /&gt;
  /** The current state of the state machine */&lt;br /&gt;
  private State currentState;&lt;br /&gt;
  /**&lt;br /&gt;
   * Create a state machine given its states and start state.&lt;br /&gt;
   * &lt;br /&gt;
   * @param states -&lt;br /&gt;
   *          Set&amp;lt;State&amp;gt; for the state machine&lt;br /&gt;
   * @param startState -&lt;br /&gt;
   *          the starting state&lt;br /&gt;
   */&lt;br /&gt;
  public StateMachine(Set states, State startState) {&lt;br /&gt;
    this(states, startState, null);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Create a state machine given its states and start state.&lt;br /&gt;
   * &lt;br /&gt;
   * @param states -&lt;br /&gt;
   *          Set&amp;lt;State&amp;gt; for the state machine&lt;br /&gt;
   * @param startState -&lt;br /&gt;
   *          the starting state&lt;br /&gt;
   * @param description -&lt;br /&gt;
   *          an optional description of the state machine&lt;br /&gt;
   */&lt;br /&gt;
  public StateMachine(Set states, State startState, String description) {&lt;br /&gt;
    this.states = new HashSet(states);&lt;br /&gt;
    this.startState = startState;&lt;br /&gt;
    this.currentState = startState;&lt;br /&gt;
    this.description = description;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Make a copy of the StateMachine maintaining the current state.&lt;br /&gt;
   * &lt;br /&gt;
   * @return a copy of the StateMachine.&lt;br /&gt;
   */&lt;br /&gt;
  public Object clone() {&lt;br /&gt;
    StateMachine clone = new StateMachine(states, startState, description);&lt;br /&gt;
    clone.currentState = currentState;&lt;br /&gt;
    return clone;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the state machine description.&lt;br /&gt;
   * &lt;br /&gt;
   * @return an possibly null description.&lt;br /&gt;
   */&lt;br /&gt;
  public String getDescription() {&lt;br /&gt;
    return description;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the current state of the state machine.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the current state.&lt;br /&gt;
   */&lt;br /&gt;
  public State getCurrentState() {&lt;br /&gt;
    return currentState;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the start state of the state machine.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the start state.&lt;br /&gt;
   */&lt;br /&gt;
  public State getStartState() {&lt;br /&gt;
    return startState;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the states of the state machine.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the machine states.&lt;br /&gt;
   */&lt;br /&gt;
  public Set getStates() {&lt;br /&gt;
    return states;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Transition to the next state given the name of a valid transition.&lt;br /&gt;
   * &lt;br /&gt;
   * @param actionName -&lt;br /&gt;
   *          the name of transition that is valid for the current state.&lt;br /&gt;
   * @return the next state&lt;br /&gt;
   * @throws IllegalTransitionException&lt;br /&gt;
   */&lt;br /&gt;
  public State nextState(String actionName) throws IllegalTransitionException {&lt;br /&gt;
    Transition t = currentState.getTransition(actionName);&lt;br /&gt;
    if (t == null) {&lt;br /&gt;
      String msg = &amp;quot;No transition for action: &amp;quot;&amp;quot; + actionName + &amp;quot;&amp;quot; from state: &amp;quot;&amp;quot;&lt;br /&gt;
          + currentState.getName() + &amp;quot;&amp;quot;&amp;quot;;&lt;br /&gt;
      throw new IllegalTransitionException(msg);&lt;br /&gt;
    }&lt;br /&gt;
    State nextState = t.getTarget();&lt;br /&gt;
    System.out.println(&amp;quot;nextState(&amp;quot; + actionName + &amp;quot;) = &amp;quot; + nextState);&lt;br /&gt;
    currentState = nextState;&lt;br /&gt;
    return currentState;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Reset the state machine back to the start state&lt;br /&gt;
   * &lt;br /&gt;
   * @return the start state&lt;br /&gt;
   */&lt;br /&gt;
  public State reset() {&lt;br /&gt;
    this.currentState = startState;&lt;br /&gt;
    return currentState;&lt;br /&gt;
  }&lt;br /&gt;
  public String toString() {&lt;br /&gt;
    StringBuffer tmp = new StringBuffer(&amp;quot;StateMachine[:\n&amp;quot;);&lt;br /&gt;
    tmp.append(&amp;quot;\tCurrentState: &amp;quot; + currentState.getName());&lt;br /&gt;
    Iterator i = states.iterator();&lt;br /&gt;
    while (i.hasNext()) {&lt;br /&gt;
      tmp.append(&amp;quot;\n&amp;quot;).append(i.next());&lt;br /&gt;
    }&lt;br /&gt;
    tmp.append(&amp;quot;]&amp;quot;);&lt;br /&gt;
    return tmp.toString();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and&lt;br /&gt;
 * individual contributors as indicated by the @authors tag. See the&lt;br /&gt;
 * copyright.txt in the distribution for a full listing of individual&lt;br /&gt;
 * contributors.&lt;br /&gt;
 * &lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it under the&lt;br /&gt;
 * terms of the GNU Lesser General Public License as published by the Free&lt;br /&gt;
 * Software Foundation; either version 2.1 of the License, or (at your option)&lt;br /&gt;
 * any later version.&lt;br /&gt;
 * &lt;br /&gt;
 * This software is distributed in the hope that it will be useful, but WITHOUT&lt;br /&gt;
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS&lt;br /&gt;
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more&lt;br /&gt;
 * details.&lt;br /&gt;
 * &lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public License&lt;br /&gt;
 * along with this software; if not, write to the Free Software Foundation,&lt;br /&gt;
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF&lt;br /&gt;
 * site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
/**&lt;br /&gt;
 * The respresentation of a state in a state machine.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Scott.Stark@jboss.org&lt;br /&gt;
 * @version $Revision: 2787 $&lt;br /&gt;
 */&lt;br /&gt;
@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
class State {&lt;br /&gt;
  /** The name of the state */&lt;br /&gt;
  private String name;&lt;br /&gt;
  /** HashMap&amp;lt;String, Transition&amp;gt; */&lt;br /&gt;
  private HashMap allowedTransitions = new HashMap();&lt;br /&gt;
  /** Arbitrary state data */&lt;br /&gt;
  private Object data;&lt;br /&gt;
  public State(String name) {&lt;br /&gt;
    this(name, null);&lt;br /&gt;
  }&lt;br /&gt;
  public State(String name, Map transitions) {&lt;br /&gt;
    this.name = name;&lt;br /&gt;
    if (transitions != null) {&lt;br /&gt;
      allowedTransitions.putAll(transitions);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the state name.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the name of the state.&lt;br /&gt;
   */&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name;&lt;br /&gt;
  }&lt;br /&gt;
  public Object getData() {&lt;br /&gt;
    return data;&lt;br /&gt;
  }&lt;br /&gt;
  public void setData(Object data) {&lt;br /&gt;
    this.data = data;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * An accept state is indicated by no transitions&lt;br /&gt;
   * &lt;br /&gt;
   * @return true if this is an accept state, false otherwise.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean isAcceptState() {&lt;br /&gt;
    return allowedTransitions.size() == 0;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Add a transition to the allowed transition map.&lt;br /&gt;
   * &lt;br /&gt;
   * @param transition&lt;br /&gt;
   * @return this to allow chained addTransition calls&lt;br /&gt;
   */&lt;br /&gt;
  public State addTransition(Transition transition) {&lt;br /&gt;
    allowedTransitions.put(transition.getName(), transition);&lt;br /&gt;
    return this;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Lookup an allowed transition given its name.&lt;br /&gt;
   * &lt;br /&gt;
   * @param name -&lt;br /&gt;
   *          the name of a valid transition from this state.&lt;br /&gt;
   * @return the valid transition if it exists, null otherwise.&lt;br /&gt;
   */&lt;br /&gt;
  public Transition getTransition(String name) {&lt;br /&gt;
    Transition t = (Transition) allowedTransitions.get(name);&lt;br /&gt;
    return t;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get the Map&amp;lt;String, Transition&amp;gt; of allowed transitions for this state.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the allowed transitions map.&lt;br /&gt;
   */&lt;br /&gt;
  public Map getTransitions() {&lt;br /&gt;
    return allowedTransitions;&lt;br /&gt;
  }&lt;br /&gt;
  public String toString() {&lt;br /&gt;
    StringBuffer tmp = new StringBuffer(&amp;quot;State(name=&amp;quot;);&lt;br /&gt;
    tmp.append(name);&lt;br /&gt;
    Iterator i = allowedTransitions.entrySet().iterator();&lt;br /&gt;
    while (i.hasNext()) {&lt;br /&gt;
      Map.Entry e = (Map.Entry) i.next();&lt;br /&gt;
      tmp.append(&amp;quot;\n\t on: &amp;quot;);&lt;br /&gt;
      tmp.append(e.getKey());&lt;br /&gt;
      Transition t = (Transition) e.getValue();&lt;br /&gt;
      tmp.append(&amp;quot; go to: &amp;quot;);&lt;br /&gt;
      tmp.append(t.getTarget().getName());&lt;br /&gt;
    }&lt;br /&gt;
    tmp.append(&amp;quot;)&amp;quot;);&lt;br /&gt;
    return tmp.toString();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and&lt;br /&gt;
 * individual contributors as indicated by the @authors tag. See the&lt;br /&gt;
 * copyright.txt in the distribution for a full listing of individual&lt;br /&gt;
 * contributors.&lt;br /&gt;
 * &lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it under the&lt;br /&gt;
 * terms of the GNU Lesser General Public License as published by the Free&lt;br /&gt;
 * Software Foundation; either version 2.1 of the License, or (at your option)&lt;br /&gt;
 * any later version.&lt;br /&gt;
 * &lt;br /&gt;
 * This software is distributed in the hope that it will be useful, but WITHOUT&lt;br /&gt;
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS&lt;br /&gt;
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more&lt;br /&gt;
 * details.&lt;br /&gt;
 * &lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public License&lt;br /&gt;
 * along with this software; if not, write to the Free Software Foundation,&lt;br /&gt;
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF&lt;br /&gt;
 * site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
/**&lt;br /&gt;
 * A representation of a transition from a state to another state.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Scott.Stark@jboss.org&lt;br /&gt;
 * @version $Revision: 1958 $&lt;br /&gt;
 */&lt;br /&gt;
class Transition {&lt;br /&gt;
  private String name;&lt;br /&gt;
  private State target;&lt;br /&gt;
  public Transition(String name, State target) {&lt;br /&gt;
    this.name = name;&lt;br /&gt;
    this.target = target;&lt;br /&gt;
  }&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name;&lt;br /&gt;
  }&lt;br /&gt;
  public State getTarget() {&lt;br /&gt;
    return target;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and&lt;br /&gt;
 * individual contributors as indicated by the @authors tag. See the&lt;br /&gt;
 * copyright.txt in the distribution for a full listing of individual&lt;br /&gt;
 * contributors.&lt;br /&gt;
 * &lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it under the&lt;br /&gt;
 * terms of the GNU Lesser General Public License as published by the Free&lt;br /&gt;
 * Software Foundation; either version 2.1 of the License, or (at your option)&lt;br /&gt;
 * any later version.&lt;br /&gt;
 * &lt;br /&gt;
 * This software is distributed in the hope that it will be useful, but WITHOUT&lt;br /&gt;
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS&lt;br /&gt;
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more&lt;br /&gt;
 * details.&lt;br /&gt;
 * &lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public License&lt;br /&gt;
 * along with this software; if not, write to the Free Software Foundation,&lt;br /&gt;
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF&lt;br /&gt;
 * site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
/**&lt;br /&gt;
 * An exception thrown when an invalid transition is attempted from a state.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Scott.Stark@jboss.org&lt;br /&gt;
 * @version $Revision: 2800 $&lt;br /&gt;
 */&lt;br /&gt;
class IllegalTransitionException extends Exception {&lt;br /&gt;
  /** The serialVersionUID */&lt;br /&gt;
  private static final long serialVersionUID = -3392564168782896452L;&lt;br /&gt;
  public IllegalTransitionException(String msg) {&lt;br /&gt;
    super(msg);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>