From 2c106dd4ead8780fe11b0f11cffb030c6b1a0b38 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Fri, 5 Sep 2003 02:16:33 +0000 Subject: [PATCH] Fixed a javadoc error, improved efficiency and added tests for CollectionUtils.isProperSubCollection git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131133 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/CollectionUtils.java | 16 +++++++--- .../collections/TestCollectionUtils.java | 32 ++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 5f4600509..966776871 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.35 2003/08/31 17:26:44 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.36 2003/09/05 02:16:33 psteitz Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -81,7 +81,7 @@ import org.apache.commons.collections.iterators.EnumerationIterator; * A set of {@link Collection} related utility methods. * * @since Commons Collections 1.0 - * @version $Revision: 1.35 $ $Date: 2003/08/31 17:26:44 $ + * @version $Revision: 1.36 $ $Date: 2003/09/05 02:16:33 $ * * @author Rodney Waldhoff * @author Paul Jack @@ -282,13 +282,19 @@ public class CollectionUtils { * for each element e in a, and there is at least one * element f such that the cardinality of f in b * is strictly greater than the cardinality of f in a. + *

+ * The implementation assumes + *

* * @see #isSubCollection * @see Collection#containsAll */ public static boolean isProperSubCollection(final Collection a, final Collection b) { - // XXX optimize me! - return CollectionUtils.isSubCollection(a,b) && (!(CollectionUtils.isEqualCollection(a,b))); + return (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b); } /** @@ -675,7 +681,7 @@ public class CollectionUtils { * * * @param obj the object to get an index of - * @param index the index to get + * @param idx the index to get * @throws IndexOutOfBoundsException * @throws NoSuchElementException */ diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 64effe130..9c7751220 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -1,7 +1,7 @@ /* - * $Id: TestCollectionUtils.java,v 1.17 2003/08/31 17:28:43 scolebourne Exp $ - * $Revision: 1.17 $ - * $Date: 2003/08/31 17:28:43 $ + * $Id: TestCollectionUtils.java,v 1.18 2003/09/05 02:16:33 psteitz Exp $ + * $Revision: 1.18 $ + * $Date: 2003/09/05 02:16:33 $ * * ==================================================================== * @@ -81,7 +81,7 @@ import junit.framework.TestSuite; * @author Matthew Hawthorne * @author Stephen Colebourne * - * @version $Revision: 1.17 $ $Date: 2003/08/31 17:28:43 $ + * @version $Revision: 1.18 $ $Date: 2003/09/05 02:16:33 $ */ public class TestCollectionUtils extends TestCase { public TestCollectionUtils(String testName) { @@ -411,6 +411,30 @@ public class TestCollectionUtils extends TestCase { assertTrue(CollectionUtils.isEqualCollection(a,b)); assertTrue(CollectionUtils.isEqualCollection(b,a)); } + + public void testIsProperSubCollection() { + Collection a = new ArrayList(); + Collection b = new ArrayList(); + assertTrue(!CollectionUtils.isProperSubCollection(a,b)); + b.add("1"); + assertTrue(CollectionUtils.isProperSubCollection(a,b)); + assertTrue(!CollectionUtils.isProperSubCollection(b,a)); + assertTrue(!CollectionUtils.isProperSubCollection(b,b)); + assertTrue(!CollectionUtils.isProperSubCollection(a,a)); + a.add("1"); + a.add("2"); + b.add("2"); + assertTrue(!CollectionUtils.isProperSubCollection(b,a)); + assertTrue(!CollectionUtils.isProperSubCollection(a,b)); + a.add("1"); + assertTrue(CollectionUtils.isProperSubCollection(b,a)); + assertTrue(CollectionUtils.isProperSubCollection( + CollectionUtils.intersection(_a, _b), _a)); + assertTrue(CollectionUtils.isProperSubCollection( + CollectionUtils.subtract(a, b), a)); + assertTrue(!CollectionUtils.isProperSubCollection( + a, CollectionUtils.subtract(a, b))); + } public void testIndex() {