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
This commit is contained in:
parent
730b224f37
commit
a249eba720
|
@ -43,6 +43,7 @@ No interface changes, or deprecations have occurred.
|
||||||
<li>Functors - Add get methods to retrieve internal state [27515]</li>
|
<li>Functors - Add get methods to retrieve internal state [27515]</li>
|
||||||
<li>Functors - Add additional getInstance() methods for consistency [27856,27857]</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 - 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>CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]</li>
|
||||||
<li>SingletonIterator - make remove() functionality optional</li>
|
<li>SingletonIterator - make remove() functionality optional</li>
|
||||||
<li>AbstractLinkedList/NodeCachingLinkedList - added getValue() and setValue() to Node, and made everything use them</li>
|
<li>AbstractLinkedList/NodeCachingLinkedList - added getValue() and setValue() to Node, and made everything use them</li>
|
||||||
|
|
|
@ -254,6 +254,9 @@
|
||||||
<contributor>
|
<contributor>
|
||||||
<name>Joe Raysa</name>
|
<name>Joe Raysa</name>
|
||||||
</contributor>
|
</contributor>
|
||||||
|
<contributor>
|
||||||
|
<name>Jon Schewe</name>
|
||||||
|
</contributor>
|
||||||
<contributor>
|
<contributor>
|
||||||
<name>Michael Smith</name>
|
<name>Michael Smith</name>
|
||||||
</contributor>
|
</contributor>
|
||||||
|
|
|
@ -38,7 +38,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||||
* Provides utility methods and decorators for {@link Collection} instances.
|
* Provides utility methods and decorators for {@link Collection} instances.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 1.0
|
* @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 Rodney Waldhoff
|
||||||
* @author Paul Jack
|
* @author Paul Jack
|
||||||
|
@ -50,6 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||||
* @author Janek Bogucki
|
* @author Janek Bogucki
|
||||||
* @author Phil Steitz
|
* @author Phil Steitz
|
||||||
* @author Steven Melzer
|
* @author Steven Melzer
|
||||||
|
* @author Jon Schewe
|
||||||
*/
|
*/
|
||||||
public class CollectionUtils {
|
public class CollectionUtils {
|
||||||
|
|
||||||
|
@ -319,24 +320,29 @@ public class CollectionUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of occurrences of <i>obj</i>
|
* Returns the number of occurrences of <i>obj</i> in <i>coll</i>.
|
||||||
* in <i>col</i>.
|
|
||||||
*
|
*
|
||||||
* @param obj the object to find the cardinality of
|
* @param obj the object to find the cardinality of
|
||||||
* @param col the collection to search
|
* @param coll the collection to search
|
||||||
* @return the the number of occurrences of obj in col
|
* @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;
|
int count = 0;
|
||||||
if(null == obj) {
|
if (obj == null) {
|
||||||
for(Iterator it = col.iterator();it.hasNext();) {
|
for (Iterator it = coll.iterator();it.hasNext();) {
|
||||||
if(null == it.next()) {
|
if (it.next() == null) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(Iterator it = col.iterator();it.hasNext();) {
|
for (Iterator it = coll.iterator();it.hasNext();) {
|
||||||
if(obj.equals(it.next())) {
|
if (obj.equals(it.next())) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||||
* @author Phil Steitz
|
* @author Phil Steitz
|
||||||
* @author Steven Melzer
|
* @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 {
|
public class TestCollectionUtils extends TestCase {
|
||||||
|
|
||||||
|
@ -113,17 +113,39 @@ public class TestCollectionUtils extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCardinality() {
|
public void testCardinality() {
|
||||||
assertEquals(1,CollectionUtils.cardinality("a",collectionA));
|
assertEquals(1, CollectionUtils.cardinality("a", collectionA));
|
||||||
assertEquals(2,CollectionUtils.cardinality("b",collectionA));
|
assertEquals(2, CollectionUtils.cardinality("b", collectionA));
|
||||||
assertEquals(3,CollectionUtils.cardinality("c",collectionA));
|
assertEquals(3, CollectionUtils.cardinality("c", collectionA));
|
||||||
assertEquals(4,CollectionUtils.cardinality("d",collectionA));
|
assertEquals(4, CollectionUtils.cardinality("d", collectionA));
|
||||||
assertEquals(0,CollectionUtils.cardinality("e",collectionA));
|
assertEquals(0, CollectionUtils.cardinality("e", collectionA));
|
||||||
|
|
||||||
assertEquals(0,CollectionUtils.cardinality("a",collectionB));
|
assertEquals(0, CollectionUtils.cardinality("a", collectionB));
|
||||||
assertEquals(4,CollectionUtils.cardinality("b",collectionB));
|
assertEquals(4, CollectionUtils.cardinality("b", collectionB));
|
||||||
assertEquals(3,CollectionUtils.cardinality("c",collectionB));
|
assertEquals(3, CollectionUtils.cardinality("c", collectionB));
|
||||||
assertEquals(2,CollectionUtils.cardinality("d",collectionB));
|
assertEquals(2, CollectionUtils.cardinality("d", collectionB));
|
||||||
assertEquals(1,CollectionUtils.cardinality("e",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() {
|
public void testCardinalityOfNull() {
|
||||||
|
|
Loading…
Reference in New Issue