feat: 新增断路状态管理器.
This commit is contained in:
@@ -0,0 +1,90 @@
|
|||||||
|
package com.serliunx.statemanagement.manager;
|
||||||
|
|
||||||
|
import com.serliunx.statemanagement.exception.StateException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断路的单向状态管理器
|
||||||
|
* <p> 逻辑与{@link UnidirectionalStateManager}大体相同, 不同的点在于:
|
||||||
|
* <li> 最后一个状态无法转向第一个状态, 即为一次性的状态管理器.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025/1/13
|
||||||
|
*/
|
||||||
|
public final class BreakageUnidirectionalStateManager<S> extends DefaultUnidirectionalStateManager<S> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否在切换失败时抛出异常
|
||||||
|
*/
|
||||||
|
private final boolean allowThrow;
|
||||||
|
|
||||||
|
public BreakageUnidirectionalStateManager(List<S> stateList, boolean allowThrow) {
|
||||||
|
super(stateList);
|
||||||
|
this.allowThrow = allowThrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BreakageUnidirectionalStateManager(S[] states, boolean allowThrow) {
|
||||||
|
super(states);
|
||||||
|
this.allowThrow = allowThrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BreakageUnidirectionalStateManager(List<S> stateList) {
|
||||||
|
this(stateList, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BreakageUnidirectionalStateManager(S[] states) {
|
||||||
|
this(states, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public S switchNextAndGet() {
|
||||||
|
if (isLast()) {
|
||||||
|
if (allowThrow)
|
||||||
|
throw new StateException("The last state has been reached and cannot be switched again!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return super.switchNextAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public S getAndSwitchNext() {
|
||||||
|
if (isLast()) {
|
||||||
|
if (allowThrow)
|
||||||
|
throw new StateException("The last state has been reached and cannot be switched again!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return super.getAndSwitchNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void switchNext() {
|
||||||
|
if (isLast()) {
|
||||||
|
if (allowThrow)
|
||||||
|
throw new StateException("The last state has been reached and cannot be switched again!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.switchNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean switchTo(S state) {
|
||||||
|
if (isLast()) {
|
||||||
|
if (allowThrow)
|
||||||
|
throw new StateException("The last state has been reached and cannot be switched again!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return super.switchTo(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCircle() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
throw new UnsupportedOperationException("Cannot reset state for BreakageUnidirectionalStateManager!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.serliunx.statemanagement.manager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将指定状态管理器标记为循环的状态管理器
|
||||||
|
* <li> 允许单向、双向循环
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025/1/13
|
||||||
|
*/
|
||||||
|
public interface CircleStateManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为循环的状态管理器
|
||||||
|
*
|
||||||
|
* @return 属于循环状态管理器返回真, 否则返回假.
|
||||||
|
*/
|
||||||
|
default boolean isCircle() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ package com.serliunx.statemanagement.manager;
|
|||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
* @since 2024/12/28
|
* @since 2024/12/28
|
||||||
*/
|
*/
|
||||||
public interface UnidirectionalStateManager<S> extends StateManager<S> {
|
public interface UnidirectionalStateManager<S> extends StateManager<S>, CircleStateManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 切换至下一个状态并返回切换后的状态
|
* 切换至下一个状态并返回切换后的状态
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.serliunx.statemanagement;
|
package com.serliunx.statemanagement;
|
||||||
|
|
||||||
|
import com.serliunx.statemanagement.manager.BreakageUnidirectionalStateManager;
|
||||||
import com.serliunx.statemanagement.manager.DefaultUnidirectionalStateManager;
|
import com.serliunx.statemanagement.manager.DefaultUnidirectionalStateManager;
|
||||||
import com.serliunx.statemanagement.manager.UnidirectionalStateManager;
|
import com.serliunx.statemanagement.manager.UnidirectionalStateManager;
|
||||||
import com.serliunx.statemanagement.support.PrinterState;
|
import com.serliunx.statemanagement.support.PrinterState;
|
||||||
@@ -24,4 +25,11 @@ public class ManagerTest {
|
|||||||
System.out.println(unidirectionalStateManager.switchTo(PrinterState.IDLE));
|
System.out.println(unidirectionalStateManager.switchTo(PrinterState.IDLE));
|
||||||
System.out.println(unidirectionalStateManager.switchTo(PrinterState.SCANNING));
|
System.out.println(unidirectionalStateManager.switchTo(PrinterState.SCANNING));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBreakageUnidirectionalStateManager() {
|
||||||
|
UnidirectionalStateManager<PrinterState> bum = new BreakageUnidirectionalStateManager<>(PrinterState.values());
|
||||||
|
System.out.println(bum.isCircle());
|
||||||
|
bum.switchNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user