@SuppressWarnings("unchecked")
  private void mergeServiceChecks(UpgradeGroupHolder newHolder, UpgradeGroupHolder oldHolder) {

    LinkedHashSet<StageWrapper> priority = new LinkedHashSet<>();
    LinkedHashSet<StageWrapper> others = new LinkedHashSet<>();

    Set<String> extraKeys = new HashSet<>();
    LinkedHashSet<StageWrapper> extras = new LinkedHashSet<>();

    for (List<StageWrapper> holderItems : new List[] { oldHolder.items, newHolder.items }) {
      for (StageWrapper stageWrapper : holderItems) {
        if (stageWrapper instanceof ServiceCheckStageWrapper) {
          ServiceCheckStageWrapper wrapper = (ServiceCheckStageWrapper) stageWrapper;
          if (wrapper.priority) {
            priority.add(stageWrapper);
          } else {
            others.add(stageWrapper);
          }
        } else {
          // !!! It's a good chance that back-to-back service check groups are adding the
          // same non-service-check wrappers.
          // this should be "equal enough" to prevent them from duplicating on merge
          String key = stageWrapper.toString();
          if (!extraKeys.contains(key)) {
            extras.add(stageWrapper);
            extraKeys.add(key);
          }
        }

      }
    }

    // !!! remove duplicate wrappers that are now in the priority list
    others = new LinkedHashSet<>(CollectionUtils.subtract(others, priority));

    oldHolder.items = Lists.newLinkedList(priority);
    oldHolder.items.addAll(others);
    oldHolder.items.addAll(extras);
  }
Merges two service check groups when they have been orchestrated back-to-back.
@param newHolder   the "new" group holder, which was orchestrated after the "old" one
@param oldHolder   the "old" group holder, which is one that was already orchestrated
ambari-server/src/main/java/org/apache/ambari/server/stack/upgrade/orchestrate/UpgradeHelper.java