make the path tree more generic

browse-v2
Zlatin Balevsky 2022-08-27 21:14:21 +01:00
parent dfc8f9c291
commit fb60085d93
No known key found for this signature in database
GPG Key ID: A72832072D525E41
2 changed files with 9 additions and 9 deletions

View File

@ -38,7 +38,7 @@ class FileCollection {
this.files = files this.files = files
name = files.first().pathElements.first() name = files.first().pathElements.first()
tree = new PathTree(name) tree = new PathTree<FileCollectionItem>(name)
for(FileCollectionItem item : files) { for(FileCollectionItem item : files) {
tree.add(item.pathElements, item) tree.add(item.pathElements, item)
} }
@ -74,7 +74,7 @@ class FileCollection {
throw new InvalidCollectionException("invalid signature") throw new InvalidCollectionException("invalid signature")
name = files.first().pathElements.first() name = files.first().pathElements.first()
tree = new PathTree(name) tree = new PathTree<FileCollectionItem>(name)
for(FileCollectionItem item : files) { for(FileCollectionItem item : files) {
tree.add(item.pathElements, item) tree.add(item.pathElements, item)
} }

View File

@ -1,6 +1,6 @@
package com.muwire.core.collections package com.muwire.core.collections
class PathTree { class PathTree<T> {
private final Map<PathNode.Key, PathNode> keyToNode = new HashMap<>() private final Map<PathNode.Key, PathNode> keyToNode = new HashMap<>()
@ -11,7 +11,7 @@ class PathTree {
keyToNode.put(this.root.key(), this.root) keyToNode.put(this.root.key(), this.root)
} }
void add(List<String> paths, FileCollectionItem item) { void add(List<String> paths, T item) {
PathNode current = null PathNode current = null
for (String path : paths) { for (String path : paths) {
if (current == null) { if (current == null) {
@ -40,7 +40,7 @@ class PathTree {
private void doTraverse(PathNode node, Callback cb) { private void doTraverse(PathNode node, Callback cb) {
if (node.children.isEmpty()) if (node.children.isEmpty())
cb.onFile(node.path) cb.onFile(node.path, node.userObject)
else { else {
cb.onDirectoryEnter(node.path) cb.onDirectoryEnter(node.path)
for (PathNode child : node.children) for (PathNode child : node.children)
@ -49,12 +49,12 @@ class PathTree {
} }
} }
static class PathNode { static class PathNode<T> {
final String path final String path
final PathNode parent final PathNode parent
final Set<PathNode> children = new LinkedHashSet<>() final Set<PathNode> children = new LinkedHashSet<>()
private final int hashCode private final int hashCode
Object userObject T userObject
PathNode(String path, PathNode parent) { PathNode(String path, PathNode parent) {
this.parent = parent this.parent = parent
@ -82,9 +82,9 @@ class PathTree {
} }
} }
public static interface Callback { public static interface Callback<T> {
public void onDirectoryEnter(String name) public void onDirectoryEnter(String name)
public void onDirectoryLeave() public void onDirectoryLeave()
public void onFile(String name) public void onFile(String name, T value)
} }
} }