Add apply method to FluentIterable.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1682126 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-05-27 21:34:35 +00:00
parent e57282bd9c
commit 9cb3de6713
3 changed files with 44 additions and 0 deletions

View File

@ -344,6 +344,16 @@ public class FluentIterable<E> implements Iterable<E> {
return IteratorUtils.asEnumeration(iterator());
}
/**
* Applies the closure to all elements contained in this iterable.
*
* @param closure the closure to apply to each element, may not be null
* @throws NullPointerException if closure is null
*/
public void apply(final Closure<? super E> closure) {
IterableUtils.apply(iterable, closure);
}
/**
* Checks if all elements contained in this iterable are matching the
* provided predicate.

View File

@ -445,6 +445,18 @@ public class IterableUtils {
return iterable != null ? iterable.iterator() : IteratorUtils.<E>emptyIterator();
}
/**
* Applies the closure to each element of the provided iterable.
*
* @param <E> the element type
* @param iterable the iterator to use, may be null
* @param closure the closure to apply to each element, may not be null
* @throws NullPointerException if closure is null
*/
public static <E> void apply(final Iterable<E> iterable, final Closure<? super E> closure) {
IteratorUtils.apply(emptyIteratorIfNull(iterable), closure);
}
/**
* Answers true if a predicate is true for every element of an iterable.
* <p>

View File

@ -1175,6 +1175,28 @@ public class IteratorUtils {
// Utility methods
//-----------------------------------------------------------------------
/**
* Applies the closure to each element of the provided iterator.
*
* @param <E> the element type
* @param iterator the iterator to use, may be null
* @param closure the closure to apply to each element, may not be null
* @throws NullPointerException if closure is null
* @since 4.1
*/
public static <E> void apply(final Iterator<E> iterator, final Closure<? super E> closure) {
if (closure == null) {
throw new NullPointerException("Closure must not be null");
}
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
closure.execute(element);
}
}
}
/**
* Answers true if a predicate is true for any element of the iterator.
* <p>