Index: MapUtils.java
===================================================================
RCS file:  
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/ 
commons/collections/MapUtils.java,v
retrieving revision 1.12
diff -u -r1.12 MapUtils.java
--- MapUtils.java       19 Aug 2002 21:56:18 -0000      1.12
+++ MapUtils.java       12 Oct 2002 20:46:26 -0000
@@ -1166,4 +1166,74 @@
      public static SortedMap lazySortedMap(SortedMap map, Factory  
factory) {
          return new LazySortedMap(map, factory);
      }
+
+
+    /**
+     * Converts the given array into a {@link Map}. Each element of  
the array
+     * must be either a {@link Map.Entry} or an Array, containing at  
least two
+     * elements, where the first element is used as key and the second  
as
+     * value. This method can be used to initialize:
+     *
+     * <pre>
+     * // Create a Map mapping colors.
+     * Map colorMap = MapUtils.asMap(new String[][] {{
+     *     {"RED", "#FF0000"},
+     *     {"GREEN", "#00FF00"},
+     *     {"BLUE", "#0000FF"}});
+     * </pre>
+     *
+     * @param array an array whose elements are either a {@link  
Map.Entry} or
+     * an Array, containing at least two elements.
+     * @throws ArrayIndexOutOfBoundsException if one elment of this  
Array is
+     * itself an Array containing less then two elements.
+     * @throws IllegalArgumentException if the array contains elements  
other
+     * than {@link Map.Entry} and an Array.
+     * @throws NullPointerException if the array contains  
<code>null</code>
+     * elements.
+     * @return a Map that was created from the array.
+     * @see #toMapEntry(Object)
+     */
+    public static Map asMap(Object[] array) {
+        Map map = new HashMap(array.length);
+        for (int i = 0; i < array.length; i++) {
+            Map.Entry entry = toMapEntry(array[i]);
+            map.put(entry.getKey(), entry.getValue());
+        }
+        return map;
+    }
+
+
+    /**
+     * Wraps the {@link Map.Entry} interface around the given object.  
This
+     * method will fail, if the object is neither instance of {@link
+     * Map.Entry} nor an Array (of at least <code>length >= 2</code>).  
If the
+     * object is an Array, then the first element will be used as
+     * key and the second will be used as value.
+     * <p>
+     * <b>Caution:</b> if the Array contains less then two elements, an
+     * {@link ArrayIndexOutOfBoundsException} will not be thrown  
before the
+     * {@link Map.Entry}'s {@link Map.Entry#getKey()} or {@link
+     * Map.Entry#getValue()} methods are used.
+     *
+     * @param object will be used as the core of the {@link  
Map.Entry}. Must
+     * be either an instance of {@link Map.Entry} or an Array of at  
least
+     * <code>length >= 2</code>).
+     * @return the object itself, if it is an instance of {@link  
Map.Entry} or
+     * a new instance of {@link Map.Entry}, if the object is an Array.
+     * @throws IllegalArgumentException if the object is neither  
instance of
+     * {@link Map.Entry} nor an Array.
+     * @throws NullPointerException if the object is <code>null</code>.
+     */
+    public static Map.Entry toMapEntry(Object object) {
+        if (object instanceof Map.Entry) {
+            return (Map.Entry) object;
+        } else if (object.getClass().isArray()) {
+            Object[] array = (Object[]) object;
+            return new DefaultMapEntry(array[0], array[1]);
+        } else {
+            throw new IllegalArgumentException(object
+                    + " is neither of type Map.Entry nor an Array.");
+        }
+    }
+
  }


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to