commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java
Date Sat, 07 Sep 2002 19:49:49 GMT
rwaldhoff    2002/09/07 12:49:49

  Modified:    collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
  Log:
  add containsAny(Collection,Collection) and tests
  
  Revision  Changes    Path
  1.16      +30 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java
  
  Index: CollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- CollectionUtils.java	18 Aug 2002 20:11:37 -0000	1.15
  +++ CollectionUtils.java	7 Sep 2002 19:49:49 -0000	1.16
  @@ -83,7 +83,7 @@
    * @author Rodney Waldhoff
    * @author Paul Jack
    * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
  - * @version $Id$
  + * @version $Revision$ $Date$
    */
   public class CollectionUtils {
   
  @@ -134,6 +134,7 @@
        * 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();
  @@ -198,6 +199,30 @@
       }
   
       /**
  +     * 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}
        * representing the number of occurances of that element
  @@ -689,6 +714,7 @@
               i++;
           }
       }
  +
       private static final int getFreq(final Object obj, final Map freqMap) {
           try {
               return ((Integer)(freqMap.get(obj))).intValue();
  @@ -699,7 +725,6 @@
           }
           return 0;
       }
  -
   
       /**
        *  Base class for collection decorators.  I decided to do it this way
  
  
  
  1.6       +45 -4     jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java
  
  Index: TestCollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestCollectionUtils.java	18 Aug 2002 15:26:43 -0000	1.5
  +++ TestCollectionUtils.java	7 Sep 2002 19:49:49 -0000	1.6
  @@ -141,6 +141,47 @@
           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);
  
  
  

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