commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject 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
Date Sat, 15 Feb 2014 17:57:27 GMT
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);
+    }
 }



Mime
View raw message