You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/06 18:03:59 UTC
[commons-configuration] 02/05: Add ImmutableNode.stream()
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
commit a38e90c5834e53c6d69fe8d5092228a1315a5c0e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Aug 6 13:47:45 2022 -0400
Add ImmutableNode.stream()
Use ImmutableNode.stream()
---
src/changes/changes.xml | 5 ++++-
.../tree/AbstractImmutableNodeHandler.java | 10 ++--------
.../commons/configuration2/tree/ImmutableNode.java | 12 ++++++++++++
.../commons/configuration2/tree/OverrideCombiner.java | 6 +-----
.../commons/configuration2/tree/TestImmutableNode.java | 16 ++++++++++++++++
5 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8a74641c..0d9eba6e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,9 +30,12 @@
CombinedConfiguration#getKeys() can throw NoSuchElementException.
</action>
<!-- ADD -->
- <action issue="CONFIGURATION-799" type="fix" dev="ggregory" due-to="Xinshiyou, Gary Gregory">
+ <action issue="CONFIGURATION-799" type="add" dev="ggregory" due-to="Xinshiyou, Gary Gregory">
Add DefaultConversionHandler#setListDelimiterHandler(ListDelimiterHandler).
</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">
+ Add ImmutableNode.stream().
+ </action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot">
Bump spotbugs-maven-plugin from 4.7.0.0 to 4.7.1.1 #193, #195.
diff --git a/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java b/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java
index d838b66e..40ec23c3 100644
--- a/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java
+++ b/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java
@@ -16,10 +16,10 @@
*/
package org.apache.commons.configuration2.tree;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
+import java.util.stream.Collectors;
/**
* <p>
@@ -59,13 +59,7 @@ abstract class AbstractImmutableNodeHandler implements NodeHandler<ImmutableNode
*/
@Override
public <C> List<ImmutableNode> getMatchingChildren(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
- final List<ImmutableNode> result = new ArrayList<>(node.getChildren().size());
- node.forEach(c -> {
- if (matcher.matches(c, this, criterion)) {
- result.add(c);
- }
- });
- return Collections.unmodifiableList(result);
+ return Collections.unmodifiableList(node.stream().filter(c -> matcher.matches(c, this, criterion)).collect(Collectors.toList()));
}
/**
diff --git a/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java b/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java
index 836a87a0..03d9821d 100644
--- a/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java
+++ b/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java
@@ -25,6 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
/**
* <p>
@@ -559,6 +561,16 @@ public final class ImmutableNode implements Iterable<ImmutableNode> {
}
}
+ /**
+ * Returns a sequential {@code Stream} with this node as its source.
+ *
+ * @return a sequential {@code Stream} over the elements in this node.
+ * @since 2.9.0
+ */
+ public Stream<ImmutableNode> stream() {
+ return StreamSupport.stream(spliterator(), false);
+ }
+
@Override
public String toString() {
return super.toString() + "(" + nodeName + ")";
diff --git a/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java b/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java
index b1c1abe0..1477b5ee 100644
--- a/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java
+++ b/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java
@@ -64,11 +64,7 @@ public class OverrideCombiner extends NodeCombiner {
// Process nodes from the second structure, which are not contained
// in the first structure
- node2.forEach(child -> {
- if (HANDLER.getChildrenCount(node1, child.getNodeName()) < 1) {
- result.addChild(child);
- }
- });
+ node2.stream().filter(child -> HANDLER.getChildrenCount(node1, child.getNodeName()) < 1).forEach(result::addChild);
// Handle attributes and value
addAttributes(result, node1, node2);
diff --git a/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java b/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java
index c345d07e..04b6759b 100644
--- a/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java
+++ b/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java
@@ -582,4 +582,20 @@ public class TestImmutableNode {
assertTrue(node.getChildren().isEmpty());
assertTrue(node.getAttributes().isEmpty());
}
+
+ /**
+ * Tests streaming children.
+ */
+ @Test
+ public void testStream() {
+ final ImmutableNode node = createDefaultNode(VALUE);
+ final ImmutableNode child2 = new ImmutableNode.Builder().name("child2").create();
+ final ImmutableNode node2 = node.addChild(child2);
+ checkUpdatedNode(node, node2);
+ assertEquals(1, node.stream().count());
+ assertEquals(0, child2.stream().count());
+ assertEquals(2, node2.stream().count());
+ assertEquals(1, node.stream().count());
+ }
+
}