Skip to content

Instantly share code, notes, and snippets.

@gabriel-srodrigues
Created July 1, 2025 20:44
Show Gist options
  • Select an option

  • Save gabriel-srodrigues/d5bf1d4c82f9b55de5377ae385037635 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-srodrigues/d5bf1d4c82f9b55de5377ae385037635 to your computer and use it in GitHub Desktop.
State machine
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