commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Yevtushenko <ser...@inferenzsysteme.informatik.tu-darmstadt.de>
Subject [collections] bug in CollectionUtils.cardinality method
Date Sat, 19 Oct 2002 07:15:42 GMT
There is a bug in cardinality method, which shows, when obj parameter is 
null;
It leads to NullPointerException.

In order to reveal it, add line

assertEquals(0, CollectionUtils.cardinality(null, _b));

to testCardinality in TestCollectionUtils.

One variant of correct implementation is following:

 public static int cardinality(Object obj, final Collection col) {
        int count = 0;
        Iterator it = col.iterator();
        if(null==obj){
            while(it.hasNext()){
                Object elt = it.next();
                if(null==elt){
                    count++;
                }
            }
        }else{
            while(it.hasNext()) {
                Object elt = it.next();
                if(obj.equals(elt)) {
                    count++;
                }
            }
        }
        return count;
    }

Kind regards.

Sergey Yevtushenko




--
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