From 62d9d28fe14f1cd37422156948d6dd02cdf4317b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:47:41 +0100 Subject: [PATCH] {Abstract}NodeItem: reduce complexity of findInPath() --- src/main/java/net/kemitix/node/AbstractNodeItem.java | 11 +++-------- src/main/java/net/kemitix/node/NodeItem.java | 11 +++-------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 7954f1d..5f6e5d5 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -139,14 +139,9 @@ abstract class AbstractNodeItem implements Node { return Optional.empty(); } Node current = this; - for (T item : path) { - final Optional> child = current.findChild(item); - if (child.isPresent()) { - current = child.get(); - } else { - current = null; - break; - } + for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) { + current = current.findChild(path.get(i)) + .orElse(null); } return Optional.ofNullable(current); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5afea63..0e92dfb 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -283,14 +283,9 @@ class NodeItem implements Node { return Optional.empty(); } Node current = this; - for (T item : path) { - final Optional> child = current.findChild(item); - if (child.isPresent()) { - current = child.get(); - } else { - current = null; - break; - } + for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) { + current = current.findChild(path.get(i)) + .orElse(null); } return Optional.ofNullable(current); }