[COLLECTIONS-737]: Return 0 immeditaly if the given iterable is null in IterableUtils#size. Update tests.

This commit is contained in:
Pengyu Nie 2019-11-26 15:45:33 -06:00 committed by Bruno P. Kinoshita
parent e20e373fe0
commit 3d8d96c7d2
3 changed files with 8 additions and 6 deletions

View File

@ -797,6 +797,9 @@ public class IterableUtils {
* @return the number of elements contained in the iterable
*/
public static int size(final Iterable<?> iterable) {
if (iterable == null) {
return 0;
}
if (iterable instanceof Collection<?>) {
return ((Collection<?>) iterable).size();
}

View File

@ -416,12 +416,6 @@ public class FluentIterableTest {
@Test
public void size() {
try {
FluentIterable.of((Iterable<?>) null).size();
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
assertEquals(0, FluentIterable.of(emptyIterable).size());
assertEquals(IterableUtils.toList(iterableOdd).size(), FluentIterable.of(iterableOdd).size());
}

View File

@ -579,4 +579,9 @@ public class IterableUtilsTest {
// expected
}
}
@Test
public void size() {
assertEquals(0, IterableUtils.size(null));
}
}