Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 27508 invoked from network); 13 Oct 2002 13:34:48 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 13 Oct 2002 13:34:48 -0000 Received: (qmail 29131 invoked by uid 97); 13 Oct 2002 13:35:32 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 29089 invoked by uid 97); 13 Oct 2002 13:35:31 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 29077 invoked by uid 98); 13 Oct 2002 13:35:31 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: Sun, 13 Oct 2002 15:34:43 +0200 Mime-Version: 1.0 (Apple Message framework v546) Content-Type: multipart/mixed; boundary=Apple-Mail-21-374308622 Subject: [collections] [PATCH] MapUtil From: Moritz Petersen To: Commons Dev Message-Id: <882F93BA-DEB0-11D6-8246-003065C7C8BE@mac.com> X-Mailer: Apple Mail (2.546) X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --Apple-Mail-21-374308622 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed The patch contains: MapUtil#toMap(Object[]) - converts the array to a Map MapUtil#asMap(Object[]) - wraps an ArrayMap around the array MapUtil.ArrayMap - inner class for #asMap(Object[]) TestMapUtil#testToMap() TestMapUtil#testAsMap() Patch was applied: diff -u -r1.13 MapUtils.java diff -u -r1.2 TestMapUtils.java -Moritz. --Apple-Mail-21-374308622 Content-Disposition: attachment; filename=collections-MapUtil-src-patch.txt Content-Transfer-Encoding: 7bit Content-Type: text/plain; x-unix-mode=0644; name="collections-MapUtil-src-patch.txt" Index: src//java/org/apache/commons/collections/MapUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v retrieving revision 1.13 diff -u -r1.13 MapUtils.java --- src//java/org/apache/commons/collections/MapUtils.java 13 Oct 2002 00:38:36 -0000 1.13 +++ src//java/org/apache/commons/collections/MapUtils.java 13 Oct 2002 13:25:02 -0000 @@ -1182,4 +1182,302 @@ 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: + * + *
+     * // Create a Map mapping colors.
+     * Map colorMap = MapUtils.asMap(new String[][] {{
+     *     {"RED", "#FF0000"},
+     *     {"GREEN", "#00FF00"},
+     *     {"BLUE", "#0000FF"}});
+     * 
+ * + * @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. + * @return a Map that was created from the array. + */ + public static Map toMap(Object[] array) { + Map map = new HashMap(array.length); + for (int i = 0; i < array.length; i++) { + Object object = array[i]; + if (object instanceof Map.Entry) { + Map.Entry entry = (Map.Entry) object; + map.put(entry.getKey(), entry.getValue()); + } else if (object instanceof Object[]) { + Object[] entry = (Object[]) object; + map.put(entry[0], entry[1]); + } else { + throw new IllegalArgumentException(" Element (" + + object + + ") is neither of type Map.Entry nor an Array."); + } + } + return map; + } + + + /** + * Returns an immutable Map that is backed up by the given array. + * + * @param array The array that is used to back up the + * returned Map. + * @return An immutable Map, backed up by the given array. + */ + public static Map asMap(Object[] array) { + return new ArrayMap(array); + } + + + /** + * A immutable Map that is backed up by an array. + */ + static class ArrayMap implements Map { + /** + * The backing up array. + */ + private Object[] array; + + /** + * Creates a new ArrayMap, that is backed up by the given array. + * + * @param array the array that backs the Map up. + */ + public ArrayMap(Object[] array) { + if (array == null) { + throw new NullPointerException(); + } + this.array = array; + } + + + /** @see Map#clear() */ + public void clear() { + throw new UnsupportedOperationException(); + } + + + /** @see Map#containsKey(Object) */ + public boolean containsKey(Object key) { + if (key == null) { + for (int i = 0; i < array.length; i++) { + if (null == getKey(array[i])) { + return true; + } + } + } else { + for (int i = 0; i < array.length; i++) { + if (key.equals(getKey(array[i]))) { + return true; + } + } + } + return false; + } + + + /** @see Map#containsValue(Object) */ + public boolean containsValue(Object value) { + if (value == null) { + for (int i = 0; i < array.length; i++) { + if (null == getValue(array[i])) { + return true; + } + } + } else { + for (int i = 0; i < array.length; i++) { + if (value.equals(getValue(array[i]))) { + return true; + } + } + } + return false; + } + + + /** @see Map#entrySet() */ + public Set entrySet() { + Set entrySet = new HashSet(array.length); + for (int i = 0; i < array.length; i++) { + if (array[i] instanceof Map.Entry) { + entrySet.add(array[i]); + } else if (array[i] instanceof Object[]) { + Object[] entry = (Object[]) array[i]; + // TODO: Establish an Map.Entry implementation that is + // directly backed up by the array. + entrySet.add(new DefaultMapEntry(entry[0], entry[1])); + } else { + throw new IllegalArgumentException(" Element (" + + array[i] + + ") is neither of type Map.Entry nor an Array."); + } + } + return entrySet; + } + + + /** @see Map#equals(Object) */ + public boolean equals(Object o) { + if (o instanceof Map) { + return entrySet().equals(((Map) o).entrySet()); + } + return false; + } + + + /** @see Map#get(Object) */ + public Object get(Object key) { + // not sure, if a map is allowed to have a 'null' key... + if (key == null) { + for (int i = 0; i < array.length; i++) { + if (null == getKey(array[i])) { + return getValue(array[i]); + } + } + } else { + for (int i = 0; i < array.length; i++) { + if (key.equals(getKey(array[i]))) { + return getValue(array[i]); + } + } + } + return null; + } + + + /** @see Map#hashCode() */ + public int hashCode() { + int hashCode = 0; + for (int i = 0; i < array.length; i++) { + hashCode += hashCode(array[i]); + } + return hashCode(); + } + + + /** @see Map#isEmpty() */ + public boolean isEmpty() { + return array.length == 0; + } + + + /** @see Map#keySet() */ + public Set keySet() { + Set s = new HashSet(array.length); + for (int i = 0; i < array.length; i++) { + s.add(getKey(array[i])); + } + return s; + } + + + /** @see Map#put(Object, Object) */ + public Object put(Object key, Object value) { + throw new UnsupportedOperationException(); + } + + + /** @see Map#putAll(Map) */ + public void putAll(Map t) { + throw new UnsupportedOperationException(); + } + + + /** @see Map#remove(Object) */ + public Object remove(Object key) { + throw new UnsupportedOperationException(); + } + + + /** @see Map#size() */ + public int size() { + return array.length; + } + + + /** @see Map#values() */ + public Collection values() { + Collection c = new ArrayList(array.length); + for (int i = 0; i < array.length; i++) { + c.add(getValue(array[i])); + } + return c; + } + + + /** + * Returns the value of the object, which is defined as + * follows. + * The given object can be either a Map.Entry or an array with at + * least two elements. + *
    + *
  • If the object is a Map.Entry, then its value is returned.
  • + *
  • If the object is an array, the second element is returned.
  • + *
+ * + * @param object the object must be either a Map.Entry or an array. + * @return the value of the object, as described above. + */ + private Object getValue(Object object) { + if (object instanceof Map.Entry) { + return ((Map.Entry) object).getValue(); + } else if (object instanceof Object[]) { + return ((Object[]) object)[1]; + } else { + throw new IllegalArgumentException(" Element (" + + object + + ") is neither of type Map.Entry nor an Array."); + } + } + + + /** + * Returns the key of the object, which is defined as + * follows. + * The given object can be either a Map.Entry or an array with at + * least two elements. + *
    + *
  • If the object is a Map.Entry, then its key is returned.
  • + *
  • If the object is an array, the first element is returned.
  • + *
+ * + * @param object the object must be either a Map.Entry or an array. + * @return the key of the object, as described above. + */ + private Object getKey(Object object) { + if (object instanceof Map.Entry) { + return ((Map.Entry) object).getKey(); + } else if (object instanceof Object[]) { + return ((Object[]) object)[0]; + } else { + throw new IllegalArgumentException(" Element (" + + object + + ") is neither of type Map.Entry nor an Array."); + } + } + + + /** + * Returns the hash code of the object. It is treated as a Map.Entry + * using {@link #getKey(Object)} and {@link #getValue(Object)}. + * + * @param object the object whose hash code is returned. + * @return the hash code of the object. + * @see Map.Entry#hashCode() + */ + private int hashCode(Object object) { + Object key = getKey(object); + Object value = getValue(object); + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); + } + } } Index: src//test/org/apache/commons/collections/TestMapUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v retrieving revision 1.2 diff -u -r1.2 TestMapUtils.java --- src//test/org/apache/commons/collections/TestMapUtils.java 12 Oct 2002 22:36:22 -0000 1.2 +++ src//test/org/apache/commons/collections/TestMapUtils.java 13 Oct 2002 13:25:02 -0000 @@ -133,7 +133,77 @@ // expected } } + + + public void testToMap() { + Map map = MapUtils.toMap(new String[][] {{"foo", "bar"}, {"hello", "world"}}); + + assertEquals("bar", map.get("foo")); + assertEquals("world", map.get("hello")); + + try { + MapUtils.toMap(new String[][] {{"foo", "bar"}, {"short"}}); + fail("exception expected"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected. + } + + try { + MapUtils.toMap(new Object[] {new Object[] {"foo", "bar"}, "illegal type"}); + fail("exception expected"); + } catch (IllegalArgumentException e) { + // expected. + } + + try { + MapUtils.toMap(new Object[] {new Object[] {"foo", "bar"}, null}); + fail("exception expected"); + } catch (IllegalArgumentException e) { + // expected. + } + + map = MapUtils.toMap(new Object[] {new DefaultMapEntry("foo", "bar")}); + assertEquals("bar", map.get("foo")); + } + + public void testAsMap() { + Map map = MapUtils.asMap(new String[][] {{"foo", "bar"}, {"hello", "world"}}); + + assertEquals("bar", map.get("foo")); + assertEquals("world", map.get("hello")); + + try { + map = MapUtils.asMap(new String[][] {{"foo", "bar"}, {"short"}}); + // access all elements using the hashCode() method. + map.hashCode(); + fail("exception expected"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected. + } + + try { + map = MapUtils.asMap(new Object[] {new Object[] {"foo", "bar"}, "illegal type"}); + // access all elements using the hashCode() method. + map.hashCode(); + fail("exception expected"); + } catch (IllegalArgumentException e) { + // expected. + } + + try { + map = MapUtils.asMap(new Object[] {new Object[] {"foo", "bar"}, null}); + // access all elements using the hashCode() method. + map.hashCode(); + fail("exception expected"); + } catch (IllegalArgumentException e) { + // expected. + } + + map = MapUtils.asMap(new Object[] {new DefaultMapEntry("foo", "bar")}); + assertEquals("bar", map.get("foo")); + } + public BulkTest bulkTestPredicatedMap() { return new TestMap("") { --Apple-Mail-21-374308622 Content-Type: text/plain; charset=us-ascii -- To unsubscribe, e-mail: For additional commands, e-mail: --Apple-Mail-21-374308622--