Skip to content

Commit

Permalink
Rewrite Iterables.toArray to work correctly in the presence of concur…
Browse files Browse the repository at this point in the history
…rent modification (#1558).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=105071914
  • Loading branch information
lowasser authored and cpovirk committed Oct 12, 2015
1 parent 80b78c8 commit 0cdfef6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Expand Up @@ -295,7 +295,7 @@ public static <T> T getOnlyElement(Iterable<? extends T> iterable, @Nullable T d
} }


static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) { static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) {
Collection<? extends T> collection = toCollection(iterable); Collection<? extends T> collection = castOrCopyToCollection(iterable);
return collection.toArray(array); return collection.toArray(array);
} }


Expand All @@ -307,15 +307,15 @@ static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) {
* have been copied * have been copied
*/ */
static Object[] toArray(Iterable<?> iterable) { static Object[] toArray(Iterable<?> iterable) {
return toCollection(iterable).toArray(); return castOrCopyToCollection(iterable).toArray();
} }


/** /**
* Converts an iterable into a collection. If the iterable is already a * Converts an iterable into a collection. If the iterable is already a
* collection, it is returned. Otherwise, an {@link java.util.ArrayList} is * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is
* created with the contents of the iterable in the same iteration order. * created with the contents of the iterable in the same iteration order.
*/ */
private static <E> Collection<E> toCollection(Iterable<E> iterable) { private static <E> Collection<E> castOrCopyToCollection(Iterable<E> iterable) {
return (iterable instanceof Collection) return (iterable instanceof Collection)
? (Collection<E>) iterable ? (Collection<E>) iterable
: Lists.newArrayList(iterable.iterator()); : Lists.newArrayList(iterable.iterator());
Expand Down
10 changes: 4 additions & 6 deletions guava/src/com/google/common/collect/Iterables.java
Expand Up @@ -306,13 +306,11 @@ public static <T> T getOnlyElement(Iterable<? extends T> iterable, @Nullable T d
*/ */
@GwtIncompatible("Array.newInstance(Class, int)") @GwtIncompatible("Array.newInstance(Class, int)")
public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
Collection<? extends T> collection = toCollection(iterable); return toArray(iterable, ObjectArrays.newArray(type, 0));
T[] array = ObjectArrays.newArray(type, collection.size());
return collection.toArray(array);
} }


static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) { static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) {
Collection<? extends T> collection = toCollection(iterable); Collection<? extends T> collection = castOrCopyToCollection(iterable);
return collection.toArray(array); return collection.toArray(array);
} }


Expand All @@ -324,15 +322,15 @@ static <T> T[] toArray(Iterable<? extends T> iterable, T[] array) {
* have been copied * have been copied
*/ */
static Object[] toArray(Iterable<?> iterable) { static Object[] toArray(Iterable<?> iterable) {
return toCollection(iterable).toArray(); return castOrCopyToCollection(iterable).toArray();
} }


/** /**
* Converts an iterable into a collection. If the iterable is already a * Converts an iterable into a collection. If the iterable is already a
* collection, it is returned. Otherwise, an {@link java.util.ArrayList} is * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is
* created with the contents of the iterable in the same iteration order. * created with the contents of the iterable in the same iteration order.
*/ */
private static <E> Collection<E> toCollection(Iterable<E> iterable) { private static <E> Collection<E> castOrCopyToCollection(Iterable<E> iterable) {
return (iterable instanceof Collection) return (iterable instanceof Collection)
? (Collection<E>) iterable ? (Collection<E>) iterable
: Lists.newArrayList(iterable.iterator()); : Lists.newArrayList(iterable.iterator());
Expand Down

0 comments on commit 0cdfef6

Please sign in to comment.