Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Repository files navigation

jfsm

Maven Central Maven Central (snapshot) Codecov Java Version

com.io7m.jfsm

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

Repository Relocation

Development of this project has moved to an open-source but not open-contribution model.

Source code and commits will remain publicly available perpetually, but issues and/or pull requests will be rejected and/or ignored. Additionally, this project will now only be available via a read-only mirror at:

https://codeberg.org/io7m-com/jfsm

jfsm

The jfsm package provides implementations of finite state machines.

Features

  • Mutable enum-based finite state machines.
  • Written in pure Java 17.
  • High coverage test suite.
  • OSGi-ready.
  • JPMS-ready.
  • ISC license.

Usage

Declare a type that will be used to represent the states of the FSM. An enum type or a sealed hierarchy is a good choice.

enum TrafficLight
{
  RED,
  YELLOW,
  GREEN
}

Declare the allowed state transitions and build an FSM:

final FSMEnumMutableBuilderType<TrafficLight> b =
  FSMEnumMutable.builder(TrafficLight.RED);
b.addTransition(TrafficLight.RED, TrafficLight.YELLOW);
b.addTransition(TrafficLight.YELLOW, TrafficLight.GREEN);
b.addTransition(TrafficLight.GREEN, TrafficLight.YELLOW);
b.addTransition(TrafficLight.YELLOW, TrafficLight.RED);
var fsm = b.build();

Execute transitions:

assert TrafficLight.RED == m.current();

m.transition(TrafficLight.YELLOW;
assert TrafficLight.YELLOW == m.current();

m.transition(TrafficLight.GREEN;
assert TrafficLight.GREEN == m.current();

m.transition(TrafficLight.YELLOW;
assert TrafficLight.YELLOW == m.current();

m.transition(TrafficLight.RED;
assert TrafficLight.RED == m.current();

m.transition(TrafficLight.YELLOW;
assert TrafficLight.YELLOW == m.current();

m.transition(TrafficLight.GREEN;
assert TrafficLight.GREEN == m.current();

m.transition(TrafficLight.YELLOW;
assert TrafficLight.YELLOW == m.current();

m.transition(TrafficLight.RED;
assert TrafficLight.RED == m.current();

m.transition(TrafficLight.YELLOW;
assert TrafficLight.YELLOW == m.current();

m.transition(TrafficLight.GREEN;
assert TrafficLight.GREEN == m.current();

The FSM will throw an FSMTransitionException if an attempt is made to perform a transition that is not permitted.

Releases

Used by

Contributors

Languages