{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()) {
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue