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/04/05 21:49:30 UTC

svn commit: r1585192 - 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 Apr  5 19:49:29 2014
New Revision: 1585192

URL: http://svn.apache.org/r1585192
Log:
ImmutableNode now allows setting multiple attributes at once.

A method was added which expects a map with attributes to be set. This is
required for more complex update operations.

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=1585192&r1=1585191&r2=1585192&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 Apr  5 19:49:29 2014
@@ -250,6 +250,28 @@ public class ImmutableNode
 
     /**
      * Returns a new {@code ImmutableNode} instance which is a copy of this
+     * object, but with all attributes added defined by the given map. This
+     * method is analogous to {@link #setAttribute(String, Object)}, but all
+     * attributes in the given map are added. If the map is <b>null</b> or
+     * empty, this method has no effect.
+     *
+     * @param newAttributes the map with attributes to be added
+     * @return the new node with these attributes
+     */
+    public ImmutableNode setAttributes(Map<String, ?> newAttributes)
+    {
+        if (newAttributes == null || newAttributes.isEmpty())
+        {
+            return this;
+        }
+
+        Map<String, Object> newAttrs = new HashMap<String, Object>(attributes);
+        newAttrs.putAll(newAttributes);
+        return createWithNewAttributes(newAttrs);
+    }
+
+    /**
+     * Returns a new {@code ImmutableNode} instance which is a copy of this
      * object, but with the specified attribute removed. If there is no
      * attribute with the given name, the same node instance is returned.
      *

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=1585192&r1=1585191&r2=1585192&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 Apr  5 19:49:29 2014
@@ -519,6 +519,70 @@ public class TestImmutableNode
     }
 
     /**
+     * Tests whether multiple attributes can be set.
+     */
+    @Test
+    public void testSetAttributes()
+    {
+        ImmutableNode node = createDefaultNode(VALUE);
+        Map<String, Object> attributes = new HashMap<String, Object>();
+        attributes.put("newAttribute1", "value1");
+        attributes.put("newAttribute2", "value2");
+        ImmutableNode node2 = node.setAttributes(attributes);
+        assertEquals("Wrong number of attributes", attributes.size()
+                + node.getAttributes().size(), node2.getAttributes().size());
+        checkAttributesContained(node2, attributes);
+        checkAttributesContained(node2, node.getAttributes());
+    }
+
+    /**
+     * Helper method for testing whether a node contains all the attributes in
+     * the specified map.
+     *
+     * @param node the node to be checked
+     * @param attributes the map with expected attributes
+     */
+    private static void checkAttributesContained(ImmutableNode node,
+            Map<String, Object> attributes)
+    {
+        for (Map.Entry<String, Object> e : attributes.entrySet())
+        {
+            assertEquals("Wrong attribute value", e.getValue(), node
+                    .getAttributes().get(e.getKey()));
+        }
+    }
+
+    /**
+     * Helper method for testing a setAttributes() operation which has no
+     * effect.
+     *
+     * @param attributes the map with attributes
+     */
+    private void checkSetAttributesNoOp(Map<String, Object> attributes)
+    {
+        ImmutableNode node = createDefaultNode(VALUE);
+        assertSame("Node was changed", node, node.setAttributes(attributes));
+    }
+
+    /**
+     * Tests setAttributes() if an empty map is passed in.
+     */
+    @Test
+    public void testSetAttributesEmpty()
+    {
+        checkSetAttributesNoOp(new HashMap<String, Object>());
+    }
+
+    /**
+     * Tests setAttributes() for null input.
+     */
+    @Test
+    public void testSetAttributesNull()
+    {
+        checkSetAttributesNoOp(null);
+    }
+
+    /**
      * Tests whether an existing attribute can be removed.
      */
     @Test