build a tree of paths for UI represention

pull/53/head
Zlatin Balevsky 2020-10-30 22:58:32 +00:00
parent 8de2d05dcb
commit 8cc6c1e1c4
No known key found for this signature in database
GPG Key ID: A72832072D525E41
1 changed files with 68 additions and 0 deletions

View File

@ -21,6 +21,8 @@ class FileCollection {
private final Set<FileCollectionItem> files = new LinkedHashSet<>()
final PathTree tree
FileCollection(long timestamp, Persona author, String comment, Set<FileCollectionItem> files,
SigningPrivateKey spk) {
this.timestamp = timestamp;
@ -28,6 +30,11 @@ class FileCollection {
this.comment = comment
this.files = files
tree = new PathTree(files.first().pathElements.first())
for(FileCollectionItem item : files) {
tree.add(item.pathElements)
}
byte [] signablePayload = signablePayload()
Signature signature = DSAEngine.getInstance().sign(signablePayload, spk)
this.sig = signature.getData()
@ -112,6 +119,67 @@ class FileCollection {
os.write(payload)
}
static class PathTree {
private final Map<PathNode.Key, PathNode> 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<String> 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<PathNode> 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)
}
}
}
}