Source:
private Set<NodeMetadata> addTagsAndNamesToInstancesInRegion(Map<String, String> common, Set<String> nodeNames, Set<? extends NodeMetadata> input, String region, String group) { Map<String, ? extends NodeMetadata> instancesById = Maps.uniqueIndex(input, instanceId); ImmutableSet.Builder<NodeMetadata> builder = ImmutableSet.<NodeMetadata> builder(); List<String> namesToUse = Lists.newArrayList(nodeNames); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); String name; if (!namesToUse.isEmpty()) { name = namesToUse.remove(0); } else { name = id.replaceAll(".*-", group + "-"); } Map<String, String> tags = ImmutableMap.<String, String> builder().putAll(common) .put("Name", name).build(); logger.debug(">> applying tags %s to instance %s in region %s", tags, id, region); client.getTagApiForRegion(region).get().applyToResources(tags, ImmutableSet.of(id)); builder.add(addTagsForInstance(tags, instancesById.get(id))); } } else { Iterable<String> ids = instancesById.keySet(); logger.debug(">> applying tags %s to instances %s in region %s", common, ids, region); client.getTagApiForRegion(region).get().applyToResources(common, ids); for (NodeMetadata in : input) builder.add(addTagsForInstance(common, in)); } if (logger.isDebugEnabled()) { Multimap<String, String> filter = new TagFilterBuilder().resourceIds(instancesById.keySet()).build(); FluentIterable<Tag> tags = client.getTagApiForRegion(region).get().filter(filter); logger.debug("<< applied tags in region %s: %s", region, resourceToTagsAsMap(tags)); } return builder.build(); }

Target:
private Set<NodeMetadata> addTagsAndNamesToInstancesInRegion(Map<String, String> common, Set<String> nodeNames, Set<? extends NodeMetadata> input, String region, String group) { Map<String, ? extends NodeMetadata> instancesById = Maps.uniqueIndex(input, instanceId); ImmutableSet.Builder<NodeMetadata> builder = ImmutableSet.<NodeMetadata> builder(); List<String> namesToUse = newArrayList(nodeNames); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); String name; if (!namesToUse.isEmpty()) { name = namesToUse.remove(0); } else { name = id.replaceAll(".*-", group + "-"); } Map<String, String> tags = ImmutableMap.<String, String> builder().putAll(common) .put("Name", name).build(); logger.debug(">> applying tags %s to instance %s in region %s", tags, id, region); client.getTagApiForRegion(region).get().applyToResources(tags, ImmutableSet.of(id)); builder.add(addTagsForInstance(tags, instancesById.get(id))); } } else { Iterable<String> ids = instancesById.keySet(); logger.debug(">> applying tags %s to instances %s in region %s", common, ids, region); client.getTagApiForRegion(region).get().applyToResources(common, ids); for (NodeMetadata in : input) builder.add(addTagsForInstance(common, in)); } if (logger.isDebugEnabled()) { Multimap<String, String> filter = new TagFilterBuilder().resourceIds(instancesById.keySet()).build(); FluentIterable<Tag> tags = client.getTagApiForRegion(region).get().filter(filter); logger.debug("<< applied tags in region %s: %s", region, resourceToTagsAsMap(tags)); } return builder.build(); }