scolebourne    2003/01/25 03:40:26

  Modified:    collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
               collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
  Log:
  Add exists() to test if any one element in the collection matches the predicate
  from Peter KoBek
  
  Revision  Changes    Path
  1.12      +19 -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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TestCollectionUtils.java  25 Jan 2003 11:31:12 -0000      1.11
  +++ TestCollectionUtils.java  25 Jan 2003 11:40:26 -0000      1.12
  @@ -459,6 +459,21 @@
           assertEquals(0, CollectionUtils.countMatches(null, null));
       }
   
  +    public void testExists() {
  +        List list = new ArrayList();
  +        assertEquals(false, CollectionUtils.exists(null, null));
  +        assertEquals(false, CollectionUtils.exists(list, null));
  +        assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO));
  +        assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
  +        list.add("One");
  +        list.add("Three");
  +        list.add("Four");
  +        assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
  +
  +        list.add("Two");
  +        assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO));
  +    }
  +    
       public void testSelect() {
           List list = new ArrayList();
           list.add("One");
  
  
  
  1.28      +22 -2     
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.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- CollectionUtils.java      25 Jan 2003 11:29:37 -0000      1.27
  +++ CollectionUtils.java      25 Jan 2003 11:40:26 -0000      1.28
  @@ -450,6 +450,26 @@
       }
   
       /** 
  +     * Answers true if a predicate is true for at least one element of a collection.
  +     * <p>
  +     * A <code>null</code> collection or predicate returns false.
  +     * 
  +     * @param collection the collection to get the input from, may be null
  +     * @param predicate the predicate to use, may be null
  +     * @return true if at least one element of the collection matches the predicate
  +     */
  +    public static boolean exists(Collection collection, Predicate predicate) {
  +        if (collection != null && predicate != null) {
  +            for (Iterator it = collection.iterator(); it.hasNext();) {
  +                if (predicate.evaluate(it.next())) {
  +                    return true;
  +                }
  +            }
  +        }
  +        return false;
  +    }
  +
  +    /** 
        * Selects all elements from input collection which match the given predicate
        * into an output collection.
        * <p>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to