State Machines done easy, in a convenient Java package. Here's an example, you can find more in the testing suite:
import dev.eruizc.sml4j.StateMachine;
import dev.eruizc.sml4j.StateMachineBuilder;
public class LightSwitch {
enum State { ON, OFF };
enum Action { TURN_ON, TURN_OFF };
private final StateMachine<State, Action> sm = new StateMachineBuilder<State, Action>()
.allowTransition(State.OFF, Action.TURN_ON, State.ON) // When off, allow it to turn_on, to change state to on
.allowTransition(State.ON, Action.TURN_OFF, State.OFF) // When on, allow turn_off, to change state to off
.buildFrom(State.OFF); // Starts OFF
public boolean isOn() {
return sm.getState().equals(State.ON);
}
public void turnOn() {
sm.transition(Action.TURN_ON);
}
public void turnOff() {
sm.transition(Action.TURN_OFF);
}
}
<!-- pom.xml -->
<dependency>
<groupId>dev.eruizc</groupId>
<artifactId>sml4j</artifactId>
<version>0.3.0</version>
</dependency>
// build.gradle
dependencies {
implementation('dev.eruizc:sml4j:0.3.0')
}
This project uses Semantic Versioning, keep in mind:
- No breaking changes between MINOR and PATCH versions
- Versions below 1.0.0 are not considered stable, previously mentioned rules might not apply