Skip to content

Commit 5c23590

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedJun 1, 2023
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
1 parent b6193d1 commit 5c23590

File tree

6 files changed

+20
-24
lines changed

6 files changed

+20
-24
lines changed
 

‎android/guava/src/com/google/common/collect/FluentIterable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ public final boolean isEmpty() {
785785
* copied
786786
*/
787787
@GwtIncompatible // Array.newArray(Class, int)
788-
public final @Nullable E[] toArray(Class<@NonNull E> type) {
789-
return Iterables.toArray(getDelegate(), type);
788+
public final E[] toArray(Class<@NonNull E> type) {
789+
return Iterables.<E>toArray(getDelegate(), type);
790790
}
791791

792792
/**

‎android/guava/src/com/google/common/collect/Iterables.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.RandomAccess;
3737
import java.util.Set;
3838
import javax.annotation.CheckForNull;
39+
import org.checkerframework.checker.nullness.qual.NonNull;
3940
import org.checkerframework.checker.nullness.qual.Nullable;
4041

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

‎android/guava/src/com/google/common/collect/Iterators.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.PriorityQueue;
4848
import java.util.Queue;
4949
import javax.annotation.CheckForNull;
50+
import org.checkerframework.checker.nullness.qual.NonNull;
5051
import org.checkerframework.checker.nullness.qual.Nullable;
5152

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

353354
/**

‎guava/src/com/google/common/collect/FluentIterable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ public final boolean isEmpty() {
777777
* copied
778778
*/
779779
@GwtIncompatible // Array.newArray(Class, int)
780-
public final @Nullable E[] toArray(Class<@NonNull E> type) {
781-
return Iterables.toArray(getDelegate(), type);
780+
public final E[] toArray(Class<@NonNull E> type) {
781+
return Iterables.<E>toArray(getDelegate(), type);
782782
}
783783

784784
/**

‎guava/src/com/google/common/collect/Iterables.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.function.Consumer;
4040
import java.util.stream.Stream;
4141
import javax.annotation.CheckForNull;
42+
import org.checkerframework.checker.nullness.qual.NonNull;
4243
import org.checkerframework.checker.nullness.qual.Nullable;
4344

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

‎guava/src/com/google/common/collect/Iterators.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.PriorityQueue;
4848
import java.util.Queue;
4949
import javax.annotation.CheckForNull;
50+
import org.checkerframework.checker.nullness.qual.NonNull;
5051
import org.checkerframework.checker.nullness.qual.Nullable;
5152

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

353354
/**

0 commit comments

Comments
 (0)
Please sign in to comment.