From 9f3aec202af31a3ef83f59da503e3ac0de099c40 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 4 Sep 2016 18:59:22 +0100 Subject: [PATCH] {Abstract}NodeItem: reduce nested if statements --- .../net/kemitix/node/AbstractNodeItem.java | 15 ++++----- src/main/java/net/kemitix/node/NodeItem.java | 31 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index f7efaf9..3097d4a 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -102,14 +102,15 @@ abstract class AbstractNodeItem implements Node { */ @Override public Optional> findInPath(@NonNull final List path) { - if (path.size() > 0) { - Optional> found = findChild(path.get(0)); - if (found.isPresent()) { - if (path.size() > 1) { - return found.get().findInPath(path.subList(1, path.size())); - } - return found; + 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())); } + return found; } return Optional.empty(); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 812adf4..2ed9b02 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -2,19 +2,15 @@ package net.kemitix.node; import lombok.NonNull; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Function; /** * Represents a tree of nodes. * - * @author Paul Campbell - * * @param the type of data stored in each node + * + * @author Paul Campbell */ class NodeItem implements Node { @@ -147,7 +143,7 @@ class NodeItem implements Node { Optional> childParent = child.getParent(); boolean isOrphan = !childParent.isPresent(); boolean hasDifferentParent = !isOrphan && !childParent.get() - .equals(this); + .equals(this); if (isOrphan || hasDifferentParent) { child.setParent(this); } @@ -264,14 +260,15 @@ class NodeItem implements Node { */ @Override public Optional> findInPath(@NonNull final List path) { - if (path.size() > 0) { - Optional> found = findChild(path.get(0)); - if (found.isPresent()) { - if (path.size() > 1) { - return found.get().findInPath(path.subList(1, path.size())); - } - return found; + 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())); } + return found; } return Optional.empty(); } @@ -315,8 +312,8 @@ class NodeItem implements Node { @Override public Optional> findChildByName(@NonNull final String named) { return children.stream() - .filter((Node t) -> t.getName().equals(named)) - .findAny(); + .filter((Node t) -> t.getName().equals(named)) + .findAny(); } @Override