[COLLECTIONS-263] Added MapUtils#populateMap(MultiMap, ...) methods.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1475949 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b591dacd5c
commit
766d9add03
|
@ -334,6 +334,10 @@
|
||||||
<action issue="COLLECTIONS-265" dev="bayard" type="update" due-to="David Saff">
|
<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.
|
"TreeBag" will now only accept "Comparable" objects as input when used with natural ordering.
|
||||||
</action>
|
</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">
|
<action issue="COLLECTIONS-262" dev="bayard" type="fix" due-to="Lisen Mu">
|
||||||
Fixed javadoc for methods "firstKey()" and "lastKey()" in class "AbstractLinkedMap".
|
Fixed javadoc for methods "firstKey()" and "lastKey()" in class "AbstractLinkedMap".
|
||||||
</action>
|
</action>
|
||||||
|
|
|
@ -1704,6 +1704,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}.
|
* Get the specified {@link Map} as an {@link IterableMap}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.commons.collections4;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -44,6 +45,7 @@ import org.apache.commons.collections4.keyvalue.DefaultKeyValue;
|
||||||
import org.apache.commons.collections4.keyvalue.DefaultMapEntry;
|
import org.apache.commons.collections4.keyvalue.DefaultMapEntry;
|
||||||
import org.apache.commons.collections4.map.HashedMap;
|
import org.apache.commons.collections4.map.HashedMap;
|
||||||
import org.apache.commons.collections4.map.LazyMap;
|
import org.apache.commons.collections4.map.LazyMap;
|
||||||
|
import org.apache.commons.collections4.map.MultiValueMap;
|
||||||
import org.apache.commons.collections4.map.PredicatedMap;
|
import org.apache.commons.collections4.map.PredicatedMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -789,6 +791,48 @@ public class MapUtilsTest extends BulkTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
public void testIterableMap() {
|
||||||
try {
|
try {
|
||||||
MapUtils.iterableMap(null);
|
MapUtils.iterableMap(null);
|
||||||
|
|
Loading…
Reference in New Issue