[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
This commit is contained in:
Thomas Neidhart 2012-07-31 19:02:53 +00:00
parent cde8c81a4e
commit 61fe60ef9d
3 changed files with 47 additions and 3 deletions

View File

@ -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).

View File

@ -472,6 +472,27 @@ public class CollectionUtils {
return closure;
}
/**
* 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.

View File

@ -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<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 MockTestCase {
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");