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
This commit is contained in:
parent
e6d4f46544
commit
015f5a8724
|
@ -28,6 +28,7 @@ import java.util.Properties;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.apache.commons.collections.map.FixedSizeMap;
|
import org.apache.commons.collections.map.FixedSizeMap;
|
||||||
import org.apache.commons.collections.map.FixedSizeSortedMap;
|
import org.apache.commons.collections.map.FixedSizeSortedMap;
|
||||||
|
@ -1642,4 +1643,36 @@ public class MapUtils {
|
||||||
return LazySortedMap.decorate(map, transformerFactory);
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,8 @@ import java.util.Map;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import junit.framework.Test;
|
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.LazyMap;
|
||||||
import org.apache.commons.collections.map.PredicatedMap;
|
import org.apache.commons.collections.map.PredicatedMap;
|
||||||
import org.apache.commons.collections.map.TestPredicatedMap;
|
import org.apache.commons.collections.map.TestPredicatedMap;
|
||||||
|
import org.apache.commons.collections.collection.TestTransformedCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for MapUtils.
|
* Tests for MapUtils.
|
||||||
|
@ -805,4 +808,39 @@ public class TestMapUtils extends BulkTest {
|
||||||
assertEquals(false, MapUtils.isNotEmpty(map));
|
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))));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue