diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index e688355..3f6f6bc 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -1,5 +1,7 @@ package net.kemitix.node; +import lombok.NonNull; + import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -128,10 +130,7 @@ class NodeItem implements Node { * @param child the node to add */ @Override - public void addChild(final Node child) { - if (child == null) { - throw new NullPointerException("child"); - } + public void addChild(@NonNull final Node child) { if (this.equals(child) || isDescendantOf(child)) { throw new NodeException("Child is an ancestor"); } @@ -162,10 +161,7 @@ class NodeItem implements Node { * @return the new child node */ @Override - public Node createChild(final T child) { - if (child == null) { - throw new NullPointerException("child"); - } + public Node createChild(@NonNull final T child) { return new NodeItem<>(child, this); } @@ -184,10 +180,7 @@ class NodeItem implements Node { * @param descendants the line of descendants from the current node */ @Override - public void createDescendantLine(final List descendants) { - if (descendants == null) { - throw new NullPointerException("descendants"); - } + public void createDescendantLine(@NonNull final List descendants) { if (!descendants.isEmpty()) { findOrCreateChild(descendants.get(0)).createDescendantLine( descendants.subList(1, descendants.size())); @@ -204,10 +197,7 @@ class NodeItem implements Node { */ @Override @Deprecated - public Node findOrCreateChild(final T child) { - if (child == null) { - throw new NullPointerException("child"); - } + public Node findOrCreateChild(@NonNull final T child) { return findChild(child).orElseGet(() -> createChild(child)); } @@ -219,10 +209,7 @@ class NodeItem implements Node { * @return an {@link Optional} containing the child node if found */ @Override - public Optional> findChild(final T child) { - if (child == null) { - throw new NullPointerException("child"); - } + public Optional> findChild(@NonNull final T child) { return children.stream().filter(node -> { final Optional d = node.getData(); return d.isPresent() && d.get().equals(child); @@ -257,10 +244,7 @@ class NodeItem implements Node { * @param parent the new parent node */ @Override - public final void setParent(final Node parent) { - if (parent == null) { - throw new NullPointerException("parent"); - } + public final void setParent(@NonNull final Node parent) { if (this.equals(parent) || parent.isDescendantOf(this)) { throw new NodeException("Parent is a descendant"); } @@ -279,10 +263,7 @@ class NodeItem implements Node { * @return the child or null */ @Override - public Optional> findInPath(final List path) { - if (path == null) { - throw new NullPointerException("path"); - } + public Optional> findInPath(@NonNull final List path) { if (path.size() > 0) { Optional> found = findChild(path.get(0)); if (found.isPresent()) { @@ -330,10 +311,7 @@ class NodeItem implements Node { } @Override - public Optional> findChildByName(final String named) { - if (named == null) { - throw new NullPointerException("name"); - } + public Optional> findChildByName(@NonNull final String named) { return children.stream() .filter((Node t) -> t.getName().equals(named)) .findAny();