[COLLECTIONS-429] Add CollectionUtils.containsAll method with guaranteed runtime complexity regardless of used Collection.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1440418 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-01-30 13:55:02 +00:00
parent 5a31eea7b4
commit c2b58d4ac7
2 changed files with 59 additions and 1 deletions

View File

@ -282,6 +282,35 @@ public class CollectionUtils {
return list;
}
/**
* Returns <code>true</code> iff all elements of {@code coll2} are also contained
* in {@code coll1}.
* <p>
* In other words, this method returns <code>true</code> iff the
* {@link #intersection} of <i>coll1</i> and <i>coll2</i> has the same cardinality as
* {@code coll2}. In case {@code coll2} is empty, {@code true} will be returned.
* <p>
* This method is intended as a replacement for {@link Collection#containsAll(Collection)}
* with a guaranteed runtime complexity of {@code O(n)}. Depending on the type of
* {@link Collection} provided, this method will be much faster than calling
* {@link Collection#containsAll(Collection)} instead.
*
* @param coll1 the first collection, must not be null
* @param coll2 the second collection, must not be null
* @return <code>true</code> iff the intersection of the collections has the same cardinality
* of the second collection
* @since 4.0
* @see #intersection
*/
public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) {
if (coll2.isEmpty()) {
return true;
} else {
Collection<Object> result = CollectionUtils.<Object>intersection(coll1, coll2);
return result.size() == coll2.size();
}
}
/**
* Returns <code>true</code> iff at least one element is in both collections.
* <p>
@ -289,7 +318,7 @@ public class CollectionUtils {
* {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
*
* @param coll1 the first collection, must not be null
* @param coll2 the first collection, must not be null
* @param coll2 the second collection, must not be null
* @return <code>true</code> iff the intersection of the collections is non-empty
* @since 2.1
* @see #intersection

View File

@ -240,6 +240,35 @@ public class CollectionUtilsTest extends MockTestCase {
}
}
@Test
public void containsAll() {
final Collection<String> empty = new ArrayList<String>(0);
final Collection<String> one = new ArrayList<String>(1);
one.add("1");
final Collection<String> two = new ArrayList<String>(1);
two.add("2");
final Collection<String> three = new ArrayList<String>(1);
three.add("3");
final Collection<String> odds = new ArrayList<String>(2);
odds.add("1");
odds.add("3");
assertTrue("containsAll({1},{1,3}) should return false.", !CollectionUtils.containsAll(one, odds));
assertTrue("containsAll({1,3},{1}) should return true.", CollectionUtils.containsAll(odds, one));
assertTrue("containsAll({3},{1,3}) should return false.", !CollectionUtils.containsAll(three, odds));
assertTrue("containsAll({1,3},{3}) should return true.", CollectionUtils.containsAll(odds, three));
assertTrue("containsAll({2},{2}) should return true.", CollectionUtils.containsAll(two, two));
assertTrue("containsAll({1,3},{1,3}) should return true.", CollectionUtils.containsAll(odds, odds));
assertTrue("containsAll({2},{1,3}) should return false.", !CollectionUtils.containsAll(two, odds));
assertTrue("containsAll({1,3},{2}) should return false.", !CollectionUtils.containsAll(odds, two));
assertTrue("containsAll({1},{3}) should return false.", !CollectionUtils.containsAll(one, three));
assertTrue("containsAll({3},{1}) should return false.", !CollectionUtils.containsAll(three, one));
assertTrue("containsAll({1,3},{}) should return true.", CollectionUtils.containsAll(odds, empty));
assertTrue("containsAll({},{1,3}) should return false.", !CollectionUtils.containsAll(empty, odds));
assertTrue("containsAll({},{}) should return true.", CollectionUtils.containsAll(empty, empty));
}
@Test
public void containsAny() {
final Collection<String> empty = new ArrayList<String>(0);