From ed1d755babdeba49bf3912d2e3788d777293bea2 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sat, 6 Jun 2015 12:16:11 +0000 Subject: [PATCH] Add emptyIfNull method, simplification. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1683900 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections4/FluentIterable.java | 9 +++------ .../commons/collections4/IterableUtils.java | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/FluentIterable.java b/src/main/java/org/apache/commons/collections4/FluentIterable.java index 2ae931d9a..84fa47bf4 100644 --- a/src/main/java/org/apache/commons/collections4/FluentIterable.java +++ b/src/main/java/org/apache/commons/collections4/FluentIterable.java @@ -449,10 +449,7 @@ public class FluentIterable implements Iterable { if (collection == null) { throw new NullPointerException("Collection must not be null"); } - - for (final E element : iterable) { - collection.add(element); - } + CollectionUtils.addAll(collection, iterable); } /** @@ -468,8 +465,8 @@ public class FluentIterable implements Iterable { } /** - * Returns a list containing all elements of this iterable by - * traversing its iterator. + * Returns a mutable list containing all elements of this iterable + * by traversing its iterator. *

* The returned list is guaranteed to be mutable. * diff --git a/src/main/java/org/apache/commons/collections4/IterableUtils.java b/src/main/java/org/apache/commons/collections4/IterableUtils.java index 91ad37260..9088bd4cb 100644 --- a/src/main/java/org/apache/commons/collections4/IterableUtils.java +++ b/src/main/java/org/apache/commons/collections4/IterableUtils.java @@ -27,7 +27,6 @@ import org.apache.commons.collections4.functors.EqualPredicate; import org.apache.commons.collections4.iterators.LazyIteratorChain; import org.apache.commons.collections4.iterators.ReverseListIterator; import org.apache.commons.collections4.iterators.UniqueFilterIterator; -import org.apache.commons.collections4.iterators.ZippingIterator; /** * Provides utility methods and decorators for {@link Iterable} instances. @@ -452,9 +451,7 @@ public class IterableUtils { if (iterable instanceof UnmodifiableIterable) { return iterable; } - @SuppressWarnings("unchecked") // safe - final Iterable it = iterable != null ? iterable : EMPTY_ITERABLE; - return new UnmodifiableIterable(it); + return new UnmodifiableIterable(emptyIfNull(iterable)); } /** @@ -521,7 +518,7 @@ public class IterableUtils { for (int i = 0; i < iterables.length; i++) { iterators[i] = emptyIteratorIfNull(iterables[i]); } - return new ZippingIterator(iterators); + return IteratorUtils.zippingIterator(iterators); } }; } @@ -529,6 +526,18 @@ public class IterableUtils { // Utility methods // ---------------------------------------------------------------------- + /** + * Returns an immutable empty iterable if the argument is null, + * or the argument itself otherwise. + * + * @param the element type + * @param iterable the iterable, may be null + * @return an empty iterable if the argument is null + */ + public static Iterable emptyIfNull(final Iterable iterable) { + return iterable == null ? IterableUtils.emptyIterable() : iterable; + } + /** * Returns an empty iterator if the argument is null, * or returns {@code iterable.iterator()} otherwise.