FullUpdateModifier { @VisibleForTesting protected static void renamePrefixKeys(Map<String, Collection<String>> image, String oldKey, String newKey) { Map<String, Set<String>> replacement = new HashMap<>(); for (Iterator<Map.Entry<String, Collection<String>>> it = image.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, Collection<String>> entry = it.next(); String key = entry.getKey(); if (key.startsWith(oldKey)) { String updatedKey = key.replaceAll("^" + oldKey + "(.*)", newKey + "$1"); if (!image.containsKey(updatedKey)) { LOGGER.debug("Rename {} to {}", key, updatedKey); replacement.put(updatedKey, (Set<String>) entry.getValue()); it.remove(); } else { LOGGER.warn("skipping key {} - already present", updatedKey); } } } mergeMaps(image, replacement); } private  FullUpdateModifier();   }
@Test public void testRenamePrefixKeys() throws Exception { String oldKey = "foo."; String newKey = "baz."; String postfix = "bar"; Map<String, Collection<String>> update = new HashMap<>(); update.put(oldKey + postfix , Collections.<String>emptySet()); FullUpdateModifier.renamePrefixKeys(update, oldKey, newKey); assertEquals(1, update.size()); assertTrue(update.containsKey(newKey + postfix)); }
@Test public void testRenameKeysWithConflicts() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put("foo.bar", Collections.<String>emptySet()); update.put("baz.bar", Collections.<String>emptySet()); Map<String, Collection<String>> expected = new HashMap<>(update); FullUpdateModifier.renamePrefixKeys(update, "foo.", "baz."); assertEquals(update, expected); }