You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/04/25 22:38:58 UTC

svn commit: r1475949 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/MapUtils.java test/java/org/apache/commons/collections4/MapUtilsTest.java

Author: tn
Date: Thu Apr 25 20:38:58 2013
New Revision: 1475949

URL: http://svn.apache.org/r1475949
Log:
[COLLECTIONS-263] Added MapUtils#populateMap(MultiMap, ...) methods.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1475949&r1=1475948&r2=1475949&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Thu Apr 25 20:38:58 2013
@@ -334,6 +334,10 @@
     <action issue="COLLECTIONS-265" dev="bayard" type="update" due-to="David Saff">
       "TreeBag" will now only accept "Comparable" objects as input when used with natural ordering.
     </action>
+    <action issue="COLLECTIONS-263" dev="tn" type="add" due-to="John Hunsley">
+      Added methods "MapUtils#populateMap(MultiMap, ...)" to support also "MultiMap" instances
+      as input.
+    </action>
     <action issue="COLLECTIONS-262" dev="bayard" type="fix" due-to="Lisen Mu">
       Fixed javadoc for methods "firstKey()" and "lastKey()" in class "AbstractLinkedMap".
     </action>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java?rev=1475949&r1=1475948&r2=1475949&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java Thu Apr 25 20:38:58 2013
@@ -1705,6 +1705,45 @@ public class MapUtils {
     }
 
     /**
+     * Populates a MultiMap using the supplied <code>Transformer</code> to transform the collection
+     * values into keys, using the unaltered collection value as the value in the <code>MultiMap</code>.
+     *
+     * @param <K>  the key type
+     * @param <V>  the value type
+     * @param map the <code>MultiMap</code> to populate.
+     * @param collection the <code>Collection</code> to use as input values for the map.
+     * @param keyTransformer the <code>Transformer</code> used to transform the collection value into a key value
+     * @throws NullPointerException if the map, collection or transformer are null
+     */
+    public static <K, V> void populateMap(final MultiMap<K, V> map, final Collection<? extends V> collection,
+                                          final Transformer<V, K> keyTransformer) {
+        populateMap(map, collection, keyTransformer, TransformerUtils.<V>nopTransformer());
+    }
+    
+    /**
+     * Populates a MultiMap using the supplied <code>Transformer</code>s to transform the collection
+     * values into keys and values.
+     *
+     * @param <K>  the key type
+     * @param <V>  the value type
+     * @param <E>  the type of object contained in the {@link Collection}
+     * @param map the <code>MultiMap</code> to populate.
+     * @param collection the <code>Collection</code> to use as input values for the map.
+     * @param keyTransformer the <code>Transformer</code> used to transform the collection value into a key value
+     * @param valueTransformer the <code>Transformer</code> used to transform the collection value into a value
+     * @throws NullPointerException if the map, collection or transformers are null
+     */
+    public static <K, V, E> void populateMap(final MultiMap<K, V> map, final Collection<? extends E> collection, 
+                                             final Transformer<E, K> keyTransformer, 
+                                             final Transformer<E, V> valueTransformer) {
+        final Iterator<? extends E> iter = collection.iterator();
+        while (iter.hasNext()) {
+            final E temp = iter.next();
+            map.put(keyTransformer.transform(temp), valueTransformer.transform(temp));
+        }
+    }
+
+    /**
      * Get the specified {@link Map} as an {@link IterableMap}.
      *
      * @param <K>  the key type

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java?rev=1475949&r1=1475948&r2=1475949&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java Thu Apr 25 20:38:58 2013
@@ -18,6 +18,7 @@ package org.apache.commons.collections4;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -44,6 +45,7 @@ import org.apache.commons.collections4.k
 import org.apache.commons.collections4.keyvalue.DefaultMapEntry;
 import org.apache.commons.collections4.map.HashedMap;
 import org.apache.commons.collections4.map.LazyMap;
+import org.apache.commons.collections4.map.MultiValueMap;
 import org.apache.commons.collections4.map.PredicatedMap;
 
 /**
@@ -789,6 +791,48 @@ public class MapUtilsTest extends BulkTe
         }
     }
 
+    /**
+     * Test class for populateMap(MultiMap). 
+     */
+    public static class X implements Comparable<X> {
+        int key;
+        String name;
+
+        public X(int key, String name) {
+            this.key = key;
+            this.name = name;
+        }
+
+        public int compareTo(X o) {
+            return key - o.key;
+        }
+        
+    }
+
+    public void testPopulateMultiMap() {
+        // Setup Test Data
+        final List<X> list = new ArrayList<X>();
+        list.add(new X(1, "x1"));
+        list.add(new X(2, "x2"));
+        list.add(new X(2, "x3"));        
+        list.add(new X(5, "x4"));
+        list.add(new X(5, "x5"));
+
+        // Now test key transform population
+        final MultiValueMap<Integer, X> map = MultiValueMap.multiValueMap(new TreeMap<Integer, Collection<X>>());
+        MapUtils.populateMap(map, list, new Transformer<X, Integer>() {
+            public Integer transform(X input) {
+                return input.key;
+            }
+        }, TransformerUtils.<X> nopTransformer());
+        assertEquals(list.size(), map.totalSize());
+
+        for (int i = 0; i < list.size(); i++) {
+            assertEquals(true, map.containsKey(list.get(i).key));
+            assertEquals(true, map.containsValue(list.get(i)));
+        }
+    }
+
     public void testIterableMap() {
         try {
             MapUtils.iterableMap(null);