{Abstract}NodeItem: rewrite findInPath to avoid recursion

This commit is contained in:
Paul Campbell 2016-09-04 19:21:41 +01:00
parent 6987f927fe
commit 91c57f098e
2 changed files with 18 additions and 12 deletions

View file

@ -105,14 +105,17 @@ abstract class AbstractNodeItem<T> implements Node<T> {
if (path.isEmpty()) {
return Optional.empty();
}
Optional<Node<T>> found = findChild(path.get(0));
if (found.isPresent()) {
if (path.size() > 1) {
return found.get().findInPath(path.subList(1, path.size()));
Node<T> current = this;
for (T item : path) {
final Optional<Node<T>> child = current.findChild(item);
if (child.isPresent()) {
current = child.get();
} else {
current = null;
break;
}
return found;
}
return Optional.empty();
return Optional.ofNullable(current);
}
@Override

View file

@ -263,14 +263,17 @@ class NodeItem<T> implements Node<T> {
if (path.isEmpty()) {
return Optional.empty();
}
Optional<Node<T>> found = findChild(path.get(0));
if (found.isPresent()) {
if (path.size() > 1) {
return found.get().findInPath(path.subList(1, path.size()));
Node<T> current = this;
for (T item : path) {
final Optional<Node<T>> child = current.findChild(item);
if (child.isPresent()) {
current = child.get();
} else {
current = null;
break;
}
return found;
}
return Optional.empty();
return Optional.ofNullable(current);
}
@Override