package xhl.examples.statemachine;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * State machine
 *
 * Example by Martin Fowler: http://martinfowler.com/dslwip/Intro.html
 */
public class StateMachine {

    private final State start;

    private final List<Event> resetEvents = new ArrayList<Event>();

    public StateMachine(State start) {
        this.start = start;
    }

    public State getStart() {
        return start;
    }

    public Collection<State> getStates() {
        List<State> result = new ArrayList<State>();
        gatherForwards(result, start);
        return result;
    }

    private void gatherForwards(Collection<State> result, State start) {
        if (start == null) {
            return;
        }
        if (result.contains(start)) {
            return;
        } else {
            result.add(start);
            for (State next : start.getAllTargets()) {
                gatherForwards(result, next);
            }
            return;
        }
    }

    public void addResetEvents(Event... events) {
        for (Event e : events) {
            resetEvents.add(e);
        }
    }

    public boolean isResetEvent(String eventCode) {
        return resetEventCodes().contains(eventCode);
    }

    private List<String> resetEventCodes() {
        List<String> result = new ArrayList<String>();
        for (Event e : resetEvents) {
            result.add(e.getCode());
        }
        return result;
    }
}

@@ -86,16 +86,46 @@
 ection;%0A
+import java.util.Collections;%0A
 import j
@@ -762,121 +762,43 @@
 art 
-=
+!
 = null
-) %7B%0A            return;%0A        %7D%0A        if (result.contains(start)) %7B%0A            return;%0A        %7D else
+ && !result.contains(start))
  %7B%0A 
@@ -946,28 +946,8 @@
   %7D%0A
-            return;%0A
     
@@ -1021,74 +1021,48 @@
     
-for (Event e : events) %7B%0A            resetEvents.add(e);%0A        %7D
+Collections.addAll(resetEvents, events);
 %0A   

Simplify code