Skip to content

Repository files navigation

SimpleStateMachine BuildPublish to Packages

SimpleStateMachine is a small util to build small state machines in Java

Download

The library is available on JCenter public repository

Gradle

implementation 'ru.romangr:simple-state-machine:0.0.3'

Maven

<dependency>
<groupId>ru.romangr</groupId>
<artifactId>simple-state-machine</artifactId>
<version>0.0.3</version>
<type>pom</type>
</dependency>

Usage

Declare states as a Java enum

enumThreeStates {
ONE,
TWO,
THREE
}

Declare events as a Java enum

enumTwoEvents {
UP,
DOWN
}

Build your state machine

StateMachine<ThreeStates, TwoEvents> stateMachine = StateMachine.<ThreeStates, TwoEvents>builder()
.withStates(ThreeStates.class) // specify a list valid states
.withEvents(TwoEvents.class) // specify a list valid events
.withInitialState(ThreeStates.ONE) // specify initial state // specify an initial state
.withTransition(ThreeStates.ONE, ThreeStates.TWO, TwoEvents.UP) // declare some valid transitions
.withTransition(ThreeStates.TWO, ThreeStates.ONE, TwoEvents.DOWN)
.build();

Pass some event to the state machine

EventAcceptResult<ThreeStates> result = stateMachine.acceptEvent(TwoEvents.UP);

The result of this call consists of a new state and a result of passing the event. Possible results are declared in EventAcceptStatus enum

publicenumEventAcceptStatus {
SUCCESS, // event has been successfully accepted and state machine have made a transitionUNEXPECTED_EVENT// event is not valid for the current state of the state machine, the state hasn't changed
}

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages