I have the following directory structures:
/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/1.2.3/
/path/to/stuff/org/foo/bar/1.2.3/myfile.ext
/path/to/stu
You are invoking the method printIfArtifactVersionDirectory
for all visited directories. I did a little change to make it obvious:
static void printIfArtifactVersionDirectory(Path path) {
System.out.println("--- " + path);
...
}
With that additional output you will get:
--- C:\Projects\stuff
--- C:\Projects\stuff\org
--- C:\Projects\stuff\org\foo
--- C:\Projects\stuff\org\foo\bar
--- C:\Projects\stuff\org\foo\bar\1.2.3
C:\Projects\stuff\org\foo\bar
--- C:\Projects\stuff\org\foo\bar\1.2.4
C:\Projects\stuff\org\foo\bar
--- C:\Projects\stuff\org\foo\bar\blah
--- C:\Projects\stuff\org\foo\bar\blah\2.1
C:\Projects\stuff\org\foo\bar\blah
--- C:\Projects\stuff\org\foo\bar\blah\2.2
C:\Projects\stuff\org\foo\bar\blah
So you get the output as often as you have artifact version directories. If you want to remember that you already did the output for one directory, you must make store this information somewhere. One quick implementation could be:
static class Foo {
private static final Set visited = new HashSet<>();
static void printIfArtifactVersionDirectory(Path path) {
...
Path parent = path.getParent();
if (!filePaths.isEmpty() && !visited.contains(parent)) {
visited.add(parent);
System.out.println(parent);
}
}
}
With this you get the expected output:
C:\Projects\stuff\org\foo\bar
C:\Projects\stuff\org\foo\bar\blah
A better solution would be to use the set for storing the visited parents and only print them after visiting them all:
static class PathStore {
private final Set store = new HashSet<>();
void visit(Path path) {
File f = path.toAbsolutePath().toFile();
List filePaths = Arrays.asList(f.list(new MyExtFilenameFilter()));
if (!filePaths.isEmpty()) {
store.add(path.getParent());
}
}
void print() {
store.forEach(System.out::println);
}
}
Usage:
PathStore pathStore = new PathStore();
Files.walk(Paths.get("/path/to/stuff/"))
.filter(Files::isDirectory)
.forEach(pathStore::visit);
pathStore.print();