{Abstract}NodeItem: rewrite findInPath to avoid recursion
This commit is contained in:
parent
6987f927fe
commit
91c57f098e
2 changed files with 18 additions and 12 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue