public ServerGroup merge()

in iep-servergroups/src/main/java/com/netflix/iep/servergroups/ServerGroup.java [172:212]


  public ServerGroup merge(ServerGroup other) {
    if (!id.equals(other.id)) {
      throw new IllegalArgumentException("merging is only supported for the same group");
    }

    Map<String, Instance> otherInstances = new HashMap<>();
    for (Instance i : other.instances) {
      otherInstances.put(i.getNode(), i);
    }

    List<Instance> merged = new ArrayList<>(instances.size());

    // Instances in this group or both groups
    for (Instance i1 : instances) {
      Instance i2 = otherInstances.remove(i1.getNode());
      if (i2 != null) {
        merged.add(i1.merge(i2));
      } else if (UNION_STATUSES.contains(i1.getStatus())) {
        merged.add(i1);
      }
    }

    // Add instances only in the other group
    List<Instance> onlyInOtherGroup = otherInstances.values()
        .stream()
        .filter(i -> UNION_STATUSES.contains(i.getStatus()))
        .collect(Collectors.toList());
    merged.addAll(onlyInOtherGroup);

    // Sort to ensure that ordering doesn't break comparisons
    merged.sort(Comparator.comparing(Instance::getNode));

    return builder()
        .platform(platform)
        .group(group)
        .minSize(Math.max(minSize, other.minSize))
        .maxSize(Math.max(maxSize, other.maxSize))
        .desiredSize(Math.max(desiredSize, other.desiredSize))
        .addInstances(merged)
        .build();
  }