{Abstract}NodeItem: reduce nested if statements
This commit is contained in:
parent
37247e93bc
commit
9f3aec202a
2 changed files with 22 additions and 24 deletions
|
@ -102,14 +102,15 @@ abstract class AbstractNodeItem<T> implements Node<T> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findInPath(@NonNull final List<T> path) {
|
public Optional<Node<T>> findInPath(@NonNull final List<T> path) {
|
||||||
if (path.size() > 0) {
|
if (path.isEmpty()) {
|
||||||
Optional<Node<T>> found = findChild(path.get(0));
|
return Optional.empty();
|
||||||
if (found.isPresent()) {
|
}
|
||||||
if (path.size() > 1) {
|
Optional<Node<T>> found = findChild(path.get(0));
|
||||||
return found.get().findInPath(path.subList(1, path.size()));
|
if (found.isPresent()) {
|
||||||
}
|
if (path.size() > 1) {
|
||||||
return found;
|
return found.get().findInPath(path.subList(1, path.size()));
|
||||||
}
|
}
|
||||||
|
return found;
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,15 @@ package net.kemitix.node;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a tree of nodes.
|
* Represents a tree of nodes.
|
||||||
*
|
*
|
||||||
* @author Paul Campbell
|
|
||||||
*
|
|
||||||
* @param <T> the type of data stored in each node
|
* @param <T> the type of data stored in each node
|
||||||
|
*
|
||||||
|
* @author Paul Campbell
|
||||||
*/
|
*/
|
||||||
class NodeItem<T> implements Node<T> {
|
class NodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
|
@ -147,7 +143,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
Optional<Node<T>> childParent = child.getParent();
|
Optional<Node<T>> childParent = child.getParent();
|
||||||
boolean isOrphan = !childParent.isPresent();
|
boolean isOrphan = !childParent.isPresent();
|
||||||
boolean hasDifferentParent = !isOrphan && !childParent.get()
|
boolean hasDifferentParent = !isOrphan && !childParent.get()
|
||||||
.equals(this);
|
.equals(this);
|
||||||
if (isOrphan || hasDifferentParent) {
|
if (isOrphan || hasDifferentParent) {
|
||||||
child.setParent(this);
|
child.setParent(this);
|
||||||
}
|
}
|
||||||
|
@ -264,14 +260,15 @@ class NodeItem<T> implements Node<T> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findInPath(@NonNull final List<T> path) {
|
public Optional<Node<T>> findInPath(@NonNull final List<T> path) {
|
||||||
if (path.size() > 0) {
|
if (path.isEmpty()) {
|
||||||
Optional<Node<T>> found = findChild(path.get(0));
|
return Optional.empty();
|
||||||
if (found.isPresent()) {
|
}
|
||||||
if (path.size() > 1) {
|
Optional<Node<T>> found = findChild(path.get(0));
|
||||||
return found.get().findInPath(path.subList(1, path.size()));
|
if (found.isPresent()) {
|
||||||
}
|
if (path.size() > 1) {
|
||||||
return found;
|
return found.get().findInPath(path.subList(1, path.size()));
|
||||||
}
|
}
|
||||||
|
return found;
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
@ -315,8 +312,8 @@ class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findChildByName(@NonNull final String named) {
|
public Optional<Node<T>> findChildByName(@NonNull final String named) {
|
||||||
return children.stream()
|
return children.stream()
|
||||||
.filter((Node<T> t) -> t.getName().equals(named))
|
.filter((Node<T> t) -> t.getName().equals(named))
|
||||||
.findAny();
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue