class AbstractMessageManager implements MessageManager {

  /**
   * Contain all the instances in the system. Key is its ID, and value is the instance itself.
   */
  protected Map<Integer, Instance> instanceMap;

  /**
   * Constructor of AbstractMessageManager.
   */
  public AbstractMessageManager(Map<Integer, Instance> instanceMap) {
    this.instanceMap = instanceMap;
  }

  /**
   * Find the next instance with smallest ID.
   *
   * @return The next instance.
   */
  protected Instance findNextInstance(int currentId) {<FILL_FUNCTION_BODY>}

}

    Instance result = null;
    var candidateList = instanceMap.keySet()
        .stream()
        .filter((i) -> i > currentId && instanceMap.get(i).isAlive())
        .sorted()
        .toList();
    if (candidateList.isEmpty()) {
      var index = instanceMap.keySet()
          .stream()
          .filter((i) -> instanceMap.get(i).isAlive())
          .sorted()
          .toList()
          .get(0);
      result = instanceMap.get(index);
    } else {
      var index = candidateList.get(0);
      result = instanceMap.get(index);
    }
    return result;
  
<no_super_class>