add containsAny(Collection,Collection) and tests
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130811 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
52cc5cbef2
commit
034ec419ea
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.15 2002/08/18 20:11:37 pjack Exp $
|
||||
* $Revision: 1.15 $
|
||||
* $Date: 2002/08/18 20:11:37 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.16 2002/09/07 19:49:49 rwaldhoff Exp $
|
||||
* $Revision: 1.16 $
|
||||
* $Date: 2002/09/07 19:49:49 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -83,7 +83,7 @@ import org.apache.commons.collections.iterators.EnumerationIterator;
|
|||
* @author Rodney Waldhoff
|
||||
* @author Paul Jack
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: CollectionUtils.java,v 1.15 2002/08/18 20:11:37 pjack Exp $
|
||||
* @version $Revision: 1.16 $ $Date: 2002/09/07 19:49:49 $
|
||||
*/
|
||||
public class CollectionUtils {
|
||||
|
||||
|
@ -134,6 +134,7 @@ public class CollectionUtils {
|
|||
* in the two given {@link Collection}s.
|
||||
*
|
||||
* @see Collection#retainAll
|
||||
* @see #containsAny
|
||||
*/
|
||||
public static Collection intersection(final Collection a, final Collection b) {
|
||||
ArrayList list = new ArrayList();
|
||||
|
@ -197,6 +198,30 @@ public class CollectionUtils {
|
|||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> iff some element of <i>a</i>
|
||||
* is also an element of <i>b</i> (or, equivalently, if
|
||||
* some element of <i>b</i> is also an element of <i>a</i>).
|
||||
* In other words, this method returns <code>true</code>
|
||||
* iff the {@link #intersection} of <i>a</i> and <i>b</i>
|
||||
* is not empty.
|
||||
* @since 2.1
|
||||
* @param a a non-<code>null</code> Collection
|
||||
* @param b a non-<code>null</code> Collection
|
||||
* @return <code>true</code> iff the intersection of <i>a</i> and <i>b</i> is non-empty
|
||||
* @see #intersection
|
||||
*/
|
||||
public static boolean containsAny(final Collection a, final Collection b) {
|
||||
// TO DO: we may be able to optimize this by ensuring either a or b
|
||||
// is the larger of the two Collections, but I'm not sure which.
|
||||
for(Iterator iter = a.iterator(); iter.hasNext();) {
|
||||
if(b.contains(iter.next())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Map} mapping each unique element in
|
||||
* the given {@link Collection} to an {@link Integer}
|
||||
|
@ -689,6 +714,7 @@ public class CollectionUtils {
|
|||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static final int getFreq(final Object obj, final Map freqMap) {
|
||||
try {
|
||||
return ((Integer)(freqMap.get(obj))).intValue();
|
||||
|
@ -700,7 +726,6 @@ public class CollectionUtils {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Base class for collection decorators. I decided to do it this way
|
||||
* because it seemed to result in the most reuse.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v 1.5 2002/08/18 15:26:43 scolebourne Exp $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2002/08/18 15:26:43 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v 1.6 2002/09/07 19:49:49 rwaldhoff Exp $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2002/09/07 19:49:49 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -66,7 +66,7 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* @author Rodney Waldhoff
|
||||
* @version $Id: TestCollectionUtils.java,v 1.5 2002/08/18 15:26:43 scolebourne Exp $
|
||||
* @version $Id: TestCollectionUtils.java,v 1.6 2002/09/07 19:49:49 rwaldhoff Exp $
|
||||
*/
|
||||
public class TestCollectionUtils extends TestCase {
|
||||
public TestCollectionUtils(String testName) {
|
||||
|
@ -141,6 +141,47 @@ public class TestCollectionUtils extends TestCase {
|
|||
assertEquals(1,CollectionUtils.cardinality("e",_b));
|
||||
}
|
||||
|
||||
public void testContainsAny() {
|
||||
Collection empty = new ArrayList(0);
|
||||
Collection one = new ArrayList(1);
|
||||
one.add("1");
|
||||
Collection two = new ArrayList(1);
|
||||
two.add("2");
|
||||
Collection three = new ArrayList(1);
|
||||
three.add("3");
|
||||
Collection odds = new ArrayList(2);
|
||||
odds.add("1");
|
||||
odds.add("3");
|
||||
|
||||
assertTrue("containsAny({1},{1,3}) should return true.",
|
||||
CollectionUtils.containsAny(one,odds));
|
||||
assertTrue("containsAny({1,3},{1}) should return true.",
|
||||
CollectionUtils.containsAny(odds,one));
|
||||
assertTrue("containsAny({3},{1,3}) should return true.",
|
||||
CollectionUtils.containsAny(three,odds));
|
||||
assertTrue("containsAny({1,3},{3}) should return true.",
|
||||
CollectionUtils.containsAny(odds,three));
|
||||
assertTrue("containsAny({2},{2}) should return true.",
|
||||
CollectionUtils.containsAny(two,two));
|
||||
assertTrue("containsAny({1,3},{1,3}) should return true.",
|
||||
CollectionUtils.containsAny(odds,odds));
|
||||
|
||||
assertTrue("containsAny({2},{1,3}) should return false.",
|
||||
!CollectionUtils.containsAny(two,odds));
|
||||
assertTrue("containsAny({1,3},{2}) should return false.",
|
||||
!CollectionUtils.containsAny(odds,two));
|
||||
assertTrue("containsAny({1},{3}) should return false.",
|
||||
!CollectionUtils.containsAny(one,three));
|
||||
assertTrue("containsAny({3},{1}) should return false.",
|
||||
!CollectionUtils.containsAny(three,one));
|
||||
assertTrue("containsAny({1,3},{}) should return false.",
|
||||
!CollectionUtils.containsAny(odds,empty));
|
||||
assertTrue("containsAny({},{1,3}) should return false.",
|
||||
!CollectionUtils.containsAny(empty,odds));
|
||||
assertTrue("containsAny({},{}) should return false.",
|
||||
!CollectionUtils.containsAny(empty,empty));
|
||||
}
|
||||
|
||||
public void testUnion() {
|
||||
Collection col = CollectionUtils.union(_a,_b);
|
||||
Map freq = CollectionUtils.getCardinalityMap(col);
|
||||
|
|
Loading…
Reference in New Issue