diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterables.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterables.java index 03e7014cfaa8..8236b2587c51 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterables.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterables.java @@ -295,7 +295,7 @@ public static T getOnlyElement(Iterable iterable, @Nullable T d } static T[] toArray(Iterable iterable, T[] array) { - Collection collection = toCollection(iterable); + Collection collection = castOrCopyToCollection(iterable); return collection.toArray(array); } @@ -307,7 +307,7 @@ static T[] toArray(Iterable iterable, T[] array) { * have been copied */ static Object[] toArray(Iterable iterable) { - return toCollection(iterable).toArray(); + return castOrCopyToCollection(iterable).toArray(); } /** @@ -315,7 +315,7 @@ static Object[] toArray(Iterable iterable) { * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is * created with the contents of the iterable in the same iteration order. */ - private static Collection toCollection(Iterable iterable) { + private static Collection castOrCopyToCollection(Iterable iterable) { return (iterable instanceof Collection) ? (Collection) iterable : Lists.newArrayList(iterable.iterator()); diff --git a/guava/src/com/google/common/collect/Iterables.java b/guava/src/com/google/common/collect/Iterables.java index 18fd2aeed178..fca89e372d92 100644 --- a/guava/src/com/google/common/collect/Iterables.java +++ b/guava/src/com/google/common/collect/Iterables.java @@ -306,13 +306,11 @@ public static T getOnlyElement(Iterable iterable, @Nullable T d */ @GwtIncompatible("Array.newInstance(Class, int)") public static T[] toArray(Iterable iterable, Class type) { - Collection collection = toCollection(iterable); - T[] array = ObjectArrays.newArray(type, collection.size()); - return collection.toArray(array); + return toArray(iterable, ObjectArrays.newArray(type, 0)); } static T[] toArray(Iterable iterable, T[] array) { - Collection collection = toCollection(iterable); + Collection collection = castOrCopyToCollection(iterable); return collection.toArray(array); } @@ -324,7 +322,7 @@ static T[] toArray(Iterable iterable, T[] array) { * have been copied */ static Object[] toArray(Iterable iterable) { - return toCollection(iterable).toArray(); + return castOrCopyToCollection(iterable).toArray(); } /** @@ -332,7 +330,7 @@ static Object[] toArray(Iterable iterable) { * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is * created with the contents of the iterable in the same iteration order. */ - private static Collection toCollection(Iterable iterable) { + private static Collection castOrCopyToCollection(Iterable iterable) { return (iterable instanceof Collection) ? (Collection) iterable : Lists.newArrayList(iterable.iterator());