From a249eba720ca2b9430aa23d7091724659dcbbc0b Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Tue, 27 Apr 2004 20:00:18 +0000 Subject: [PATCH] Add special cases for Set and Bag to CollectionUtils cardinality() bug 28629, from Jon Schewe git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131678 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.html | 1 + project.xml | 3 ++ .../commons/collections/CollectionUtils.java | 28 +++++++----- .../collections/TestCollectionUtils.java | 44 ++++++++++++++----- 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index 3e1fa3516..606d1437f 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -43,6 +43,7 @@ No interface changes, or deprecations have occurred.
  • Functors - Add get methods to retrieve internal state [27515]
  • Functors - Add additional getInstance() methods for consistency [27856,27857]
  • CollectionUtils - get(Object,int) method now supports primitive arrays
  • +
  • CollectionUtils - cardinality() now works faster for Set and Bag instances [28629]
  • CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]
  • SingletonIterator - make remove() functionality optional
  • AbstractLinkedList/NodeCachingLinkedList - added getValue() and setValue() to Node, and made everything use them
  • diff --git a/project.xml b/project.xml index 4d8f8e801..fa127f455 100644 --- a/project.xml +++ b/project.xml @@ -254,6 +254,9 @@ Joe Raysa + + Jon Schewe + Michael Smith diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index dcc0c96ac..94bee14b6 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -38,7 +38,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * Provides utility methods and decorators for {@link Collection} instances. * * @since Commons Collections 1.0 - * @version $Revision: 1.60 $ $Date: 2004/04/01 22:43:13 $ + * @version $Revision: 1.61 $ $Date: 2004/04/27 20:00:18 $ * * @author Rodney Waldhoff * @author Paul Jack @@ -50,6 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * @author Janek Bogucki * @author Phil Steitz * @author Steven Melzer + * @author Jon Schewe */ public class CollectionUtils { @@ -319,24 +320,29 @@ public class CollectionUtils { } /** - * Returns the number of occurrences of obj - * in col. + * Returns the number of occurrences of obj in coll. * * @param obj the object to find the cardinality of - * @param col the collection to search - * @return the the number of occurrences of obj in col + * @param coll the collection to search + * @return the the number of occurrences of obj in coll */ - public static int cardinality(Object obj, final Collection col) { + public static int cardinality(Object obj, final Collection coll) { + if (coll instanceof Set) { + return (coll.contains(obj) ? 1 : 0); + } + if (coll instanceof Bag) { + return ((Bag) coll).getCount(obj); + } int count = 0; - if(null == obj) { - for(Iterator it = col.iterator();it.hasNext();) { - if(null == it.next()) { + if (obj == null) { + for (Iterator it = coll.iterator();it.hasNext();) { + if (it.next() == null) { count++; } } } else { - for(Iterator it = col.iterator();it.hasNext();) { - if(obj.equals(it.next())) { + for (Iterator it = coll.iterator();it.hasNext();) { + if (obj.equals(it.next())) { count++; } } diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index a5f39aeb6..29ef6e394 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -50,7 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * @author Phil Steitz * @author Steven Melzer * - * @version $Revision: 1.37 $ $Date: 2004/04/01 22:43:12 $ + * @version $Revision: 1.38 $ $Date: 2004/04/27 20:00:18 $ */ public class TestCollectionUtils extends TestCase { @@ -113,17 +113,39 @@ public class TestCollectionUtils extends TestCase { } public void testCardinality() { - assertEquals(1,CollectionUtils.cardinality("a",collectionA)); - assertEquals(2,CollectionUtils.cardinality("b",collectionA)); - assertEquals(3,CollectionUtils.cardinality("c",collectionA)); - assertEquals(4,CollectionUtils.cardinality("d",collectionA)); - assertEquals(0,CollectionUtils.cardinality("e",collectionA)); + assertEquals(1, CollectionUtils.cardinality("a", collectionA)); + assertEquals(2, CollectionUtils.cardinality("b", collectionA)); + assertEquals(3, CollectionUtils.cardinality("c", collectionA)); + assertEquals(4, CollectionUtils.cardinality("d", collectionA)); + assertEquals(0, CollectionUtils.cardinality("e", collectionA)); - assertEquals(0,CollectionUtils.cardinality("a",collectionB)); - assertEquals(4,CollectionUtils.cardinality("b",collectionB)); - assertEquals(3,CollectionUtils.cardinality("c",collectionB)); - assertEquals(2,CollectionUtils.cardinality("d",collectionB)); - assertEquals(1,CollectionUtils.cardinality("e",collectionB)); + assertEquals(0, CollectionUtils.cardinality("a", collectionB)); + assertEquals(4, CollectionUtils.cardinality("b", collectionB)); + assertEquals(3, CollectionUtils.cardinality("c", collectionB)); + assertEquals(2, CollectionUtils.cardinality("d", collectionB)); + assertEquals(1, CollectionUtils.cardinality("e", collectionB)); + + Set set = new HashSet(); + set.add("A"); + set.add("C"); + set.add("E"); + set.add("E"); + assertEquals(1, CollectionUtils.cardinality("A", set)); + assertEquals(0, CollectionUtils.cardinality("B", set)); + assertEquals(1, CollectionUtils.cardinality("C", set)); + assertEquals(0, CollectionUtils.cardinality("D", set)); + assertEquals(1, CollectionUtils.cardinality("E", set)); + + Bag bag = new HashBag(); + bag.add("A", 3); + bag.add("C"); + bag.add("E"); + bag.add("E"); + assertEquals(3, CollectionUtils.cardinality("A", bag)); + assertEquals(0, CollectionUtils.cardinality("B", bag)); + assertEquals(1, CollectionUtils.cardinality("C", bag)); + assertEquals(0, CollectionUtils.cardinality("D", bag)); + assertEquals(2, CollectionUtils.cardinality("E", bag)); } public void testCardinalityOfNull() {