code: public void transition(CopycatServer.State state) {
      checkThread();
  
      if (this.state != null && state == this.state.type()) {
        return;
      }
  
      LOGGER.info("{} - Transitioning to {}", cluster.member().address(), state);
  
      // Close the old state.
      try {
        this.state.close().get();
      } catch (InterruptedException | ExecutionException e) {
        throw new IllegalStateException("failed to close Raft state", e);
      }
  
      // Force state transitions to occur synchronously in order to prevent race conditions.
      try {
        this.state = createState(state);
        this.state.open().get();
      } catch (InterruptedException | ExecutionException e) {
        throw new IllegalStateException("failed to initialize Raft state", e);
      }
  
      stateChangeListeners.forEach(l -> l.accept(this.state.type()));
    }
docs: Transition handler.