47 lines
942 B
Java
47 lines
942 B
Java
package it.cavallium.strangedb.server;
|
|
|
|
import java.util.Collections;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public class TreePathWalker {
|
|
private TreePath parts[];
|
|
private int currentPosition;
|
|
|
|
public TreePathWalker(TreePath path) {
|
|
List<TreePath> parts = new LinkedList<>();
|
|
parts.add(path);
|
|
while (path.hasParent()) {
|
|
path = path.getParent();
|
|
parts.add(path);
|
|
}
|
|
Collections.reverse(parts);
|
|
this.parts = parts.toArray(new TreePath[0]);
|
|
currentPosition = this.parts.length - 1;
|
|
}
|
|
|
|
public TreePath walkToRoot() {
|
|
return parts[currentPosition = 0];
|
|
}
|
|
|
|
public TreePath current() {
|
|
return parts[currentPosition];
|
|
}
|
|
|
|
public boolean hasNext() {
|
|
return currentPosition + 1 < parts.length;
|
|
}
|
|
|
|
public boolean hasPrevious() {
|
|
return currentPosition - 1 >= 0;
|
|
}
|
|
|
|
public TreePath next() {
|
|
return parts[++currentPosition];
|
|
}
|
|
|
|
public TreePath previous() {
|
|
return parts[--currentPosition];
|
|
}
|
|
}
|