Created
July 1, 2025 20:44
-
-
Save gabriel-srodrigues/d5bf1d4c82f9b55de5377ae385037635 to your computer and use it in GitHub Desktop.
State machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Objects; | |
| public enum StateType { | |
| CREATED { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| if (Objects.isNull(nextState) || this.equals(nextState)) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| return nextState; | |
| } | |
| }, | |
| ANALYSIS { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| if (Objects.isNull(nextState) || this.equals(nextState) || CREATED.equals(nextState)) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| return nextState; | |
| } | |
| }, | |
| PENDING { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| if (Objects.isNull(nextState) || this.equals(nextState) || CREATED.equals(nextState)) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| return nextState; | |
| } | |
| }, | |
| PROPOSAL { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| if (Objects.isNull(nextState) || this.equals(nextState) | |
| || CREATED.equals(nextState) || PENDING.equals(nextState)) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| return nextState; | |
| } | |
| }, | |
| APPROVED { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| }, | |
| ARCHIVED { | |
| @Override | |
| public StateType next(StateType nextState) { | |
| throw new RuntimeException("state %s not allowed for actual state: %s".formatted(nextState, this)); | |
| } | |
| }; | |
| public abstract StateType next(StateType nextState); | |
| } | |
| // Ai na chama ai no uso do recurso, tu coloca meuobj.status().next(ARCHIVED); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment