class ClusterElderManager {

  public boolean waitForElder(final InternalDistributedMember desiredElder)
      throws InterruptedException {
    MembershipChangeListener changeListener =
        new MembershipChangeListener();

    clusterDistributionManager.addMembershipListener(changeListener);

    InternalDistributedMember currentElder;

    try {
      while (true) {
        if (clusterDistributionManager.isCloseInProgress()) {
          return false;
        }
        currentElder = getElderCandidate();
        if (desiredElder.equals(currentElder)) {
          return true;
        }
        if (logger.isDebugEnabled()) {
          logger.debug("Expecting Elder to be {} but it is {}.",
              desiredElder, currentElder);
        }
        if (!clusterDistributionManager.isCurrentMember(desiredElder)) {
          return false; // no longer present
        }
        if (!clusterDistributionManager.getId().equals(desiredElder)
            && clusterDistributionManager.getId().equals(currentElder)) {
          // Once we become the elder we no longer allow anyone else to be the
          // elder so don't let them wait anymore.
          return false;
        }

        if (logger.isDebugEnabled()) {
          logger.debug("Waiting for membership to change");
        }
        changeListener.waitForMembershipChange();
      }
    } finally {
      clusterDistributionManager.removeMembershipListener(changeListener);
    }
  }

  public  ClusterElderManager(ClusterDistributionManager clusterDistributionManager);
  public  ClusterElderManager(ClusterDistributionManager clusterDistributionManager,
      Supplier<ElderState> elderStateSupplier);

}