Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 10877 invoked from network); 14 Mar 2004 17:05:28 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 14 Mar 2004 17:05:28 -0000 Received: (qmail 46585 invoked by uid 500); 14 Mar 2004 17:05:20 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 46219 invoked by uid 500); 14 Mar 2004 17:05:18 -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 46206 invoked by uid 500); 14 Mar 2004 17:05:18 -0000 Received: (qmail 46202 invoked from network); 14 Mar 2004 17:05:18 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 14 Mar 2004 17:05:18 -0000 Received: (qmail 10809 invoked by uid 1529); 14 Mar 2004 17:05:24 -0000 Date: 14 Mar 2004 17:05:24 -0000 Message-ID: <20040314170524.10808.qmail@minotaur.apache.org> From: scolebourne@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MultiHashMap.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N scolebourne 2004/03/14 09:05:24 Modified: collections RELEASE-NOTES.html collections/src/test/org/apache/commons/collections TestMultiHashMap.java collections/src/java/org/apache/commons/collections MultiHashMap.java Log: Add five new methods to MultiHashMap to provide broader API inspired by ideas/javadoc from Jesse Chan Revision Changes Path 1.16 +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.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- RELEASE-NOTES.html 14 Mar 2004 15:33:56 -0000 1.15 +++ RELEASE-NOTES.html 14 Mar 2004 17:05:24 -0000 1.16 @@ -33,6 +33,7 @@
  • MultiKey - Add getKey(index) and size() methods and make constructor public
  • AbstractHashedMap,AbstractLinkedMap - Add methods to access entry methods when protected scope blocks
  • Functors - Add get methods to retrieve internal state [27515]
  • +
  • MultiHashMap - Add five methods to improve the API
  • BUG FIXES

    1.18 +102 -1 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMultiHashMap.java Index: TestMultiHashMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMultiHashMap.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- TestMultiHashMap.java 18 Feb 2004 01:20:35 -0000 1.17 +++ TestMultiHashMap.java 14 Mar 2004 17:05:24 -0000 1.18 @@ -15,6 +15,8 @@ */ package org.apache.commons.collections; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Map; @@ -244,4 +246,103 @@ values = map.values(); super.verifyValues(); } + + //----------------------------------------------------------------------- + public void testGetCollection() { + MultiHashMap map = new MultiHashMap(); + map.put("A", "AA"); + assertSame(map.get("A"), map.getCollection("A")); + } + + public void testTotalSize() { + MultiHashMap map = new MultiHashMap(); + assertEquals(0, map.totalSize()); + map.put("A", "AA"); + assertEquals(1, map.totalSize()); + map.put("B", "BA"); + assertEquals(2, map.totalSize()); + map.put("B", "BB"); + assertEquals(3, map.totalSize()); + map.put("B", "BC"); + assertEquals(4, map.totalSize()); + map.remove("A"); + assertEquals(3, map.totalSize()); + map.remove("B", "BC"); + assertEquals(2, map.totalSize()); + } + + public void testSize_Key() { + MultiHashMap map = new MultiHashMap(); + assertEquals(0, map.size("A")); + assertEquals(0, map.size("B")); + map.put("A", "AA"); + assertEquals(1, map.size("A")); + assertEquals(0, map.size("B")); + map.put("B", "BA"); + assertEquals(1, map.size("A")); + assertEquals(1, map.size("B")); + map.put("B", "BB"); + assertEquals(1, map.size("A")); + assertEquals(2, map.size("B")); + map.put("B", "BC"); + assertEquals(1, map.size("A")); + assertEquals(3, map.size("B")); + map.remove("A"); + assertEquals(0, map.size("A")); + assertEquals(3, map.size("B")); + map.remove("B", "BC"); + assertEquals(0, map.size("A")); + assertEquals(2, map.size("B")); + } + + public void testIterator_Key() { + MultiHashMap map = new MultiHashMap(); + assertEquals(false, map.iterator("A").hasNext()); + map.put("A", "AA"); + Iterator it = map.iterator("A"); + assertEquals(true, it.hasNext()); + it.next(); + assertEquals(false, it.hasNext()); + } + + public void testContainsValue_Key() { + MultiHashMap map = new MultiHashMap(); + assertEquals(false, map.containsValue("A", "AA")); + assertEquals(false, map.containsValue("B", "BB")); + map.put("A", "AA"); + assertEquals(true, map.containsValue("A", "AA")); + assertEquals(false, map.containsValue("A", "AB")); + } + + public void testPutAll_KeyCollection() { + MultiHashMap map = new MultiHashMap(); + Collection coll = Arrays.asList(new Object[] {"X", "Y", "Z"}); + + assertEquals(true, map.putAll("A", coll)); + assertEquals(3, map.size("A")); + assertEquals(true, map.containsValue("A", "X")); + assertEquals(true, map.containsValue("A", "Y")); + assertEquals(true, map.containsValue("A", "Z")); + + assertEquals(false, map.putAll("A", null)); + assertEquals(3, map.size("A")); + assertEquals(true, map.containsValue("A", "X")); + assertEquals(true, map.containsValue("A", "Y")); + assertEquals(true, map.containsValue("A", "Z")); + + assertEquals(false, map.putAll("A", new ArrayList())); + assertEquals(3, map.size("A")); + assertEquals(true, map.containsValue("A", "X")); + assertEquals(true, map.containsValue("A", "Y")); + assertEquals(true, map.containsValue("A", "Z")); + + coll = Arrays.asList(new Object[] {"M"}); + assertEquals(true, map.putAll("A", coll)); + assertEquals(4, map.size("A")); + assertEquals(true, map.containsValue("A", "X")); + assertEquals(true, map.containsValue("A", "Y")); + assertEquals(true, map.containsValue("A", "Z")); + assertEquals(true, map.containsValue("A", "M")); + } + } 1.17 +101 -4 jakarta-commons/collections/src/java/org/apache/commons/collections/MultiHashMap.java Index: MultiHashMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MultiHashMap.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- MultiHashMap.java 14 Mar 2004 15:33:57 -0000 1.16 +++ MultiHashMap.java 14 Mar 2004 17:05:24 -0000 1.17 @@ -131,6 +131,64 @@ //----------------------------------------------------------------------- /** + * Gets the total size of the map by counting all the values. + * + * @return the total size of the map counting all values + * @since Commons Collections 3.1 + */ + public int totalSize() { + int total = 0; + Collection values = super.values(); + for (Iterator it = values.iterator(); it.hasNext();) { + Collection coll = (Collection) it.next(); + total += coll.size(); + } + return total; + } + + /** + * Gets the collection mapped to the specified key. + * This method is a convenience method to typecast the result of get(key). + * + * @param key the key to retrieve + * @return the collection mapped to the key, null if no mapping + * @since Commons Collections 3.1 + */ + public Collection getCollection(Object key) { + return (Collection) get(key); + } + + /** + * Gets the size of the collection mapped to the specified key. + * + * @param key the key to get size for + * @return the size of the collection at the key, zero if key not in map + * @since Commons Collections 3.1 + */ + public int size(Object key) { + Collection coll = getCollection(key); + if (coll == null) { + return 0; + } + return coll.size(); + } + + /** + * Gets an iterator for the collection mapped to the specified key. + * + * @param key the key to get an iterator for + * @return the iterator of the collection at the key, empty iterator if key not in map + * @since Commons Collections 3.1 + */ + public Iterator iterator(Object key) { + Collection coll = getCollection(key); + if (coll == null) { + return IteratorUtils.EMPTY_ITERATOR; + } + return coll.iterator(); + } + + /** * Adds the value to the collection associated with the specified key. *

    * Unlike a normal Map the previous value is not replaced. @@ -143,17 +201,41 @@ public Object put(Object key, Object value) { // NOTE:: put is called during deserialization in JDK < 1.4 !!!!!! // so we must have a readObject() - Collection coll = (Collection) super.get(key); + Collection coll = getCollection(key); if (coll == null) { coll = createCollection(null); super.put(key, coll); } boolean results = coll.add(value); - return (results ? value : null); } /** + * Adds a collection of values to the collection associated with the specified key. + * + * @param key the key to store against + * @param values the values to add to the collection at the key, null ignored + * @return true if this map changed + * @since Commons Collections 3.1 + */ + public boolean putAll(Object key, Collection values) { + if (values == null || values.size() == 0) { + return false; + } + Collection coll = getCollection(key); + if (coll == null) { + coll = createCollection(values); + if (coll.size() == 0) { + return false; + } + super.put(key, coll); + return true; + } else { + return coll.addAll(values); + } + } + + /** * Checks whether the map contains the value specified. *

    * This checks all collections against all keys for the value, and thus could be slow. @@ -179,6 +261,21 @@ } /** + * Checks whether the collection at the specified key contains the value. + * + * @param value the value to search for + * @return true if the map contains the value + * @since Commons Collections 3.1 + */ + public boolean containsValue(Object key, Object value) { + Collection coll = getCollection(key); + if (coll == null) { + return false; + } + return coll.contains(value); + } + + /** * Removes a specific value from map. *

    * The item is removed from the collection mapped to the specified key. @@ -192,7 +289,7 @@ * @return the value removed (which was passed in), null if nothing removed */ public Object remove(Object key, Object item) { - Collection valuesForKey = (Collection) super.get(key); + Collection valuesForKey = getCollection(key); if (valuesForKey == null) { return null; } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org