You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/03/16 02:53:01 UTC

svn commit: r637505 - in /commons/proper/collections/trunk/src: java/org/apache/commons/collections/ListUtils.java test/org/apache/commons/collections/TestListUtils.java

Author: bayard
Date: Sat Mar 15 18:52:59 2008
New Revision: 637505

URL: http://svn.apache.org/viewvc?rev=637505&view=rev
Log:
Nathan Egge requested a ListUtils.indexOf(List, Predicate) method in COLLECTIONS-235. Applying Dave Meikle's patch. 

Modified:
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java?rev=637505&r1=637504&r2=637505&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java Sat Mar 15 18:52:59 2008
@@ -42,6 +42,7 @@
  * @author Stephen Colebourne
  * @author Neil O'Toole
  * @author Matthew Hawthorne
+ * @author Dave Meikle
  */
 public class ListUtils {
 
@@ -410,4 +411,26 @@
         return FixedSizeList.decorate(list);
     }
 
+    /**
+     * Finds the first index in the given List which matches the given predicate.
+     * <p>
+     * If the input List or predicate is null, or no element of the List
+     * matches the predicate, -1 is returned.
+     *
+     * @param list the List to search, may be null
+     * @param predicate  the predicate to use, may be null
+     * @return the first index of an Object in the List which matches the predicate or -1 if none could be found
+     */
+    public static int indexOf(List list, Predicate predicate) {
+        if (list != null && predicate != null) {
+            for (int i = 0; i < list.size(); i++) {
+                Object item = list.get(i);
+                if (predicate.evaluate(item)) {
+                    return i;
+                }
+            }
+        }
+        return -1;
+    }
+    
 }

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java?rev=637505&r1=637504&r2=637505&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java Sat Mar 15 18:52:59 2008
@@ -33,6 +33,7 @@
  * @author Stephen Colebourne
  * @author Neil O'Toole
  * @author Matthew Hawthorne
+ * @author Dave Meikle
  */
 public class TestListUtils extends BulkTest {
 
@@ -171,6 +172,22 @@
             ListUtils.removeAll(null, null);
             fail("expecting NullPointerException");
         } catch(NullPointerException npe) {} // this is what we want
+    }
+
+    /**
+     * Tests the <code>indexOf</code> method in <code>ListUtils</code> class..
+     */
+    public void testIndexOf() {
+        Predicate testPredicate = PredicateUtils.equalPredicate("d");
+        int index = ListUtils.indexOf(fullList, testPredicate);
+        assertEquals(d, fullList.get(index));
+
+        testPredicate = PredicateUtils.equalPredicate("de");
+        index = ListUtils.indexOf(fullList, testPredicate);
+        assertTrue(index == -1);
+        
+        assertEquals(ListUtils.indexOf(null,testPredicate), -1);
+        assertEquals(ListUtils.indexOf(fullList, null), -1);
     }
     
 }