{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()) { if (path.isEmpty()) {
return Optional.empty(); return Optional.empty();
} }
Optional<Node<T>> found = findChild(path.get(0)); Node<T> current = this;
if (found.isPresent()) { for (T item : path) {
if (path.size() > 1) { final Optional<Node<T>> child = current.findChild(item);
return found.get().findInPath(path.subList(1, path.size())); if (child.isPresent()) {
current = child.get();
} else {
current = null;
break;
} }
return found;
} }
return Optional.empty(); return Optional.ofNullable(current);
} }
@Override @Override

View file

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