diff --git a/pom.xml b/pom.xml index dea4b4b..548880a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ me.bvn13 fsm - 2.1-SNAPSHOT + 2.1.1-SNAPSHOT jar diff --git a/src/main/java/me/bvn13/fsm/Fsm.java b/src/main/java/me/bvn13/fsm/Fsm.java index 5c97aef..e2664cc 100644 --- a/src/main/java/me/bvn13/fsm/Fsm.java +++ b/src/main/java/me/bvn13/fsm/Fsm.java @@ -3,7 +3,6 @@ package me.bvn13.fsm; import me.bvn13.fsm.exceptions.AmbiguousTransitionException; import me.bvn13.fsm.exceptions.BrokenTransitionException; import me.bvn13.fsm.exceptions.ConditionAlreadyExistsException; -import me.bvn13.fsm.exceptions.FsmException; import me.bvn13.fsm.exceptions.NotInitializedException; import me.bvn13.fsm.exceptions.StateAlreadyExistsException; import me.bvn13.fsm.exceptions.TransitionMissedException; @@ -16,11 +15,11 @@ import java.util.function.Supplier; /** *

- * Final State Machine
+ * Final State Machine *

*

+ * Each state machine must be prepared with: *

    - * Each state machine must be prepared with: *
  1. Initial state
  2. *
  3. Finish state - may be not several states
  4. *
  5. Intermediate states - optionally
  6. @@ -29,8 +28,8 @@ import java.util.function.Supplier; *

    * *

    + * Each {@link State} may be specified with handlers: *

      - * Each {@link State} may be specified with handlers: *
    1. Before handler - is called right before FSM changes INTO this state
    2. *
    3. After handler - is called right before FSM changes FROM this state to another
    4. *
    5. Processor - the method to process events
    6. @@ -39,7 +38,7 @@ import java.util.function.Supplier; * *

      * Transition is the Rule providing FSM the possibility to change between states. - * + *

      * Each transition must be determined in terms of: *

        *
      1. From State - mandatory
      2. @@ -49,9 +48,11 @@ import java.util.function.Supplier; *
      *

      * - * + *

      * Simple way to use it - to construct an inherited class specified with the type of events to be processed * during transitions. + *

      + * *
        *  {@code
        *  SimpleFsm simpleFsm = Fsm
      @@ -73,8 +74,40 @@ import java.util.function.Supplier;
        *      .checking((fsm, event) -> true)
        *    .end()
        *    .create();
      + *  }
        * 
      * + *

      + * Otherwise you are able to use Old syntax: + *

      + * + *
      + *  {@code
      + *  NamedFsm namedFsm = new NamedFsm().setName("TEST FSM");
      + *  namedFsm.initState(new State("init") {
      + *      @Override
      + *      public void process(String event) {
      + *          initStatedProcessed.set(true);
      + *      }
      + *  });
      + *
      + *  namedFsm.addTransition("init", new State("first", true) {
      + *      @Override
      + *      public void process(String event) {
      + *          firstStatedProcessed.set(true);
      + *      }
      + *  });
      + *
      + *  namedFsm.addTransition("init", new State("another", true) {
      + *      @Override
      + *      public void process(String event) {
      + *          anotherStatedProcessed.set(true);
      + *      }
      + *  }, (fsm, event) -> false);
      + *
      + *  namedFsm.init();
      + *  }
      + * 
      * * {@link SimpleFsm} */ public class Fsm { @@ -83,27 +116,26 @@ public class Fsm { private State initialState; private State currentState; private State previousState; - private final Map> states = new HashMap<>(); - private final Map>> transitions = new HashMap<>(); + private final Map> states = new HashMap<>(); + private final Map>> transitions = new HashMap<>(); /** * Initiate a builder * * @param supplier the original FSM inherited class constructor. You may specify '{@code () -> new SimpleFsm()}' in parameter + * @param the original FSM inherited class type + * @param the class type of Events to be processed * @return FsmBuilder - * @param the original FSM inherited class type - * @param the class type of Events to be processed */ @SuppressWarnings("unchecked") - public static FsmBuilderInitializer from(Supplier supplier) { + public static FsmBuilderInitializer from(Supplier supplier) { return new FsmBuilderInitializer<>(supplier); } /** * To initialize FSM into initial state - * @throws NotInitializedException */ - public void init() throws NotInitializedException { + public void init() { currentState = initialState; if (currentState == null) { throw new NotInitializedException(); @@ -117,10 +149,9 @@ public class Fsm { * Main method to handle every event * * @param event event - * @throws FsmException */ @SuppressWarnings("unchecked") - public void process(E event) throws FsmException { + public void process(E event) { if (done) { return; } @@ -155,9 +186,8 @@ public class Fsm { * To specify initial state * * @param state {@link State} - * @throws FsmException */ - public void initState(State state) throws FsmException { + public void initState(State state) { state.setFSM(this); addState(state); initialState = state; @@ -167,9 +197,8 @@ public class Fsm { * To add another state * * @param state {@link State} - * @throws FsmException */ - public void addState(State state) throws FsmException { + public void addState(State state) { checkStateExist(state.getName()); state.setFSM(this); this.states.put(state.getName(), state); @@ -179,10 +208,9 @@ public class Fsm { * To set the transition up * * @param fromState {@link State} - * @param toState {@link State} - * @throws FsmException + * @param toState {@link State} */ - public void addTransition(String fromState, String toState) throws FsmException { + public void addTransition(String fromState, String toState) { storeTransition(fromState, toState, null); } @@ -190,11 +218,10 @@ public class Fsm { * To set the transition up * * @param fromState {@link State} - * @param toState {@link State} + * @param toState {@link State} * @param condition {@link Condition} - * @throws FsmException */ - public void addTransition(String fromState, String toState, Condition condition) throws FsmException { + public void addTransition(String fromState, String toState, Condition condition) { storeTransition(fromState, toState, condition); } @@ -202,10 +229,9 @@ public class Fsm { * To set the transition up * * @param fromState {@link State} - * @param toState {@link State} - * @throws FsmException + * @param toState {@link State} */ - public void addTransition(String fromState, State toState) throws FsmException { + public void addTransition(String fromState, State toState) { addState(toState); addTransition(fromState, toState.getName()); } @@ -214,11 +240,10 @@ public class Fsm { * To set the transition up * * @param fromState {@link State} - * @param toState {@link State} + * @param toState {@link State} * @param condition {@link Condition} - * @throws FsmException */ - public void addTransition(String fromState, State toState, Condition condition) throws FsmException { + public void addTransition(String fromState, State toState, Condition condition) { addState(toState); addTransition(fromState, toState.getName(), condition); } @@ -227,12 +252,12 @@ public class Fsm { if (!transitions.containsKey(currentState.getName())) { throw new TransitionMissedException(currentState.getName()); } - Map> conditions = transitions.get(currentState.getName()); + Map> conditions = transitions.get(currentState.getName()); List nextStates = new ArrayList<>(); for (String key : conditions.keySet()) { if (conditions.get(key) == null) { nextStates.add(key); - } else if(conditions.get(key).check((T) this, event)) { + } else if (conditions.get(key).check((T) this, event)) { nextStates.add(key); } } @@ -258,7 +283,7 @@ public class Fsm { } } - private void storeTransition(String fromState, String toState, Condition condition) throws FsmException { + private void storeTransition(String fromState, String toState, Condition condition) { if (!transitions.containsKey(fromState)) { transitions.put(fromState, new HashMap<>()); } diff --git a/src/main/java/me/bvn13/fsm/exceptions/ConditionAlreadyExistsException.java b/src/main/java/me/bvn13/fsm/exceptions/ConditionAlreadyExistsException.java index 0f7cfc5..42afed6 100644 --- a/src/main/java/me/bvn13/fsm/exceptions/ConditionAlreadyExistsException.java +++ b/src/main/java/me/bvn13/fsm/exceptions/ConditionAlreadyExistsException.java @@ -1,7 +1,7 @@ package me.bvn13.fsm.exceptions; /** - * is thrown in case of adding a transition FROM->TO, but it is already defined + * is thrown in case of adding a transition FROM->TO, but it is already defined */ public class ConditionAlreadyExistsException extends FsmException { public ConditionAlreadyExistsException(String from, String to) {