From c772e90d686d9dd1d43b90cbcca4def924720257 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 10:26:23 +0000 Subject: [PATCH 1/4] 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); + } } From b644fb9dd88a87367082a4c9b0045f7f68e72a4a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 18:16:01 +0000 Subject: [PATCH 2/4] Node:stream{All => }: rename method --- src/main/java/net/kemitix/node/Node.java | 2 +- src/main/java/net/kemitix/node/NodeItem.java | 4 ++-- src/test/java/net/kemitix/node/ImmutableNodeItemTest.java | 4 ++-- src/test/java/net/kemitix/node/NodeItemTest.java | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index ac16cb3..3e70032 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -249,7 +249,7 @@ public interface Node { * * @return a stream of all the nodes in the tree below this node */ - Stream> streamAll(); + Stream> stream(); /** * Returns a stream of all the node's ancestor nodes. diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 2c96f3f..5b75c0a 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -371,9 +371,9 @@ class NodeItem implements Node { } @Override - public Stream> streamAll() { + public Stream> stream() { return Stream.concat(Stream.of(this), getChildren().stream() - .flatMap(Node::streamAll)); + .flatMap(Node::stream)); } @Override diff --git a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java index 6f5491d..7454e05 100644 --- a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java +++ b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java @@ -437,14 +437,14 @@ public class ImmutableNodeItemTest { Nodes.namedChild("eight", "eight", n6); val immutableRoot = Nodes.asImmutable(node); //when - val result = immutableRoot.streamAll() + val result = immutableRoot.stream() .collect(Collectors.toList()); //then assertThat(result).as("full tree") .hasSize(9); // and assertThat(immutableRoot.getChild("one") - .streamAll() + .stream() .collect(Collectors.toList())).as("sub-tree") .hasSize(4); } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 9e8504c..1f25426 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -920,13 +920,13 @@ public class NodeItemTest { val n7 = Nodes.namedChild("seven", "seven", n5); val n8 = Nodes.namedChild("eight", "eight", n6); //when - val result = node.streamAll() + val result = node.stream() .collect(Collectors.toList()); //then assertThat(result).as("full tree") .contains(node, n1, n2, n3, n4, n5, n6, n7, n8); // and - assertThat(n1.streamAll() + assertThat(n1.stream() .collect(Collectors.toList())).as("sub-tree") .containsExactlyInAnyOrder(n1, n3, n5, n7); } From 081dfc7b377aad2cdd270a82366eb8e8a33dcd26 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 18:25:03 +0000 Subject: [PATCH 3/4] Node: {streamParents => parentStream} rename method --- src/main/java/net/kemitix/node/Node.java | 2 +- src/main/java/net/kemitix/node/NodeItem.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 3e70032..4a0e67a 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -256,5 +256,5 @@ public interface Node { * * @return a stream of the node's parents recursively until the root node */ - Stream> streamParents(); + Stream> parentStream(); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5b75c0a..d876c8f 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -377,8 +377,8 @@ class NodeItem implements Node { } @Override - public Stream> streamParents() { - return getParent().map(node -> Stream.concat(Stream.of(node), node.streamParents())) - .orElseGet(Stream::empty); + public Stream> parentStream() { + return findParent().map(node -> Stream.concat(Stream.of(node), node.parentStream())) + .orElseGet(Stream::empty); } } From 21c0fb889683023ec3ee0278e16bfe5bbd060541 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 18 Feb 2017 19:29:01 +0000 Subject: [PATCH 4/4] NodeItemTest: add tests for parentStream() --- .../java/net/kemitix/node/NodeItemTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 1f25426..e9928b6 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -945,4 +945,24 @@ public class NodeItemTest { assertThat(Nodes.unnamedChild(null, root) .isRoot()).isFalse(); } + + @Test + public void parentStream() { + //given + val root = Nodes.namedRoot("root data", "root"); + val child1 = Nodes.namedChild("child 1 data", "child 1", root); + val child2 = Nodes.namedChild("child 2 data", "child 2", root); + val child3 = Nodes.namedChild("child 3 data", "child 3", child2); + //when + val resultRoot = root.parentStream() + .collect(Collectors.toSet()); + val resultChild1 = child1.parentStream() + .collect(Collectors.toSet()); + val resultChild3 = child3.parentStream() + .collect(Collectors.toSet()); + //then + assertThat(resultRoot).isEmpty(); + assertThat(resultChild1).containsExactlyInAnyOrder(root); + assertThat(resultChild3).containsExactlyInAnyOrder(child2, root); + } }