feat: 状态机现可提供初始化状态.

This commit is contained in:
2025-03-27 11:32:20 +08:00
parent 5d73a20428
commit f0db621968
7 changed files with 85 additions and 53 deletions

View File

@@ -7,8 +7,6 @@ import com.serliunx.statemanagement.support.PrinterEvent;
import com.serliunx.statemanagement.support.PrinterState;
import org.junit.Test;
import java.util.concurrent.Executors;
/**
* 状态机测试
*
@@ -21,23 +19,12 @@ public class MachineTest {
@Test
public void testStandardStateMachine() throws Exception {
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
.async(false)
.async(true)
.standard()
.exchange(PrinterState.IDLE, PrinterState.SCANNING, h -> {
System.out.println("hello~");
})
.whenLeave(PrinterState.IDLE, h -> {
System.out.println(Thread.currentThread().getName() + ": leave IDLE");
})
.whenEntry(PrinterState.STOPPING, h -> {
System.out.println(Thread.currentThread().getName() + ": entry STOPPING, from " + h.getFrom());
})
.whenEntry(PrinterState.STOPPED, h -> {
System.out.println(Thread.currentThread().getName() + ": entry STOPPED, from " + h.getFrom());
})
.withInitial(PrinterState.STOPPING)
.build();
stateMachine.switchTo(PrinterState.SCANNING);
System.out.println(stateMachine.current());
}
@Test