Skip to content

A simple and lightweight event driven state machine library

License

Notifications You must be signed in to change notification settings

zevada/stateful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Introduction

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.

Installation

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>

Usage

Simple Transitions

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

On State Enter/Exit Listeners

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);

About

A simple and lightweight event driven state machine library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages