Author: bayard
Date: Fri May 16 22:29:40 2008
New Revision: 657293
URL: http://svn.apache.org/viewvc?rev=657293&view=rev
Log:
Applying Dave Meikle's patch to COLLECTIONS-194 - adding a populateMap method
to MapUtils
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java?rev=657293&r1=657292&r2=657293&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
(original)
+++
commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
Fri May 16 22:29:40 2008
@@ -28,6 +28,7 @@
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 @@
return LazySortedMap.decorate(map, transformerFactory);
}
+ /**
+ * <p>
+ * Populates a Map using the supplied <code>Transformer</code> to
transform the collection
+ * values into keys, using the unaltered collection value as the value in
the <code>Map</code>.
+ * </p>
+ * @param map the <code>Map</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 void populateMap(Map map, Collection collection, Transformer
keyTransformer) {
+ populateMap(map, collection, keyTransformer,
TransformerUtils.nopTransformer());
+ }
+
+ /**
+ * <p>
+ * Populates a Map using the supplied <code>Transformer</code>s to
transform the collection
+ * values into keys and values.
+ * </p>
+ * @param map the <code>Map</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 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));
+ }
+ }
}
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java?rev=657293&r1=657292&r2=657293&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
(original)
+++
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
Fri May 16 22:29:40 2008
@@ -26,6 +26,8 @@
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.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 @@
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))));
+ }
+ }
}