From c772e90d686d9dd1d43b90cbcca4def924720257 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 10:26:23 +0000 Subject: [PATCH] Node: streamParents(): added --- src/main/java/net/kemitix/node/Node.java | 7 +++++++ src/main/java/net/kemitix/node/NodeItem.java | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 6477681..ac16cb3 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -250,4 +250,11 @@ public interface Node { * @return a stream of all the nodes in the tree below this node */ Stream> 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> streamParents(); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 70724c5..2c96f3f 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -375,4 +375,10 @@ class NodeItem implements Node { return Stream.concat(Stream.of(this), getChildren().stream() .flatMap(Node::streamAll)); } + + @Override + public Stream> streamParents() { + return getParent().map(node -> Stream.concat(Stream.of(node), node.streamParents())) + .orElseGet(Stream::empty); + } }