Node: isRoot(): added

This commit is contained in:
Paul Campbell 2017-02-18 18:21:23 +00:00
parent 74a2d57cab
commit 2b4132493a
3 changed files with 27 additions and 0 deletions

View file

@ -82,6 +82,13 @@ public interface Node<T> {
*/ */
boolean isEmpty(); 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. * Fetch the parent node.
* *

View file

@ -113,6 +113,11 @@ class NodeItem<T> implements Node<T> {
return data == null; return data == null;
} }
@Override
public boolean isRoot() {
return parent == null;
}
@Override @Override
public Optional<Node<T>> findParent() { public Optional<Node<T>> findParent() {
return Optional.ofNullable(parent); return Optional.ofNullable(parent);

View file

@ -930,4 +930,19 @@ public class NodeItemTest {
.collect(Collectors.toList())).as("sub-tree") .collect(Collectors.toList())).as("sub-tree")
.containsExactlyInAnyOrder(n1, n3, n5, n7); .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();
}
} }