commit
2d7417afc2
3 changed files with 27 additions and 0 deletions
|
@ -82,6 +82,13 @@ public interface Node<T> {
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
|
|
@ -113,6 +113,11 @@ class NodeItem<T> implements Node<T> {
|
|||
return data == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Node<T>> findParent() {
|
||||
return Optional.ofNullable(parent);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue