From 5c2359087acc36c86ed42f1875ce69b7be231868 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Thu, 1 Jun 2023 10:45:05 -0700 Subject: [PATCH] Improve signatures of methods for converting iterables to arrays. - 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 --- .../src/com/google/common/collect/FluentIterable.java | 4 ++-- .../guava/src/com/google/common/collect/Iterables.java | 9 +++------ .../guava/src/com/google/common/collect/Iterators.java | 9 +++++---- guava/src/com/google/common/collect/FluentIterable.java | 4 ++-- guava/src/com/google/common/collect/Iterables.java | 9 +++------ guava/src/com/google/common/collect/Iterators.java | 9 +++++---- 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/android/guava/src/com/google/common/collect/FluentIterable.java b/android/guava/src/com/google/common/collect/FluentIterable.java index d4a19cffaaf1..64b5ed1cfd93 100644 --- a/android/guava/src/com/google/common/collect/FluentIterable.java +++ b/android/guava/src/com/google/common/collect/FluentIterable.java @@ -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.toArray(getDelegate(), type); } /** diff --git a/android/guava/src/com/google/common/collect/Iterables.java b/android/guava/src/com/google/common/collect/Iterables.java index f4e664a2081e..5fc18f8aefc7 100644 --- a/android/guava/src/com/google/common/collect/Iterables.java +++ b/android/guava/src/com/google/common/collect/Iterables.java @@ -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; /** @@ -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 , and then we could accept an Iterable and return a plain T[] - * instead of a @Nullable T[]. - */ - public static @Nullable T[] toArray(Iterable iterable, Class type) { + public static T[] toArray( + Iterable iterable, Class<@NonNull T> type) { return toArray(iterable, ObjectArrays.newArray(type, 0)); } diff --git a/android/guava/src/com/google/common/collect/Iterators.java b/android/guava/src/com/google/common/collect/Iterators.java index 1f3e55c646a9..0699202fefbd 100644 --- a/android/guava/src/com/google/common/collect/Iterators.java +++ b/android/guava/src/com/google/common/collect/Iterators.java @@ -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; /** @@ -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 @Nullable T[] toArray(Iterator iterator, Class type) { - List<@Nullable T> list = Lists.newArrayList(iterator); - return Iterables.toArray(list, type); + public static T[] toArray( + Iterator iterator, Class<@NonNull T> type) { + List list = Lists.newArrayList(iterator); + return Iterables.toArray(list, type); } /** diff --git a/guava/src/com/google/common/collect/FluentIterable.java b/guava/src/com/google/common/collect/FluentIterable.java index 4ad12926a82b..fd8178d7c479 100644 --- a/guava/src/com/google/common/collect/FluentIterable.java +++ b/guava/src/com/google/common/collect/FluentIterable.java @@ -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.toArray(getDelegate(), type); } /** diff --git a/guava/src/com/google/common/collect/Iterables.java b/guava/src/com/google/common/collect/Iterables.java index 4e20a4efc513..eef28a4fdf11 100644 --- a/guava/src/com/google/common/collect/Iterables.java +++ b/guava/src/com/google/common/collect/Iterables.java @@ -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; /** @@ -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 , and then we could accept an Iterable and return a plain T[] - * instead of a @Nullable T[]. - */ - public static @Nullable T[] toArray(Iterable iterable, Class type) { + public static T[] toArray( + Iterable iterable, Class<@NonNull T> type) { return toArray(iterable, ObjectArrays.newArray(type, 0)); } diff --git a/guava/src/com/google/common/collect/Iterators.java b/guava/src/com/google/common/collect/Iterators.java index 1f3e55c646a9..0699202fefbd 100644 --- a/guava/src/com/google/common/collect/Iterators.java +++ b/guava/src/com/google/common/collect/Iterators.java @@ -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; /** @@ -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 @Nullable T[] toArray(Iterator iterator, Class type) { - List<@Nullable T> list = Lists.newArrayList(iterator); - return Iterables.toArray(list, type); + public static T[] toArray( + Iterator iterator, Class<@NonNull T> type) { + List list = Lists.newArrayList(iterator); + return Iterables.toArray(list, type); } /**