Source:
<code>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(); Iterator<String> nameIter = nodeNames.iterator(); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); String name; <START> if (nameIter.hasNext()) { <END> name = nameIter.next(); } 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(); }</code><technical_language>need iterator yet? if (!nodeNames.isEmpty()) { name = Iterables.get(nodeNames, 0); // nodeNames.iterator().next(); } else..</technical_language>

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(); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); String name; if (!nodeNames.isEmpty()) { name = Iterables.get(nodeNames, 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(); }