From fb60085d935bc95c36a9b13314e9127148b287eb Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 27 Aug 2022 21:14:21 +0100 Subject: [PATCH] make the path tree more generic --- .../muwire/core/collections/FileCollection.groovy | 4 ++-- .../com/muwire/core/collections/PathTree.groovy | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) 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 f15ab314..0ccb6858 100644 --- a/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/FileCollection.groovy @@ -38,7 +38,7 @@ class FileCollection { this.files = files name = files.first().pathElements.first() - tree = new PathTree(name) + tree = new PathTree(name) for(FileCollectionItem item : files) { tree.add(item.pathElements, item) } @@ -74,7 +74,7 @@ class FileCollection { throw new InvalidCollectionException("invalid signature") name = files.first().pathElements.first() - tree = new PathTree(name) + tree = new PathTree(name) for(FileCollectionItem item : files) { tree.add(item.pathElements, item) } diff --git a/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy index 3369622f..ce37a93d 100644 --- a/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy @@ -1,6 +1,6 @@ package com.muwire.core.collections -class PathTree { +class PathTree { private final Map keyToNode = new HashMap<>() @@ -11,7 +11,7 @@ class PathTree { keyToNode.put(this.root.key(), this.root) } - void add(List paths, FileCollectionItem item) { + void add(List paths, T item) { PathNode current = null for (String path : paths) { if (current == null) { @@ -40,7 +40,7 @@ class PathTree { private void doTraverse(PathNode node, Callback cb) { if (node.children.isEmpty()) - cb.onFile(node.path) + cb.onFile(node.path, node.userObject) else { cb.onDirectoryEnter(node.path) for (PathNode child : node.children) @@ -49,12 +49,12 @@ class PathTree { } } - static class PathNode { + static class PathNode { final String path final PathNode parent final Set children = new LinkedHashSet<>() private final int hashCode - Object userObject + T userObject PathNode(String path, PathNode parent) { this.parent = parent @@ -82,9 +82,9 @@ class PathTree { } } - public static interface Callback { + public static interface Callback { public void onDirectoryEnter(String name) public void onDirectoryLeave() - public void onFile(String name) + public void onFile(String name, T value) } }