From 015f5a8724928f3086354e37d9887ec1edc2ce80 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sat, 17 May 2008 05:29:40 +0000 Subject: [PATCH] Applying Dave Meikle's patch to COLLECTIONS-194 - adding a populateMap method to MapUtils git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@657293 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/collections/MapUtils.java | 33 ++++++++++++++++ .../commons/collections/TestMapUtils.java | 38 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/java/org/apache/commons/collections/MapUtils.java b/src/java/org/apache/commons/collections/MapUtils.java index 9d3849173..42afd7dfa 100644 --- a/src/java/org/apache/commons/collections/MapUtils.java +++ b/src/java/org/apache/commons/collections/MapUtils.java @@ -28,6 +28,7 @@ import java.util.Properties; import java.util.ResourceBundle; import java.util.SortedMap; import java.util.TreeMap; +import java.util.Collection; import org.apache.commons.collections.map.FixedSizeMap; import org.apache.commons.collections.map.FixedSizeSortedMap; @@ -1642,4 +1643,36 @@ public class MapUtils { return LazySortedMap.decorate(map, transformerFactory); } + /** + *

+ * Populates a Map using the supplied Transformer to transform the collection + * values into keys, using the unaltered collection value as the value in the Map. + *

+ * @param map the Map to populate. + * @param collection the Collection to use as input values for the map. + * @param keyTransformer the Transformer used to transform the collection value into a key value + * @throws NullPointerException if the map, collection or transformer are null + */ + public static void populateMap(Map map, Collection collection, Transformer keyTransformer) { + populateMap(map, collection, keyTransformer, TransformerUtils.nopTransformer()); + } + + /** + *

+ * Populates a Map using the supplied Transformers to transform the collection + * values into keys and values. + *

+ * @param map the Map to populate. + * @param collection the Collection to use as input values for the map. + * @param keyTransformer the Transformer used to transform the collection value into a key value + * @param valueTransformer the Transformer used to transform the collection value into a value + * @throws NullPointerException if the map, collection or transformers are null + */ + public static void populateMap(Map map, Collection collection, Transformer keyTransformer, Transformer valueTransformer) { + Iterator iter = collection.iterator(); + while (iter.hasNext()) { + Object temp = iter.next(); + map.put(keyTransformer.transform(temp), valueTransformer.transform(temp)); + } + } } diff --git a/src/test/org/apache/commons/collections/TestMapUtils.java b/src/test/org/apache/commons/collections/TestMapUtils.java index da178c055..78753481e 100644 --- a/src/test/org/apache/commons/collections/TestMapUtils.java +++ b/src/test/org/apache/commons/collections/TestMapUtils.java @@ -26,6 +26,8 @@ import java.util.Map; import java.util.ResourceBundle; import java.util.Set; import java.util.TreeMap; +import java.util.List; +import java.util.ArrayList; import junit.framework.Test; @@ -34,6 +36,7 @@ import org.apache.commons.collections.keyvalue.DefaultMapEntry; import org.apache.commons.collections.map.LazyMap; import org.apache.commons.collections.map.PredicatedMap; import org.apache.commons.collections.map.TestPredicatedMap; +import org.apache.commons.collections.collection.TestTransformedCollection; /** * Tests for MapUtils. @@ -805,4 +808,39 @@ public class TestMapUtils extends BulkTest { assertEquals(false, MapUtils.isNotEmpty(map)); } + public void testPopulateMap() { + // Setup Test Data + List list = new ArrayList(); + list.add("1"); + list.add("3"); + list.add("5"); + list.add("7"); + list.add("2"); + list.add("4"); + list.add("6"); + + // Now test key transform population + Map map = new HashMap(); + MapUtils.populateMap(map, list, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); + assertEquals(list.size(), map.size()); + + for (int i = 0; i < list.size(); i++) { + assertEquals(true, map.containsKey(new Integer((String) list.get(i)))); + assertEquals(false, map.containsKey(list.get(i))); + assertEquals(true, map.containsValue(list.get(i))); + assertEquals(list.get(i), map.get(new Integer((String) list.get(i)))); + } + + // Now test both Key-Value transform population + map = new HashMap(); + MapUtils.populateMap(map, list, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); + + assertEquals(list.size(), map.size()); + for (int i = 0; i < list.size(); i++) { + assertEquals(true, map.containsKey(new Integer((String) list.get(i)))); + assertEquals(false, map.containsKey(list.get(i))); + assertEquals(true, map.containsValue(new Integer((String) list.get(i)))); + assertEquals(new Integer((String) list.get(i)), map.get(new Integer((String) list.get(i)))); + } + } }