commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1367706 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections/CollectionUtils.java test/java/org/apache/commons/collections/TestCollectionUtils.java
Date Tue, 31 Jul 2012 19:02:54 GMT
Author: tn
Date: Tue Jul 31 19:02:53 2012
New Revision: 1367706

URL: http://svn.apache.org/viewvc?rev=1367706&view=rev
Log:
[COLLECTION-383] Added overriden CollectionUtils#forAllDo method which takes an Iterator as
input, thanks to Adrian Cumiskey for report and patch.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestCollectionUtils.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1367706&r1=1367705&r2=1367706&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Tue Jul 31 19:02:53 2012
@@ -22,6 +22,9 @@
   <body>
 
   <release version="4.0" date="TBA" description="Next release">
+    <action issue="COLLECTIONS-383" dev="tn" type="add" due-to="Adrian Cumiskey">
+      Added "CollectionUtils#forAllDo" implementation which takes an "Iterator" as input.
+    </action>
     <action issue="COLLECTIONS-231" dev="tn" type="fix" due-to="Torsten Curdt">
       Return concrete class in static factory methods instead of base class interface
       (except for Unmodifiable decorators).

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java?rev=1367706&r1=1367705&r2=1367706&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
Tue Jul 31 19:02:53 2012
@@ -473,6 +473,27 @@ public class CollectionUtils {
     }
 
     /**
+     * Executes the given closure on each element in the collection.
+     * <p>
+     * If the input collection or closure is null, there is no change made.
+     *
+     * @param iterator
+     *            the iterator to get the input from, may be null
+     * @param closure
+     *            the closure to perform, may be null
+     * @return closure
+     * @since 4.0
+     */
+    public static <T, C extends Closure<? super T>> C forAllDo(Iterator<T>
iterator, C closure) {
+        if (iterator != null && closure != null) {
+            while (iterator.hasNext()) {
+                closure.execute(iterator.next());
+            }
+        }
+        return closure;
+    }
+
+    /**
      * Filter the collection by applying a Predicate to each element. If the
      * predicate returns false, remove the element.
      * <p>

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestCollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestCollectionUtils.java?rev=1367706&r1=1367705&r2=1367706&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestCollectionUtils.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestCollectionUtils.java
Tue Jul 31 19:02:53 2012
@@ -513,8 +513,9 @@ public class TestCollectionUtils extends
         assertNull(CollectionUtils.find(collectionA, null));
     }
 
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     @Test
-    public void forAllDo() {
+    public void forAllDoCollection() {
         Closure<List<? extends Number>> testClosure = ClosureUtils.invokerClosure("clear");
         Collection<List<? extends Number>> col = new ArrayList<List<? extends
Number>>();
         col.add(collectionA);
@@ -522,15 +523,34 @@ public class TestCollectionUtils extends
         Closure<List<? extends Number>> resultClosure = CollectionUtils.forAllDo(col,
testClosure);
         assertSame(testClosure, resultClosure);
         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
-        resultClosure = CollectionUtils.<List<? extends Number>,Closure<List<?
extends Number>>>forAllDo(col, null);
+        resultClosure = CollectionUtils.forAllDo(col, null);
         assertNull(resultClosure);
         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
-        resultClosure = CollectionUtils.forAllDo(null, testClosure);
+        resultClosure = CollectionUtils.forAllDo((Collection) null, testClosure);
         col.add(null);
         // null should be OK
         CollectionUtils.forAllDo(col, testClosure);
     }
 
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    @Test
+    public void forAllDoIterator() {
+        Closure<List<? extends Number>> testClosure = ClosureUtils.invokerClosure("clear");
+        Collection<List<? extends Number>> col = new ArrayList<List<? extends
Number>>();
+        col.add(collectionA);
+        col.add(collectionB);
+        Closure<List<? extends Number>> resultClosure = CollectionUtils.forAllDo(col.iterator(),
testClosure);
+        assertSame(testClosure, resultClosure);
+        assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
+        resultClosure = CollectionUtils.forAllDo(col.iterator(), null);
+        assertNull(resultClosure);
+        assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
+        resultClosure = CollectionUtils.forAllDo((Iterator) null, testClosure);
+        col.add(null);
+        // null should be OK
+        CollectionUtils.forAllDo(col.iterator(), testClosure);
+    }
+    
     @Test(expected = FunctorException.class)
     public void forAllDoFailure() {
         Closure<String> testClosure = ClosureUtils.invokerClosure("clear");



Mime
View raw message