Package com.tnsai.fsm
Class State
java.lang.Object
com.tnsai.fsm.State
Represents a state in a Finite State Machine.
States can be initial, final, or intermediate. Each state can have entry and exit actions that execute when entering or leaving the state.
Usage
State idle = State.builder("IDLE")
.initial()
.onEntry(ctx -> log.info("Entering IDLE"))
.onExit(ctx -> log.info("Leaving IDLE"))
.build();
State processing = State.builder("PROCESSING")
.timeout(Duration.ofSeconds(30))
.build();
State completed = State.builder("COMPLETED")
.terminal()
.build();
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic State.BuilderCreates a builder for a state with the given name.booleanvoidexecuteOnEntry(StateMachineContext context) Executes the entry action if present.voidexecuteOnExit(StateMachineContext context) Executes the exit action if present.getName()Gets the state name.Gets the entry action.Gets the exit action.getSpec()Gets all metadata.<T> TGets metadata value by key.longGets the timeout in milliseconds.inthashCode()booleanReturns whether this state has a timeout.static StateCreates an initial state with the given name.booleanReturns whether this is an initial state.booleanReturns whether this is a terminal (final) state.static StateCreates a simple state with the given name.static StateCreates a terminal (final) state with the given name.toString()
-
Method Details
-
builder
Creates a builder for a state with the given name.- Parameters:
name- the state name- Returns:
- new builder
-
of
Creates a simple state with the given name.- Parameters:
name- the state name- Returns:
- new state
-
initial
Creates an initial state with the given name.- Parameters:
name- the state name- Returns:
- new initial state
-
terminal
Creates a terminal (final) state with the given name.- Parameters:
name- the state name- Returns:
- new terminal state
-
getName
Gets the state name.- Returns:
- state name
-
isInitial
public boolean isInitial()Returns whether this is an initial state.- Returns:
- true if initial
-
isTerminal
public boolean isTerminal()Returns whether this is a terminal (final) state.- Returns:
- true if terminal
-
getOnEntry
Gets the entry action.- Returns:
- entry action or null
-
getOnExit
Gets the exit action.- Returns:
- exit action or null
-
getTimeoutMs
public long getTimeoutMs()Gets the timeout in milliseconds.- Returns:
- timeout in ms, 0 if no timeout
-
hasTimeout
public boolean hasTimeout()Returns whether this state has a timeout.- Returns:
- true if timeout is set
-
getSpec
Gets metadata value by key.- Type Parameters:
T- the value type- Parameters:
key- the key- Returns:
- value or null
-
getSpec
Gets all metadata.- Returns:
- unmodifiable metadata map
-
executeOnEntry
Executes the entry action if present.- Parameters:
context- the state machine context
-
executeOnExit
Executes the exit action if present.- Parameters:
context- the state machine context
-
equals
-
hashCode
public int hashCode() -
toString
-