From 2b4132493a7334708fd5bb0c19eac511b6211778 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 18:21:23 +0000 Subject: [PATCH] Node: isRoot(): added --- src/main/java/net/kemitix/node/Node.java | 7 +++++++ src/main/java/net/kemitix/node/NodeItem.java | 5 +++++ src/test/java/net/kemitix/node/NodeItemTest.java | 15 +++++++++++++++ 3 files changed, 27 insertions(+) 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(); + } }