Add more tests, add emptyIterable() method.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1683759 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-06-05 14:06:47 +00:00
parent 8d78483013
commit ed39f93348
3 changed files with 70 additions and 4 deletions

View File

@ -91,14 +91,15 @@ public class FluentIterable<E> implements Iterable<E> {
* corresponding input iterator supports it.
*
* @param <T> the element type
* @param iterable the iterable to wrap into a FluentIterable
* @param iterable the iterable to wrap into a FluentIterable, may be null
* @return a new FluentIterable wrapping the provided iterable
*/
public static <T> FluentIterable<T> of(final Iterable<T> iterable) {
if (iterable == null) {
throw new NullPointerException("Iterable must not be null");
}
if (iterable instanceof FluentIterable<?>) {
@SuppressWarnings("unchecked")
final FluentIterable<T> empty = IterableUtils.EMPTY_ITERABLE;
return empty;
} else if (iterable instanceof FluentIterable<?>) {
return (FluentIterable<T>) iterable;
} else {
return new FluentIterable<T>(iterable);

View File

@ -40,6 +40,33 @@ import org.apache.commons.collections4.iterators.ZippingIterator;
*/
public class IterableUtils {
/**
* An empty iterable.
*/
@SuppressWarnings("rawtypes")
static final FluentIterable EMPTY_ITERABLE = new FluentIterable<Object>() {
@Override
public Iterator<Object> iterator() {
return IteratorUtils.emptyIterator();
}
};
// Empty
// ----------------------------------------------------------------------
/**
* Gets an empty iterable.
* <p>
* This iterable does not contain any elements.
*
* @param <E> the element type
* @return an empty iterable
*/
@SuppressWarnings("unchecked") // OK, empty collection is compatible with any type
public static <E> Iterable<E> emptyIterable() {
return EMPTY_ITERABLE;
}
// Chained
// ----------------------------------------------------------------------

View File

@ -102,6 +102,19 @@ public class FluentIterableTest {
};
// -----------------------------------------------------------------------
@Test
public void factoryMethodOf() {
List<Integer> result = FluentIterable.of(1, 2, 3, 4, 5).toList();
assertEquals(Arrays.asList(1, 2, 3, 4, 5), result);
result = FluentIterable.of(new Integer[0]).toList();
assertTrue(result.isEmpty());
final Iterable<Integer> it = null;
result = FluentIterable.of(it).toList();
assertTrue(result.isEmpty());
}
@Test
public void appendElements() {
FluentIterable<Integer> it = FluentIterable.of(iterableA).append(10, 20, 30);
@ -269,6 +282,24 @@ public class FluentIterableTest {
assertEquals(0, result.size());
}
@Test
public void zip() {
List<Integer> result = FluentIterable.of(iterableOdd).zip(iterableEven).toList();
List<Integer> combinedList = new ArrayList<Integer>();
CollectionUtils.addAll(combinedList, iterableOdd);
CollectionUtils.addAll(combinedList, iterableEven);
Collections.sort(combinedList);
assertEquals(combinedList, result);
result = FluentIterable.of(iterableOdd).zip((Iterable<Integer>) null).toList();
List<Integer> expected = IterableUtils.toList(iterableOdd);
assertEquals(expected, result);
result = FluentIterable.of(Arrays.asList(1, 4, 7)).zip(Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9)).toList();
combinedList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
assertEquals(combinedList, result);
}
@Test
public void allMatch() {
assertTrue(FluentIterable.of(iterableEven).allMatch(EVEN));
@ -303,6 +334,13 @@ public class FluentIterableTest {
assertFalse(FluentIterable.of(iterableOdd).isEmpty());
}
@Test
public void size() {
assertEquals(0, FluentIterable.of((Iterable<?>) null).size());
assertEquals(0, FluentIterable.of(emptyIterable).size());
assertEquals(IterableUtils.toList(iterableOdd).size(), FluentIterable.of(iterableOdd).size());
}
@Test
public void contains() {
assertTrue(FluentIterable.of(iterableEven).contains(2));