diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 19351fa..6477681 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -82,6 +82,13 @@ public interface Node { */ boolean isEmpty(); + /** + * Returns true is the node is a root node (has no parent). + * + * @return true is parent is null + */ + boolean isRoot(); + /** * Fetch the parent node. * diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 739df89..70724c5 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -113,6 +113,11 @@ class NodeItem implements Node { return data == null; } + @Override + public boolean isRoot() { + return parent == null; + } + @Override public Optional> findParent() { return Optional.ofNullable(parent); diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 73b7951..9e8504c 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -930,4 +930,19 @@ public class NodeItemTest { .collect(Collectors.toList())).as("sub-tree") .containsExactlyInAnyOrder(n1, n3, n5, n7); } + + @Test + public void isRootWhenRootIsTrue() { + assertThat(Nodes.unnamedRoot(null) + .isRoot()).isTrue(); + } + + @Test + public void isRootWhenNotRootIsFalse() { + //given + val root = Nodes.unnamedRoot(null); + //then + assertThat(Nodes.unnamedChild(null, root) + .isRoot()).isFalse(); + } }