FilePathCollapser extends AbstractPathCollapser implements StringCollapser { @Override public <T> Map<String, Collection<T>> collapse(Map<String, Collection<T>> input) { Map<PathName, Collection<T>> inputMap = new HashMap<PathName, Collection<T>>(input.size()); for (Map.Entry<String, Collection<T>> fileEntry : input.entrySet()) { inputMap.put(new PathName(fileEntry.getKey().split("/"), null), fileEntry.getValue()); } Map<PathName, Collection<T>> outputMap = collapsePaths(inputMap); Map<String, Collection<T>> output = new HashMap<String, Collection<T>>(outputMap.size()); for (Map.Entry<PathName, Collection<T>> pathEntry : outputMap.entrySet()) { output.put(StringUtil.join("/", pathEntry.getKey().getPathComponents()), pathEntry.getValue()); } return output; } FilePathCollapser(int collapseThreshold); @Override Map<String, Collection<T>> collapse(Map<String, Collection<T>> input); boolean pathNameMatches(String matcher, String matchee);  }
@Test public void testRemoveRedundantPaths() { final FilePathCollapser collapser = new FilePathCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "/tmp/a/b/*", "/tmp/a/b/c" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "/tmp/a/b/*" )); output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "/tmp/a/b/*", "/tmp/a/b/c", "/tmp/a/c/*" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "/tmp/a/-" )); }
@Test public void testRemoveDuplicates() { final FilePathCollapser collapser = new FilePathCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap(Arrays.asList( "/tmp/a/b/c", "/tmp/a/b/c" ))).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList( "/tmp/a/b/c" )); }
@Test public void testCollapseToRoot() { Collection<String> props = new FilePathCollapser(2).collapse(nullMap(Arrays.asList( "/a/b", "/a/c", "/b/b", "/b/c"))).keySet(); assertEquals(props, Arrays.asList("/-")); props = new FilePathCollapser(0).collapse(nullMap(Arrays.asList( "/a/b", "/-"))).keySet(); assertEquals(props, Arrays.asList("/-")); props = new FilePathCollapser(0).collapse(nullMap(Arrays.asList( "/-", "/a/b"))).keySet(); assertEquals(props, Arrays.asList("/-")); props = new FilePathCollapser(0).collapse(nullMap(Arrays.asList( "/-", "/-"))).keySet(); assertEquals(props, Arrays.asList("/-")); }