You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/02/15 18:57:27 UTC
svn commit: r1568648 - in
/commons/proper/configuration/branches/immutableNodes/src:
main/java/org/apache/commons/configuration/tree/ImmutableNode.java
test/java/org/apache/commons/configuration/tree/TestImmutableNode.java
Author: oheger
Date: Sat Feb 15 17:57:27 2014
New Revision: 1568648
URL: http://svn.apache.org/r1568648
Log:
Added a method for replacing all children at once.
This is useful for more complex updates of ImmutableNode objects.
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java
Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java?rev=1568648&r1=1568647&r2=1568648&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java Sat Feb 15 17:57:27 2014
@@ -202,6 +202,23 @@ public class ImmutableNode
/**
* Returns a new {@code ImmutableNode} instance which is a copy of this
+ * object, but with the children replaced by the ones in the passed in
+ * collection. With this method all children can be replaced in a single
+ * step. For the collection the same rules apply as for
+ * {@link Builder#addChildren(Collection)}.
+ *
+ * @param newChildren the collection with the new children (may be
+ * <b>null</b>)
+ */
+ public ImmutableNode replaceChildren(Collection<ImmutableNode> newChildren)
+ {
+ Builder builder = new Builder(null, attributes);
+ builder.addChildren(newChildren);
+ return createWithBasicProperties(builder);
+ }
+
+ /**
+ * Returns a new {@code ImmutableNode} instance which is a copy of this
* object, but with the specified attribute set to the given value. If an
* attribute with this name does not exist, it is created now. Otherwise,
* the new value overrides the old one.
Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java?rev=1568648&r1=1568647&r2=1568648&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java Sat Feb 15 17:57:27 2014
@@ -513,4 +513,35 @@ public class TestImmutableNode
ImmutableNode node = createDefaultNode(VALUE);
assertSame("Got different instance", node, node.removeAttribute(ATTR));
}
+
+ /**
+ * Tests whether all children can be replaced at once.
+ */
+ @Test
+ public void testReplaceChildren()
+ {
+ final int childCount = 8;
+ Collection<ImmutableNode> newChildren =
+ new ArrayList<ImmutableNode>(childCount);
+ for (int i = 0; i < childCount; i++)
+ {
+ newChildren.add(createChild());
+ }
+ ImmutableNode node = createDefaultNode(VALUE);
+ ImmutableNode node2 = node.replaceChildren(newChildren);
+ checkUpdatedNode(node, node2);
+ checkChildNodes(node2, newChildren);
+ }
+
+ /**
+ * Tests whether a node's children can be replaced by a null collection.
+ */
+ @Test
+ public void testReplaceChildrenNullCollection()
+ {
+ ImmutableNode node = createDefaultNode(VALUE);
+ ImmutableNode node2 = node.replaceChildren(null);
+ checkUpdatedNode(node, node2);
+ checkChildNodes(node2);
+ }
}