commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1568646 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/NodeUpdateData.java test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java
Date Sat, 15 Feb 2014 17:56:26 GMT
Author: oheger
Date: Sat Feb 15 17:56:25 2014
New Revision: 1568646

URL: http://svn.apache.org/r1568646
Log:
Added NodeUpdateData class.

Similar to NodeAddData, this class stores all information to perform an update
operation on a node model.

Added:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeUpdateData.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java

Added: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeUpdateData.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeUpdateData.java?rev=1568646&view=auto
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeUpdateData.java
(added)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeUpdateData.java
Sat Feb 15 17:56:25 2014
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.tree;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <p>
+ * A simple data class used by node models to store parameters of an update
+ * operation.
+ * </p>
+ * <p>
+ * The {@code Configuration} interface provides a method for setting the value
+ * of a given key. The passed in value can be a single object or a collection of
+ * values. This makes an update operation rather complicated because a
+ * collection of nodes identified by the passed in key has to be matched to
+ * another collection of values - and both collections can have different sizes.
+ * Therefore, an update operation may involve changing of existing nodes, adding
+ * new nodes (if there are more values than currently existing nodes), and
+ * removing nodes (if there are more existing nodes than provided values). This
+ * class collects all this information making it possible to actually perform
+ * the update based on a passed in instance.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ * @param <T> the type of nodes involved in this update operation
+ */
+public class NodeUpdateData<T>
+{
+    /** The map with the nodes whose value has to be changed. */
+    private final Map<T, Object> changedValues;
+
+    /** The collection with the new values to be added. */
+    private final Collection<Object> newValues;
+
+    /** The collection with the nodes to be removed. */
+    private final Collection<T> removedNodes;
+
+    /** The key of the current update operation. */
+    private final String key;
+
+    /**
+     * Creates a new instance of {@code NodeUpdateData} and initializes all its
+     * properties. All passed in collections are optional and can be
+     * <b>null</b>.
+     *
+     * @param changedValues the map defining the changed values
+     * @param newValues the collection with the new values
+     * @param removedNodes the collection with the nodes to be removed
+     * @param key the key of the update operation
+     */
+    public NodeUpdateData(Map<T, Object> changedValues,
+            Collection<Object> newValues, Collection<T> removedNodes, String
key)
+    {
+        this.changedValues = copyMap(changedValues);
+        this.newValues = copyCollection(newValues);
+        this.removedNodes = copyCollection(removedNodes);
+        this.key = key;
+    }
+
+    /**
+     * Returns an unmodifiable map with the values to be changed. The keys of
+     * the map are the nodes affected, the values are the new values to be
+     * assigned to these nodes.
+     *
+     * @return the map with values to be changed
+     */
+    public Map<T, Object> getChangedValues()
+    {
+        return changedValues;
+    }
+
+    /**
+     * Returns a collection with the values to be newly added. For these values
+     * new nodes have to be created and added under the key stored in this
+     * object.
+     *
+     * @return the collection with new values
+     */
+    public Collection<Object> getNewValues()
+    {
+        return newValues;
+    }
+
+    /**
+     * Adds a collection with the nodes to be removed. These nodes are no longer
+     * needed and have to be removed from the node model processing this
+     * request.
+     *
+     * @return the collection with nodes to be removed
+     */
+    public Collection<T> getRemovedNodes()
+    {
+        return removedNodes;
+    }
+
+    /**
+     * Returns the key for this update operation.
+     *
+     * @return the key for this operation
+     */
+    public String getKey()
+    {
+        return key;
+    }
+
+    /**
+     * Creates an unmodifiable defensive copy of the passed in map which may be
+     * null.
+     *
+     * @param map the map to be copied
+     * @param <K> the type of the keys involved
+     * @param <V> the type of the values involved
+     * @return the unmodifiable copy
+     */
+    private static <K, V> Map<K, V> copyMap(Map<? extends K, ? extends V>
map)
+    {
+        if (map == null)
+        {
+            return Collections.emptyMap();
+        }
+        else
+        {
+            return Collections.unmodifiableMap(new HashMap<K, V>(map));
+        }
+    }
+
+    /**
+     * Creates an unmodifiable defensive copy of the passed in collection with
+     * may be null.
+     *
+     * @param col the collection to be copied
+     * @param <T> the element type of the collection
+     * @return the unmodifiable copy
+     */
+    private static <T> Collection<T> copyCollection(Collection<? extends T>
col)
+    {
+        if (col == null)
+        {
+            return Collections.emptySet();
+        }
+        else
+        {
+            return Collections.unmodifiableCollection(new ArrayList<T>(col));
+        }
+    }
+}

Added: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java?rev=1568646&view=auto
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java
(added)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeUpdateData.java
Sat Feb 15 17:56:25 2014
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.tree;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.junit.Test;
+
+/**
+ * Test class for {@code NodeUpdateData}.
+ *
+ * @version $Id$
+ */
+public class TestNodeUpdateData
+{
+    /**
+     * Tests whether null parameters for collections are converted to empty
+     * collections.
+     */
+    @Test
+    public void testInitNoData()
+    {
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(null, null, null, null);
+        assertTrue("Got changed values", data.getChangedValues().isEmpty());
+        assertTrue("Got new values", data.getNewValues().isEmpty());
+        assertTrue("Got removed nodes", data.getRemovedNodes().isEmpty());
+    }
+
+    /**
+     * Tests whether a defensive copy is created from the changed values.
+     */
+    @Test
+    public void testInitChangedValuesDefensiveCopy()
+    {
+        Map<Object, Object> map = new HashMap<Object, Object>();
+        map.put("test", "value");
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(map, null, null, null);
+        map.put("anotherTest", "anotherValue");
+        Map<Object, Object> changedValues = data.getChangedValues();
+        assertEquals("Wrong number of changed values", 1, changedValues.size());
+        assertEquals("Wrong changed value", "value", changedValues.get("test"));
+    }
+
+    /**
+     * Tests whether a defensive copy is created from the new values.
+     */
+    @Test
+    public void testInitNewValuesDefensiveCopy()
+    {
+        Collection<Object> col = new LinkedList<Object>();
+        col.add(42);
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(null, col, null, null);
+        col.add("anotherValue");
+        Collection<Object> newValues = data.getNewValues();
+        assertEquals("Wrong number of new values", 1, newValues.size());
+        assertEquals("Wrong value", 42, newValues.iterator().next());
+    }
+
+    /**
+     * Tests whether a defensive copy is created from the removed nodes.
+     */
+    @Test
+    public void testInitRemovedNodesDefensiveCopy()
+    {
+        Collection<Object> col = new LinkedList<Object>();
+        col.add("n1");
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(null, null, col, null);
+        col.add("n2");
+        Collection<Object> removedNodes = data.getRemovedNodes();
+        assertEquals("Wrong number of new values", 1, removedNodes.size());
+        assertEquals("Wrong value", "n1", removedNodes.iterator().next());
+    }
+
+    /**
+     * Tests that the map with changed values cannot be modified.
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testGetChangedValuesModify()
+    {
+        Map<Object, Object> map = new HashMap<Object, Object>();
+        map.put("n1", 42);
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(map, null, null, null);
+        data.getChangedValues().put("n2", 43);
+    }
+
+    /**
+     * Tests that the collection with new values cannot be modified.
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testGetNewValuesModify()
+    {
+        Collection<Object> col = new LinkedList<Object>();
+        col.add(42);
+        NodeUpdateData<Object> data =
+                new NodeUpdateData<Object>(null, col, null, null);
+        data.getNewValues().add(43);
+    }
+
+    /**
+     * Tests that the collection with removed nodes cannot be modified.
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testGetRemovedNodesModify()
+    {
+        Collection<String> col = new LinkedList<String>();
+        col.add("n1");
+        NodeUpdateData<String> data =
+                new NodeUpdateData<String>(null, null, col, null);
+        data.getRemovedNodes().add("newNode");
+    }
+}



Mime
View raw message