commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections CollectionUtils.java
Date Tue, 27 Apr 2004 20:00:18 GMT
scolebourne    2004/04/27 13:00:18

  Modified:    collections RELEASE-NOTES.html project.xml
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
               collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
  Log:
  Add special cases for Set and Bag to CollectionUtils cardinality()
  bug 28629, from Jon Schewe
  
  Revision  Changes    Path
  1.41      +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.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- RELEASE-NOTES.html	25 Apr 2004 23:33:11 -0000	1.40
  +++ RELEASE-NOTES.html	27 Apr 2004 20:00:17 -0000	1.41
  @@ -43,6 +43,7 @@
   <li>Functors - Add get methods to retrieve internal state [27515]</li>
   <li>Functors - Add additional getInstance() methods for consistency [27856,27857]</li>
   <li>CollectionUtils - get(Object,int) method now supports primitive arrays</li>
  +<li>CollectionUtils - cardinality() now works faster for Set and Bag instances [28629]</li>
   <li>CollectionUtils - Add size(Object) method to find the size of various collection-like
objects [27909]</li>
   <li>SingletonIterator - make remove() functionality optional</li>
   <li>AbstractLinkedList/NodeCachingLinkedList - added getValue() and setValue() to
Node, and made everything use them</li>
  
  
  
  1.39      +3 -0      jakarta-commons/collections/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/project.xml,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- project.xml	20 Apr 2004 23:35:07 -0000	1.38
  +++ project.xml	27 Apr 2004 20:00:17 -0000	1.39
  @@ -255,6 +255,9 @@
         <name>Joe Raysa</name>
       </contributor>
       <contributor>
  +      <name>Jon Schewe</name>
  +    </contributor>
  +    <contributor>
         <name>Michael Smith</name>
       </contributor>
       <contributor>
  
  
  
  1.38      +34 -12    jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java
  
  Index: TestCollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- TestCollectionUtils.java	1 Apr 2004 22:43:12 -0000	1.37
  +++ TestCollectionUtils.java	27 Apr 2004 20:00:18 -0000	1.38
  @@ -113,17 +113,39 @@
       }
   
       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() {
  
  
  
  1.61      +17 -11    jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java
  
  Index: CollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
  retrieving revision 1.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- CollectionUtils.java	1 Apr 2004 22:43:13 -0000	1.60
  +++ CollectionUtils.java	27 Apr 2004 20:00:18 -0000	1.61
  @@ -50,6 +50,7 @@
    * @author Janek Bogucki
    * @author Phil Steitz
    * @author Steven Melzer
  + * @author Jon Schewe
    */
   public class CollectionUtils {
   
  @@ -319,24 +320,29 @@
       }
   
       /**
  -     * Returns the number of occurrences of <i>obj</i>
  -     * in <i>col</i>.
  +     * Returns the number of occurrences of <i>obj</i> in <i>coll</i>.
        *
        * @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++;
                   }
               }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message