diff --git a/src/main/java/com/serliunx/statemanagement/manager/BreakageUnidirectionalStateManager.java b/src/main/java/com/serliunx/statemanagement/manager/BreakageUnidirectionalStateManager.java new file mode 100644 index 0000000..d454aa5 --- /dev/null +++ b/src/main/java/com/serliunx/statemanagement/manager/BreakageUnidirectionalStateManager.java @@ -0,0 +1,90 @@ +package com.serliunx.statemanagement.manager; + +import com.serliunx.statemanagement.exception.StateException; + +import java.util.List; + +/** + * 断路的单向状态管理器 + *

逻辑与{@link UnidirectionalStateManager}大体相同, 不同的点在于: + *

  • 最后一个状态无法转向第一个状态, 即为一次性的状态管理器. + * + * @author SerLiunx + * @version 1.0.0 + * @since 2025/1/13 + */ +public final class BreakageUnidirectionalStateManager extends DefaultUnidirectionalStateManager { + + /** + * 是否在切换失败时抛出异常 + */ + private final boolean allowThrow; + + public BreakageUnidirectionalStateManager(List stateList, boolean allowThrow) { + super(stateList); + this.allowThrow = allowThrow; + } + + public BreakageUnidirectionalStateManager(S[] states, boolean allowThrow) { + super(states); + this.allowThrow = allowThrow; + } + + public BreakageUnidirectionalStateManager(List 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!"); + } +} diff --git a/src/main/java/com/serliunx/statemanagement/manager/CircleStateManager.java b/src/main/java/com/serliunx/statemanagement/manager/CircleStateManager.java new file mode 100644 index 0000000..c6e86f1 --- /dev/null +++ b/src/main/java/com/serliunx/statemanagement/manager/CircleStateManager.java @@ -0,0 +1,21 @@ +package com.serliunx.statemanagement.manager; + +/** + * 将指定状态管理器标记为循环的状态管理器 + *
  • 允许单向、双向循环 + * + * @author SerLiunx + * @version 1.0.0 + * @since 2025/1/13 + */ +public interface CircleStateManager { + + /** + * 是否为循环的状态管理器 + * + * @return 属于循环状态管理器返回真, 否则返回假. + */ + default boolean isCircle() { + return true; + } +} \ No newline at end of file diff --git a/src/main/java/com/serliunx/statemanagement/manager/UnidirectionalStateManager.java b/src/main/java/com/serliunx/statemanagement/manager/UnidirectionalStateManager.java index 439345e..d2d9f62 100644 --- a/src/main/java/com/serliunx/statemanagement/manager/UnidirectionalStateManager.java +++ b/src/main/java/com/serliunx/statemanagement/manager/UnidirectionalStateManager.java @@ -17,7 +17,7 @@ package com.serliunx.statemanagement.manager; * @version 1.0.0 * @since 2024/12/28 */ -public interface UnidirectionalStateManager extends StateManager { +public interface UnidirectionalStateManager extends StateManager, CircleStateManager { /** * 切换至下一个状态并返回切换后的状态 diff --git a/src/test/java/com/serliunx/statemanagement/ManagerTest.java b/src/test/java/com/serliunx/statemanagement/ManagerTest.java index 134b776..66bc1ad 100644 --- a/src/test/java/com/serliunx/statemanagement/ManagerTest.java +++ b/src/test/java/com/serliunx/statemanagement/ManagerTest.java @@ -1,5 +1,6 @@ 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; @@ -24,4 +25,11 @@ public class ManagerTest { System.out.println(unidirectionalStateManager.switchTo(PrinterState.IDLE)); System.out.println(unidirectionalStateManager.switchTo(PrinterState.SCANNING)); } + + @Test + public void testBreakageUnidirectionalStateManager() { + UnidirectionalStateManager bum = new BreakageUnidirectionalStateManager<>(PrinterState.values()); + System.out.println(bum.isCircle()); + bum.switchNext(); + } }