Add method putAll to put an array of key/value pairs into a map

Bug 30882, suggested by Rafael U. C. Afonso


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131816 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-09-22 23:03:50 +00:00
parent 80850c330d
commit a324ca7a63
3 changed files with 197 additions and 3 deletions

View File

@ -40,6 +40,7 @@ The only new deprecations are ................
<center><h3>ENHANCEMENTS</h3></center>
<ul>
<li>CollectionUtils.addIgnoreNull - Adds to the collection if the value being added is not null [30020]</li>
<li>MapUtils.putAll - Puts an array of key/value pairs into a map [30882]</li>
</ul>
<center><h3>BUG FIXES</h3></center>

View File

@ -67,7 +67,7 @@ import org.apache.commons.collections.map.UnmodifiableSortedMap;
* </ul>
*
* @since Commons Collections 1.0
* @version $Revision: 1.47 $ $Date: 2004/07/17 21:23:59 $
* @version $Revision: 1.48 $ $Date: 2004/09/22 23:03:50 $
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
@ -1073,6 +1073,7 @@ public class MapUtils {
return out;
}
//-----------------------------------------------------------------------
/**
* Protects against adding null values to a map.
* <p>
@ -1098,6 +1099,85 @@ public class MapUtils {
}
}
//-----------------------------------------------------------------------
/**
* Puts all the keys and values from the specified array into the map.
* <p>
* This method is an alternative to the {@link java.util.Map#putAll(java.util.Map)}
* method and constructors. It allows you to build a map from an object array
* of various possible styles.
* <p>
* If the first entry in the object array implements {@link java.util.Map.Entry}
* or {@link KeyValue} then the key and value are added from that object.
* If the first entry in the object array is an object array itself, then
* it is assumed that index 0 in the sub-array is the key and index 1 is the value.
* Otherwise, the array is treated as keys and values in alternate indices.
* <p>
* For example, to create a color map:
* <pre>
* Map colorMap = MapUtils.putAll(new HashMap(), new String[][] {
* {"RED", "#FF0000"},
* {"GREEN", "#00FF00"},
* {"BLUE", "#0000FF"}
* });
* </pre>
* or:
* <pre>
* Map colorMap = MapUtils.putAll(new HashMap(), new String[] {
* "RED", "#FF0000",
* "GREEN", "#00FF00",
* "BLUE", "#0000FF"
* });
* </pre>
* or:
* <pre>
* Map colorMap = MapUtils.putAll(new HashMap(), new Map.Entry[] {
* new DefaultMapEntry("RED", "#FF0000"),
* new DefaultMapEntry("GREEN", "#00FF00"),
* new DefaultMapEntry("BLUE", "#0000FF")
* });
* </pre>
*
* @param map the map to populate, must not be null
* @param array an array to populate from, null ignored
* @return the input map
* @throws NullPointerException if map is null
* @throws IllegalArgumentException if sub-array or entry matching used and an
* entry is invalid
* @throws ClassCaseException if the array contents is mixed
*/
public static Map putAll(Map map, Object[] array) {
map.size(); // force NPE
if (array == null || array.length == 0) {
return map;
}
Object obj = array[0];
if (obj instanceof Map.Entry) {
for (int i = 0; i < array.length; i++) {
Map.Entry entry = (Map.Entry) array[i];
map.put(entry.getKey(), entry.getValue());
}
} else if (obj instanceof KeyValue) {
for (int i = 0; i < array.length; i++) {
KeyValue keyval = (KeyValue) array[i];
map.put(keyval.getKey(), keyval.getValue());
}
} else if (obj instanceof Object[]) {
for (int i = 0; i < array.length; i++) {
Object[] sub = (Object[]) array[i];
if (sub == null || sub.length < 2) {
throw new IllegalArgumentException("Invalid array element: " + i);
}
map.put(sub[0], sub[1]);
}
} else {
for (int i = 0; i < array.length - 1;) {
map.put(array[i++], array[i++]);
}
}
return map;
}
// Map decorators
//-----------------------------------------------------------------------
/**

View File

@ -28,6 +28,8 @@ import java.util.TreeMap;
import junit.framework.Test;
import org.apache.commons.collections.keyvalue.DefaultKeyValue;
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;
@ -35,7 +37,7 @@ import org.apache.commons.collections.map.TestPredicatedMap;
/**
* Tests for MapUtils.
*
* @version $Revision: 1.23 $ $Date: 2004/04/09 14:55:39 $
* @version $Revision: 1.24 $ $Date: 2004/09/22 23:03:50 $
*
* @author Stephen Colebourne
* @author Arun Mammen Thomas
@ -215,6 +217,117 @@ public class TestMapUtils extends BulkTest {
assertEquals( out.get("E"), "5" );
}
public void testPutAll_Map_array() {
try {
MapUtils.putAll(null, null);
fail();
} catch (NullPointerException ex) {}
try {
MapUtils.putAll(null, new Object[0]);
fail();
} catch (NullPointerException ex) {}
Map test = MapUtils.putAll(new HashMap(), new String[0]);
assertEquals(0, test.size());
// sub array
test = MapUtils.putAll(new HashMap(), new String[][] {
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
assertEquals(true, test.containsKey("GREEN"));
assertEquals("#00FF00", test.get("GREEN"));
assertEquals(true, test.containsKey("BLUE"));
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
try {
MapUtils.putAll(new HashMap(), new String[][] {
{"RED", "#FF0000"},
null,
{"BLUE", "#0000FF"}
});
fail();
} catch (IllegalArgumentException ex) {}
try {
MapUtils.putAll(new HashMap(), new String[][] {
{"RED", "#FF0000"},
{"GREEN"},
{"BLUE", "#0000FF"}
});
fail();
} catch (IllegalArgumentException ex) {}
try {
MapUtils.putAll(new HashMap(), new String[][] {
{"RED", "#FF0000"},
{},
{"BLUE", "#0000FF"}
});
fail();
} catch (IllegalArgumentException ex) {}
// flat array
test = MapUtils.putAll(new HashMap(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
assertEquals(true, test.containsKey("GREEN"));
assertEquals("#00FF00", test.get("GREEN"));
assertEquals(true, test.containsKey("BLUE"));
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
test = MapUtils.putAll(new HashMap(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF",
"PURPLE" // ignored
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
assertEquals(true, test.containsKey("GREEN"));
assertEquals("#00FF00", test.get("GREEN"));
assertEquals(true, test.containsKey("BLUE"));
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
// map entry
test = MapUtils.putAll(new HashMap(), new Object[] {
new DefaultMapEntry("RED", "#FF0000"),
new DefaultMapEntry("GREEN", "#00FF00"),
new DefaultMapEntry("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
assertEquals(true, test.containsKey("GREEN"));
assertEquals("#00FF00", test.get("GREEN"));
assertEquals(true, test.containsKey("BLUE"));
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
// key value
test = MapUtils.putAll(new HashMap(), new Object[] {
new DefaultKeyValue("RED", "#FF0000"),
new DefaultKeyValue("GREEN", "#00FF00"),
new DefaultKeyValue("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
assertEquals(true, test.containsKey("GREEN"));
assertEquals("#00FF00", test.get("GREEN"));
assertEquals(true, test.containsKey("BLUE"));
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
}
public void testConvertResourceBundle() {
final Map in = new HashMap( 5 , 1 );
in.put( "1" , "A" );