import java.util.*; class State { protected State (String name) { _defaultTransition = null; _transitionList = null; _name = name; } public State (DefaultTransition defaultTransition, String name) { _defaultTransition = defaultTransition; _transitionList = new LinkedList(); _name = name; } public String toString () { return _name; } public boolean success () { return false; } public boolean failure () { return false; } public State followTransition (char c) { // Iterate through the transition list, looking for one that // applies to this symbol. ListIterator i = null; try { i = _transitionList.listIterator(0); } catch (IndexOutOfBoundsException e) { i = null; } while (i != null) { // Test the current transition. Transition current = null; try { current = (Transition)i.next(); } catch (NoSuchElementException e) { i = null; continue; } // Does this transition apply to the symbol? if (current.testSymbol(c)) { return current.followTransition(); } } // No regular transition applies, so take the default. return _defaultTransition.followTransition(); } protected boolean isRedundant (Transition transition) { // Iterate through the existing transitions. ListIterator i = null; try { i = _transitionList.listIterator(0); } catch (IndexOutOfBoundsException e) { i = null; } while (i != null) { // Test the given transition against this one. Transition current = null; try { current = (Transition)i.next(); } catch (NoSuchElementException e) { i = null; continue; } // These two transitions cover the same symbol. if (current.isRedundant(transition)) { return true; } } // No redundancy. return false; } public void addTransition (Transition transition) { // Ensure that this transition does not cover a symbol already // covered by another transition. if (!isRedundant(transition)) { _transitionList.add(transition); } } protected DefaultTransition _defaultTransition; protected LinkedList _transitionList; protected String _name; }