Skip to content

Commit

Permalink
Improve signatures of methods for converting iterables to arrays.
Browse files Browse the repository at this point in the history
- Refine the signature of `Iterables.toArray` (and `Iterators.toArray`) by using `@NonNull`.
- Finish fixing the signature of `FluentIterable.toArray`, which we'd started doing in cl/519736884 but didn't finish: In that CL, we'd added `@NonNull` to fix a build error but not removed the `@Nullable` in the return type. Actually, that `@Nullable` probably _never_ made sense? Probably I had blindly added it as part of addressing a build error during cl/388680701 instead of reasoning out that it wasn't necessary. It's especially sad to have gone out of the way to add it when we ended up needing a suppression on the method regardless. I hope I didn't need to uglify any callers in response to the change....

RELNOTES=n/a
PiperOrigin-RevId: 537067780
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 1, 2023
1 parent b6193d1 commit 5c23590
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,8 @@ public final boolean isEmpty() {
* copied
*/
@GwtIncompatible // Array.newArray(Class, int)
public final @Nullable E[] toArray(Class<@NonNull E> type) {
return Iterables.toArray(getDelegate(), type);
public final E[] toArray(Class<@NonNull E> type) {
return Iterables.<E>toArray(getDelegate(), type);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions android/guava/src/com/google/common/collect/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.RandomAccess;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -323,12 +324,8 @@ public static String toString(Iterable<?> iterable) {
* @return a newly-allocated array into which all the elements of the iterable have been copied
*/
@GwtIncompatible // Array.newInstance(Class, int)
/*
* If we could express Class<@Nonnull T>, we could generalize the type parameter to <T extends
* @Nullable Object>, and then we could accept an Iterable<? extends T> and return a plain T[]
* instead of a @Nullable T[].
*/
public static <T> @Nullable T[] toArray(Iterable<? extends @Nullable T> iterable, Class<T> type) {
public static <T extends @Nullable Object> T[] toArray(
Iterable<? extends T> iterable, Class<@NonNull T> type) {
return toArray(iterable, ObjectArrays.newArray(type, 0));
}

Expand Down
9 changes: 5 additions & 4 deletions android/guava/src/com/google/common/collect/Iterators.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.PriorityQueue;
import java.util.Queue;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -344,10 +345,10 @@ public static String toString(Iterator<?> iterator) {
* @return a newly-allocated array into which all the elements of the iterator have been copied
*/
@GwtIncompatible // Array.newInstance(Class, int)
// For discussion of this signature, see the corresponding overload of *Iterables*.toArray.
public static <T> @Nullable T[] toArray(Iterator<? extends @Nullable T> iterator, Class<T> type) {
List<@Nullable T> list = Lists.newArrayList(iterator);
return Iterables.toArray(list, type);
public static <T extends @Nullable Object> T[] toArray(
Iterator<? extends T> iterator, Class<@NonNull T> type) {
List<T> list = Lists.newArrayList(iterator);
return Iterables.<T>toArray(list, type);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions guava/src/com/google/common/collect/FluentIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ public final boolean isEmpty() {
* copied
*/
@GwtIncompatible // Array.newArray(Class, int)
public final @Nullable E[] toArray(Class<@NonNull E> type) {
return Iterables.toArray(getDelegate(), type);
public final E[] toArray(Class<@NonNull E> type) {
return Iterables.<E>toArray(getDelegate(), type);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions guava/src/com/google/common/collect/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -285,12 +286,8 @@ public static String toString(Iterable<?> iterable) {
* @return a newly-allocated array into which all the elements of the iterable have been copied
*/
@GwtIncompatible // Array.newInstance(Class, int)
/*
* If we could express Class<@Nonnull T>, we could generalize the type parameter to <T extends
* @Nullable Object>, and then we could accept an Iterable<? extends T> and return a plain T[]
* instead of a @Nullable T[].
*/
public static <T> @Nullable T[] toArray(Iterable<? extends @Nullable T> iterable, Class<T> type) {
public static <T extends @Nullable Object> T[] toArray(
Iterable<? extends T> iterable, Class<@NonNull T> type) {
return toArray(iterable, ObjectArrays.newArray(type, 0));
}

Expand Down
9 changes: 5 additions & 4 deletions guava/src/com/google/common/collect/Iterators.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.PriorityQueue;
import java.util.Queue;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -344,10 +345,10 @@ public static String toString(Iterator<?> iterator) {
* @return a newly-allocated array into which all the elements of the iterator have been copied
*/
@GwtIncompatible // Array.newInstance(Class, int)
// For discussion of this signature, see the corresponding overload of *Iterables*.toArray.
public static <T> @Nullable T[] toArray(Iterator<? extends @Nullable T> iterator, Class<T> type) {
List<@Nullable T> list = Lists.newArrayList(iterator);
return Iterables.toArray(list, type);
public static <T extends @Nullable Object> T[] toArray(
Iterator<? extends T> iterator, Class<@NonNull T> type) {
List<T> list = Lists.newArrayList(iterator);
return Iterables.<T>toArray(list, type);
}

/**
Expand Down

0 comments on commit 5c23590

Please sign in to comment.