From 61fe60ef9db30f810b7afc743a6a2a3cf45a7dcf Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 31 Jul 2012 19:02:53 +0000 Subject: [PATCH] [COLLECTION-383] Added overriden CollectionUtils#forAllDo method which takes an Iterator as input, thanks to Adrian Cumiskey for report and patch. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1367706 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 +++ .../commons/collections/CollectionUtils.java | 21 +++++++++++++++ .../collections/TestCollectionUtils.java | 26 ++++++++++++++++--- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 03fb1456e..18a014b94 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,9 @@ + + Added "CollectionUtils#forAllDo" implementation which takes an "Iterator" as input. + Return concrete class in static factory methods instead of base class interface (except for Unmodifiable decorators). diff --git a/src/main/java/org/apache/commons/collections/CollectionUtils.java b/src/main/java/org/apache/commons/collections/CollectionUtils.java index c56c78192..986a8c5ce 100644 --- a/src/main/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections/CollectionUtils.java @@ -472,6 +472,27 @@ public class CollectionUtils { return closure; } + /** + * Executes the given closure on each element in the collection. + *

+ * 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 > C forAllDo(Iterator 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. diff --git a/src/test/java/org/apache/commons/collections/TestCollectionUtils.java b/src/test/java/org/apache/commons/collections/TestCollectionUtils.java index 4bb525532..886819ca3 100644 --- a/src/test/java/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/java/org/apache/commons/collections/TestCollectionUtils.java @@ -513,8 +513,9 @@ public class TestCollectionUtils extends MockTestCase { assertNull(CollectionUtils.find(collectionA, null)); } + @SuppressWarnings({ "unchecked", "rawtypes" }) @Test - public void forAllDo() { + public void forAllDoCollection() { Closure> testClosure = ClosureUtils.invokerClosure("clear"); Collection> col = new ArrayList>(); col.add(collectionA); @@ -522,15 +523,34 @@ public class TestCollectionUtils extends MockTestCase { Closure> resultClosure = CollectionUtils.forAllDo(col, testClosure); assertSame(testClosure, resultClosure); assertTrue(collectionA.isEmpty() && collectionB.isEmpty()); - resultClosure = CollectionUtils.,Closure>>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> testClosure = ClosureUtils.invokerClosure("clear"); + Collection> col = new ArrayList>(); + col.add(collectionA); + col.add(collectionB); + Closure> 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 testClosure = ClosureUtils.invokerClosure("clear");