diff --git a/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy b/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy index f821a840..71a09585 100644 --- a/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy @@ -118,68 +118,4 @@ class FileCollection { } os.write(payload) } - - static class PathTree { - - private final Map keyToNode = new HashMap<>() - - final PathNode root - PathTree(String root) { - this.root = new PathNode(root, null) - keyToNode.put(this.root.key(), this.root) - } - - void add(List paths) { - PathNode current = null - for (String path : paths) { - if (current == null) { - if (path == root.path) { - current = root - continue - } - } - - - PathNode child = new PathNode(path, current) - PathNode.Key childKey = child.key() - if (!keyToNode.containsKey(childKey)) { - keyToNode.put(childKey, child) - current.children.add(child) - } - current = keyToNode.get(childKey) - } - } - } - - static class PathNode { - final String path - final PathNode parent - final Set children = new LinkedHashSet<>() - private final int hashCode - - PathNode(String path, PathNode parent) { - this.parent = parent - this.path = path - this.hashCode = Objects.hash(path, parent) - } - - - Key key() { - return new Key() - } - - private class Key { - - private final PathNode node = PathNode.this - - public int hashCode() { - hashCode - } - public boolean equals(Object o) { - Key other = (Key)o - Objects.equals(path, other.node.path) && - Objects.equals(parent, other.node.parent) - } - } - } } diff --git a/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy new file mode 100644 index 00000000..e63a8ec1 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy @@ -0,0 +1,86 @@ +package com.muwire.core.collections + +class PathTree { + + private final Map keyToNode = new HashMap<>() + + final PathNode root + PathTree(String root) { + this.root = new PathNode(root, null) + keyToNode.put(this.root.key(), this.root) + } + + void add(List paths) { + PathNode current = null + for (String path : paths) { + if (current == null) { + if (path == root.path) { + current = root + continue + } + } + + + PathNode child = new PathNode(path, current) + PathNode.Key childKey = child.key() + if (!keyToNode.containsKey(childKey)) { + keyToNode.put(childKey, child) + current.children.add(child) + } + current = keyToNode.get(childKey) + } + } + + public void traverse(Callback cb) { + doTraverse(root, cb) + } + + private void doTraverse(PathNode node, Callback cb) { + if (node.children.isEmpty()) + cb.onFile(node.path) + else { + cb.onDirectoryEnter(node.path) + for (PathNode child : node.children) + doTraverse(child, cb) + cb.onDirectoryLeave() + } + } + + static class PathNode { + final String path + final PathNode parent + final Set children = new LinkedHashSet<>() + private final int hashCode + + PathNode(String path, PathNode parent) { + this.parent = parent + this.path = path + this.hashCode = Objects.hash(path, parent) + } + + + Key key() { + return new Key() + } + + private class Key { + + private final PathNode node = PathNode.this + + public int hashCode() { + hashCode + } + public boolean equals(Object o) { + Key other = (Key)o + Objects.equals(path, other.node.path) && + Objects.equals(parent, other.node.parent) + } + } + } + + public static interface Callback { + public void onDirectoryEnter(String name) + public void onDirectoryLeave() + public void onFile(String name) + } +}