commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java
Date Fri, 01 Nov 2002 19:54:27 GMT
rwaldhoff    2002/11/01 11:54:27

  Modified:    collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
  Log:
  allow cardinality(null,collection) and add test for it
  (fixing http://issues.apache.org/bugzilla/show_bug.cgi?id=13785 per suggestion of Serhiy
Yevtushenko)
  
  Revision  Changes    Path
  1.19      +17 -10    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.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- CollectionUtils.java	13 Oct 2002 00:38:36 -0000	1.18
  +++ CollectionUtils.java	1 Nov 2002 19:54:26 -0000	1.19
  @@ -1,5 +1,5 @@
   /*
  - * $Header$
  + * $Id$
    * $Revision$
    * $Date$
    *
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -75,6 +75,7 @@
   
   import org.apache.commons.collections.iterators.ArrayIterator;
   import org.apache.commons.collections.iterators.EnumerationIterator;
  +
   /**
    * A set of {@link Collection} related utility methods.
    *
  @@ -318,11 +319,17 @@
        */
       public static int cardinality(Object obj, final Collection col) {
           int count = 0;
  -        Iterator it = col.iterator();
  -        while(it.hasNext()) {
  -            Object elt = it.next();
  -            if((null == obj && null == elt) || obj.equals(elt)) {
  -                count++;
  +        if(null == obj) {
  +            for(Iterator it = col.iterator();it.hasNext();) {
  +                if(null == it.next()) {
  +                    count++;
  +                }
  +            }
  +        } else {
  +            for(Iterator it = col.iterator();it.hasNext();) {
  +                if(obj.equals(it.next())) {
  +                    count++;
  +                }
               }
           }
           return count;
  
  
  
  1.7       +64 -19    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestCollectionUtils.java	7 Sep 2002 19:49:49 -0000	1.6
  +++ TestCollectionUtils.java	1 Nov 2002 19:54:27 -0000	1.7
  @@ -1,5 +1,5 @@
   /*
  - * $Header$
  + * $Id$
    * $Revision$
    * $Date$
    *
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -66,7 +66,7 @@
   
   /**
    * @author Rodney Waldhoff
  - * @version $Id$
  + * @version $Revision$ $Date$
    */
   public class TestCollectionUtils extends TestCase {
       public TestCollectionUtils(String testName) {
  @@ -117,10 +117,10 @@
           assertEquals(new Integer(2),freq.get("b"));
           assertEquals(new Integer(3),freq.get("c"));
           assertEquals(new Integer(4),freq.get("d"));
  -        assertTrue(null == freq.get("e"));
  +        assertNull(freq.get("e"));
   
           freq = CollectionUtils.getCardinalityMap(_b);
  -        assertTrue(null == freq.get("a"));
  +        assertNull(freq.get("a"));
           assertEquals(new Integer(4),freq.get("b"));
           assertEquals(new Integer(3),freq.get("c"));
           assertEquals(new Integer(2),freq.get("d"));
  @@ -140,6 +140,51 @@
           assertEquals(2,CollectionUtils.cardinality("d",_b));
           assertEquals(1,CollectionUtils.cardinality("e",_b));
       }
  +    
  +    public void testCardinalityOfNull() {
  +        List list = new ArrayList();
  +        assertEquals(0,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertNull(freq.get(null));
  +        }
  +        list.add("A");
  +        assertEquals(0,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertNull(freq.get(null));
  +        }
  +        list.add(null);
  +        assertEquals(1,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertEquals(new Integer(1),freq.get(null));
  +        }
  +        list.add("B");
  +        assertEquals(1,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertEquals(new Integer(1),freq.get(null));
  +        }
  +        list.add(null);
  +        assertEquals(2,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertEquals(new Integer(2),freq.get(null));
  +        }
  +        list.add("B");
  +        assertEquals(2,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertEquals(new Integer(2),freq.get(null));
  +        }
  +        list.add(null);
  +        assertEquals(3,CollectionUtils.cardinality(null,list));
  +        {
  +            Map freq = CollectionUtils.getCardinalityMap(list);
  +            assertEquals(new Integer(3),freq.get(null));
  +        }
  +    }
   
       public void testContainsAny() {
           Collection empty = new ArrayList(0);
  @@ -203,19 +248,19 @@
       public void testIntersection() {
           Collection col = CollectionUtils.intersection(_a,_b);
           Map freq = CollectionUtils.getCardinalityMap(col);
  -        assertTrue(null == freq.get("a"));
  +        assertNull(freq.get("a"));
           assertEquals(new Integer(2),freq.get("b"));
           assertEquals(new Integer(3),freq.get("c"));
           assertEquals(new Integer(2),freq.get("d"));
  -        assertTrue(null == freq.get("e"));
  +        assertNull(freq.get("e"));
   
           Collection col2 = CollectionUtils.intersection(_b,_a);
           Map freq2 = CollectionUtils.getCardinalityMap(col2);
  -        assertTrue(null == freq2.get("a"));
  +        assertNull(freq2.get("a"));
           assertEquals(new Integer(2),freq2.get("b"));
           assertEquals(new Integer(3),freq2.get("c"));
           assertEquals(new Integer(2),freq2.get("d"));
  -        assertTrue(null == freq2.get("e"));
  +        assertNull(freq2.get("e"));
       }
   
       public void testDisjunction() {
  @@ -223,7 +268,7 @@
           Map freq = CollectionUtils.getCardinalityMap(col);
           assertEquals(new Integer(1),freq.get("a"));
           assertEquals(new Integer(2),freq.get("b"));
  -        assertTrue(null == freq.get("c"));
  +        assertNull(freq.get("c"));
           assertEquals(new Integer(2),freq.get("d"));
           assertEquals(new Integer(1),freq.get("e"));
   
  @@ -231,7 +276,7 @@
           Map freq2 = CollectionUtils.getCardinalityMap(col2);
           assertEquals(new Integer(1),freq2.get("a"));
           assertEquals(new Integer(2),freq2.get("b"));
  -        assertTrue(null == freq2.get("c"));
  +        assertNull(freq2.get("c"));
           assertEquals(new Integer(2),freq2.get("d"));
           assertEquals(new Integer(1),freq2.get("e"));
       }
  @@ -254,18 +299,18 @@
           Collection col = CollectionUtils.subtract(_a,_b);
           Map freq = CollectionUtils.getCardinalityMap(col);
           assertEquals(new Integer(1),freq.get("a"));
  -        assertTrue(null == freq.get("b"));
  -        assertTrue(null == freq.get("c"));
  +        assertNull(freq.get("b"));
  +        assertNull(freq.get("c"));
           assertEquals(new Integer(2),freq.get("d"));
  -        assertTrue(null == freq.get("e"));
  +        assertNull(freq.get("e"));
   
           Collection col2 = CollectionUtils.subtract(_b,_a);
           Map freq2 = CollectionUtils.getCardinalityMap(col2);
           assertEquals(new Integer(1),freq2.get("e"));
  -        assertTrue(null == freq2.get("d"));
  -        assertTrue(null == freq2.get("c"));
  +        assertNull(freq2.get("d"));
  +        assertNull(freq2.get("c"));
           assertEquals(new Integer(2),freq2.get("b"));
  -        assertTrue(null == freq2.get("a"));
  +        assertNull(freq2.get("a"));
       }
   
       public void testIsSubCollectionOfSelf() {
  
  
  

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


Mime
View raw message