Package com.tnsai.fsm

Class State

java.lang.Object
com.tnsai.fsm.State

public class State extends Object
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();
 
  • Method Details

    • builder

      public static State.Builder builder(String name)
      Creates a builder for a state with the given name.
      Parameters:
      name - the state name
      Returns:
      new builder
    • of

      public static State of(String name)
      Creates a simple state with the given name.
      Parameters:
      name - the state name
      Returns:
      new state
    • initial

      public static State initial(String name)
      Creates an initial state with the given name.
      Parameters:
      name - the state name
      Returns:
      new initial state
    • terminal

      public static State terminal(String name)
      Creates a terminal (final) state with the given name.
      Parameters:
      name - the state name
      Returns:
      new terminal state
    • getName

      public String 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

      public StateAction getOnEntry()
      Gets the entry action.
      Returns:
      entry action or null
    • getOnExit

      public StateAction 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

      public <T> T getSpec(String key)
      Gets metadata value by key.
      Type Parameters:
      T - the value type
      Parameters:
      key - the key
      Returns:
      value or null
    • getSpec

      public Map<String,Object> getSpec()
      Gets all metadata.
      Returns:
      unmodifiable metadata map
    • executeOnEntry

      public void executeOnEntry(StateMachineContext context)
      Executes the entry action if present.
      Parameters:
      context - the state machine context
    • executeOnExit

      public void executeOnExit(StateMachineContext context)
      Executes the exit action if present.
      Parameters:
      context - the state machine context
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object