Stateful is a simple and lightweight event driven state machine library. With Stateful you can implement complex state dependant logic with a clean and simple design.
The project is built using Maven and the artifacts are available from Maven Central.
<dependency>
<groupId>com.github.zevada</groupId>
<artifactId>stateful</artifactId>
<version>1.0.0</version>
</dependency>
enum State {
INIT, RUNNING, COMPLETED
}
enum Event {
RUN, END
}
...
StateMachine<State, Event> stateMachine =
new StateMachineBuilder<State, Event>(State.INIT)
.addTransition(State.INIT, Event.RUN, State.RUNNING)
.addTransition(State.RUNNING, Event.END, State.COMPLETED)
.build();
stateMachine.getState(); // State.INIT
stateMachine.apply(Event.RUN);
stateMachine.getState(); // State.RUNNING
StateMachine<State, Event> stateMachine =
new StateMachineBuilder<State, Event>(State.INIT)
.addTransition(State.INIT, Event.RUN, State.RUNNING)
.onExit(State.INIT, () -> System.out.println("Exiting Init!"))
.onEnter(State.RUNNING, () -> System.out.println("Entering Running!"))
.build();
stateMachine.apply(Event.RUN);