From 91c57f098e3feca06c2ca91d4af6c35db5838166 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 4 Sep 2016 19:21:41 +0100 Subject: [PATCH] {Abstract}NodeItem: rewrite findInPath to avoid recursion --- .../java/net/kemitix/node/AbstractNodeItem.java | 15 +++++++++------ src/main/java/net/kemitix/node/NodeItem.java | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 3097d4a..1fe6eab 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -105,14 +105,17 @@ abstract class AbstractNodeItem implements Node { if (path.isEmpty()) { return Optional.empty(); } - Optional> found = findChild(path.get(0)); - if (found.isPresent()) { - if (path.size() > 1) { - return found.get().findInPath(path.subList(1, path.size())); + Node current = this; + for (T item : path) { + final Optional> child = current.findChild(item); + if (child.isPresent()) { + current = child.get(); + } else { + current = null; + break; } - return found; } - return Optional.empty(); + return Optional.ofNullable(current); } @Override diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 2ed9b02..519867f 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -263,14 +263,17 @@ class NodeItem implements Node { if (path.isEmpty()) { return Optional.empty(); } - Optional> found = findChild(path.get(0)); - if (found.isPresent()) { - if (path.size() > 1) { - return found.get().findInPath(path.subList(1, path.size())); + Node current = this; + for (T item : path) { + final Optional> child = current.findChild(item); + if (child.isPresent()) { + current = child.get(); + } else { + current = null; + break; } - return found; } - return Optional.empty(); + return Optional.ofNullable(current); } @Override