Node: streamParents(): added

This commit is contained in:
Paul Campbell 2017-02-18 10:26:23 +00:00
parent 2d7417afc2
commit c772e90d68
2 changed files with 13 additions and 0 deletions

View file

@ -250,4 +250,11 @@ public interface Node<T> {
* @return a stream of all the nodes in the tree below this node
*/
Stream<Node<T>> streamAll();
/**
* Returns a stream of all the node's ancestor nodes.
*
* @return a stream of the node's parents recursively until the root node
*/
Stream<Node<T>> streamParents();
}

View file

@ -375,4 +375,10 @@ class NodeItem<T> implements Node<T> {
return Stream.concat(Stream.of(this), getChildren().stream()
.flatMap(Node::streamAll));
}
@Override
public Stream<Node<T>> streamParents() {
return getParent().map(node -> Stream.concat(Stream.of(node), node.streamParents()))
.orElseGet(Stream::empty);
}
}