change: 调整状态机实例化参数.
This commit is contained in:
@@ -20,32 +20,7 @@ import java.util.function.Consumer;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> implements StateMachine<S> {
|
public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> implements StateMachine<S> {
|
||||||
|
|
||||||
/**
|
protected final StateMachineContext<S> context;
|
||||||
* 进入事件集合
|
|
||||||
*/
|
|
||||||
protected final Map<S, List<StateHandlerWrapper<S>>> entryHandlers;
|
|
||||||
/**
|
|
||||||
* 离开事件集合
|
|
||||||
*/
|
|
||||||
protected final Map<S, List<StateHandlerWrapper<S>>> leaveHandlers;
|
|
||||||
/**
|
|
||||||
* 交换事件集合
|
|
||||||
*/
|
|
||||||
protected final Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers;
|
|
||||||
/**
|
|
||||||
* 事件注册集合
|
|
||||||
*/
|
|
||||||
protected final Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries;
|
|
||||||
/**
|
|
||||||
* 异步执行器
|
|
||||||
*/
|
|
||||||
protected final Executor executor;
|
|
||||||
/**
|
|
||||||
* 是否异步执行
|
|
||||||
* <p>
|
|
||||||
* 当具体的执行器没有指定是否异步时, 将根据该值决定是否异步执行.
|
|
||||||
*/
|
|
||||||
protected final Boolean async;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认的构造函数
|
* 默认的构造函数
|
||||||
@@ -65,16 +40,12 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
|
|||||||
Boolean async
|
Boolean async
|
||||||
) {
|
) {
|
||||||
super(stateList);
|
super(stateList);
|
||||||
this.entryHandlers = entryHandlers;
|
context = new StateMachineContext<>(entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
||||||
this.leaveHandlers = leaveHandlers;
|
|
||||||
this.exchangeHandlers = exchangeHandlers;
|
|
||||||
this.executor = executor;
|
|
||||||
this.async = async;
|
|
||||||
this.eventRegistries = eventRegistries;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws Exception {
|
public void close() throws Exception {
|
||||||
|
final Executor executor = context.executor;
|
||||||
if (executor == null) {
|
if (executor == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -262,14 +233,14 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
|
|||||||
*/
|
*/
|
||||||
protected final void invokeHandlers(S from, S to) {
|
protected final void invokeHandlers(S from, S to) {
|
||||||
// 触发离开处理器
|
// 触发离开处理器
|
||||||
doInvokeHandlers(leaveHandlers.get(from), from, to);
|
doInvokeHandlers(context.leaveHandlers.get(from), from, to);
|
||||||
|
|
||||||
// 触发进入处理器
|
// 触发进入处理器
|
||||||
doInvokeHandlers(entryHandlers.get(to), from, to);
|
doInvokeHandlers(context.entryHandlers.get(to), from, to);
|
||||||
|
|
||||||
// 触发交换处理器
|
// 触发交换处理器
|
||||||
final String key = from.toString() + "-" + to.toString();
|
final String key = from.toString() + "-" + to.toString();
|
||||||
doInvokeHandlers(exchangeHandlers.get(key), from, to);
|
doInvokeHandlers(context.exchangeHandlers.get(key), from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -285,11 +256,11 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
|
|||||||
return;
|
return;
|
||||||
final StateHandlerProcessParams<S> params = new StateHandlerProcessParams<>(from, to, null);
|
final StateHandlerProcessParams<S> params = new StateHandlerProcessParams<>(from, to, null);
|
||||||
if (hw.getAsync() == null ?
|
if (hw.getAsync() == null ?
|
||||||
(this.async != null && this.async) :
|
(context.async != null && context.async) :
|
||||||
hw.getAsync()) {
|
hw.getAsync()) {
|
||||||
final Executor executor;
|
final Executor executor;
|
||||||
if ((executor = hw.getExecutor() == null ?
|
if ((executor = hw.getExecutor() == null ?
|
||||||
this.executor : hw.getExecutor()) == null)
|
context.executor : hw.getExecutor()) == null)
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
executor.execute(() -> stateHandler.handle(params));
|
executor.execute(() -> stateHandler.handle(params));
|
||||||
} else
|
} else
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class StandardStateMachine<S> extends AbstractStateMachine<S> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void publish(Object event) {
|
public void publish(Object event) {
|
||||||
List<Consumer<StateMachine<S>>> consumers = eventRegistries.get(event);
|
List<Consumer<StateMachine<S>>> consumers = context.eventRegistries.get(event);
|
||||||
if (consumers != null) {
|
if (consumers != null) {
|
||||||
consumers.forEach(consumer -> consumer.accept(this));
|
consumers.forEach(consumer -> consumer.accept(this));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.serliunx.statemanagement.machine;
|
||||||
|
|
||||||
|
import com.serliunx.statemanagement.machine.handler.StateHandlerWrapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025/2/2
|
||||||
|
*/
|
||||||
|
public final class StateMachineContext<S> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进入事件集合
|
||||||
|
*/
|
||||||
|
final Map<S, List<StateHandlerWrapper<S>>> entryHandlers;
|
||||||
|
/**
|
||||||
|
* 离开事件集合
|
||||||
|
*/
|
||||||
|
final Map<S, List<StateHandlerWrapper<S>>> leaveHandlers;
|
||||||
|
/**
|
||||||
|
* 交换事件集合
|
||||||
|
*/
|
||||||
|
final Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers;
|
||||||
|
/**
|
||||||
|
* 事件注册集合
|
||||||
|
*/
|
||||||
|
final Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries;
|
||||||
|
/**
|
||||||
|
* 异步执行器
|
||||||
|
*/
|
||||||
|
final Executor executor;
|
||||||
|
/**
|
||||||
|
* 是否异步执行
|
||||||
|
* <p>
|
||||||
|
* 当具体的执行器没有指定是否异步时, 将根据该值决定是否异步执行.
|
||||||
|
*/
|
||||||
|
final Boolean async;
|
||||||
|
|
||||||
|
StateMachineContext(Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
||||||
|
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
||||||
|
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
||||||
|
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
||||||
|
Executor executor,
|
||||||
|
Boolean async
|
||||||
|
) {
|
||||||
|
this.entryHandlers = entryHandlers;
|
||||||
|
this.leaveHandlers = leaveHandlers;
|
||||||
|
this.exchangeHandlers = exchangeHandlers;
|
||||||
|
this.executor = executor;
|
||||||
|
this.async = async;
|
||||||
|
this.eventRegistries = eventRegistries;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user