You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/09/23 01:03:50 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MapUtils.java
scolebourne 2004/09/22 16:03:50
Modified: collections RELEASE-NOTES.html
collections/src/test/org/apache/commons/collections
TestMapUtils.java
collections/src/java/org/apache/commons/collections
MapUtils.java
Log:
Add method putAll to put an array of key/value pairs into a map
Bug 30882, suggested by Rafael U. C. Afonso
Revision Changes Path
1.70 +1 -0 jakarta-commons/collections/RELEASE-NOTES.html
Index: RELEASE-NOTES.html
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/RELEASE-NOTES.html,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- RELEASE-NOTES.html 17 Jul 2004 21:38:33 -0000 1.69
+++ RELEASE-NOTES.html 22 Sep 2004 23:03:50 -0000 1.70
@@ -40,6 +40,7 @@
<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>
1.24 +115 -2 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java
Index: TestMapUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- TestMapUtils.java 9 Apr 2004 14:55:39 -0000 1.23
+++ TestMapUtils.java 22 Sep 2004 23:03:50 -0000 1.24
@@ -28,6 +28,8 @@
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;
@@ -214,7 +216,118 @@
assertEquals( out.get("D"), "4" );
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" );
1.48 +81 -1 jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java
Index: MapUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -r1.47 -r1.48
--- MapUtils.java 17 Jul 2004 21:23:59 -0000 1.47
+++ MapUtils.java 22 Sep 2004 23:03:50 -0000 1.48
@@ -1073,6 +1073,7 @@
return out;
}
+ //-----------------------------------------------------------------------
/**
* Protects against adding null values to a map.
* <p>
@@ -1096,6 +1097,85 @@
} else {
map.put(key, value);
}
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * 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
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org