change: 调整工程结构.
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
package com.serliunx.statemanagement;
|
||||
|
||||
import com.serliunx.statemanagement.machine.ConcurrentStateMachine;
|
||||
import com.serliunx.statemanagement.machine.StateMachine;
|
||||
import com.serliunx.statemanagement.machine.StateMachineBuilder;
|
||||
import com.serliunx.statemanagement.support.PrinterEvent;
|
||||
import com.serliunx.statemanagement.support.PrinterState;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 状态机测试
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/12/28
|
||||
*/
|
||||
public class MachineTest {
|
||||
|
||||
@Test
|
||||
public void testStandardStateMachine() throws Exception {
|
||||
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||
.async(true)
|
||||
.standard()
|
||||
.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());
|
||||
})
|
||||
.whenHappened(PrinterEvent.TURN_ON, m -> {
|
||||
m.switchTo(PrinterState.SCANNING);
|
||||
})
|
||||
.whenHappened(PrinterEvent.TURN_OFF, m -> {
|
||||
if (m.switchTo(PrinterState.STOPPING))
|
||||
m.switchTo(PrinterState.STOPPED);
|
||||
})
|
||||
.build();
|
||||
|
||||
stateMachine.publish(PrinterEvent.TURN_ON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentStateMachine() throws Exception {
|
||||
ConcurrentStateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||
.whenEntry(PrinterState.STOPPING, h -> {
|
||||
System.out.println(1111);
|
||||
})
|
||||
.concurrent()
|
||||
.build();
|
||||
|
||||
System.out.println(stateMachine.compareAndSet(PrinterState.IDLE, PrinterState.STOPPING, true));
|
||||
|
||||
stateMachine.close();
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.serliunx.statemanagement;
|
||||
|
||||
import com.serliunx.statemanagement.manager.BreakageUnidirectionalStateManager;
|
||||
import com.serliunx.statemanagement.manager.DefaultUnidirectionalStateManager;
|
||||
import com.serliunx.statemanagement.manager.UnidirectionalStateManager;
|
||||
import com.serliunx.statemanagement.support.PrinterState;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 状态管理器测试
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/12/28
|
||||
*/
|
||||
public class ManagerTest {
|
||||
|
||||
@Test
|
||||
public void testUnidirectionalStateManager() {
|
||||
UnidirectionalStateManager<PrinterState> unidirectionalStateManager =
|
||||
new DefaultUnidirectionalStateManager<>(PrinterState.values());
|
||||
|
||||
System.out.println(unidirectionalStateManager.switchTo(PrinterState.IDLE));
|
||||
System.out.println(unidirectionalStateManager.switchTo(PrinterState.SCANNING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBreakageUnidirectionalStateManager() {
|
||||
UnidirectionalStateManager<PrinterState> bum = new BreakageUnidirectionalStateManager<>(PrinterState.values());
|
||||
|
||||
while (bum.isSwitchable()) {
|
||||
System.out.println(bum.getAndSwitchNext());
|
||||
}
|
||||
System.out.println(bum.current());
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.serliunx.statemanagement.support;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @since 2025/1/24
|
||||
*/
|
||||
public enum PrinterEvent {
|
||||
|
||||
TURN_ON,
|
||||
|
||||
TURN_OFF,
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.serliunx.statemanagement.support;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/12/28
|
||||
*/
|
||||
public enum PrinterState {
|
||||
IDLE,
|
||||
SCANNING,
|
||||
PRINTING,
|
||||
STOPPING,
|
||||
STOPPED
|
||||
}
|
||||
Reference in New Issue
Block a user