change: 代码整理、定向切换时校验当前状态.
This commit is contained in:
@@ -149,8 +149,16 @@ public class StandardStateMachine<S> extends AbstractStateManager<S> implements
|
||||
|
||||
@Override
|
||||
public boolean switchTo(S state) {
|
||||
int i = indexOf(state);
|
||||
if (i == -1 || i == currentIndex()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
writeLock.lock();
|
||||
// 重新检查
|
||||
if (i == currentIndex()) {
|
||||
return false;
|
||||
}
|
||||
S oldState = get();
|
||||
boolean result = super.switchTo(state);
|
||||
if (result) {
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.serliunx.statemanagement.manager.BidirectionalStateManager;
|
||||
* <li> 切出指定状态时触发 (离开事件)
|
||||
* <li> 从A切换到B状态时触发 (交换事件)
|
||||
* <p>
|
||||
* 推荐使用{@link StateMachineBuilder} 来构建状态机.
|
||||
* 请使用{@link StateMachineBuilder} 来构建状态机.
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
@@ -22,5 +22,12 @@ import com.serliunx.statemanagement.manager.BidirectionalStateManager;
|
||||
*/
|
||||
public interface StateMachine<S> extends BidirectionalStateManager<S> {
|
||||
|
||||
|
||||
/**
|
||||
* 切换到指定状态
|
||||
* <li> 在使用状态机的情况, 仅切换成功才会触发注册的各种事件.
|
||||
*
|
||||
* @param state 新的状态
|
||||
* @return 切换成功返回真, 否则返回假
|
||||
*/
|
||||
boolean switchTo(S state);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.serliunx.statemanagement.machine;
|
||||
|
||||
import com.serliunx.statemanagement.machine.handler.StateHandler;
|
||||
import com.serliunx.statemanagement.machine.handler.StateHandlerWrapper;
|
||||
import com.serliunx.statemanagement.manager.BidirectionalStateManager;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.serliunx.statemanagement.machine.handler;
|
||||
|
||||
import com.serliunx.statemanagement.manager.BidirectionalStateManager;
|
||||
|
||||
/**
|
||||
* 状态处理器入参
|
||||
* <p>
|
||||
|
||||
Reference in New Issue
Block a user