Add emptyIfNull method, simplification.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1683900 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-06-06 12:16:11 +00:00
parent 6c8caea331
commit ed1d755bab
2 changed files with 17 additions and 11 deletions

View File

@ -449,10 +449,7 @@ public class FluentIterable<E> implements Iterable<E> {
if (collection == null) { if (collection == null) {
throw new NullPointerException("Collection must not be null"); throw new NullPointerException("Collection must not be null");
} }
CollectionUtils.addAll(collection, iterable);
for (final E element : iterable) {
collection.add(element);
}
} }
/** /**
@ -468,8 +465,8 @@ public class FluentIterable<E> implements Iterable<E> {
} }
/** /**
* Returns a list containing all elements of this iterable by * Returns a mutable list containing all elements of this iterable
* traversing its iterator. * by traversing its iterator.
* <p> * <p>
* The returned list is guaranteed to be mutable. * The returned list is guaranteed to be mutable.
* *

View File

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