From 6e2d0d8296f43c68657ecb09d366d358c939700c Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 2 Feb 2024 16:09:18 -0800 Subject: [PATCH] Add an overload of `spliterator()` to the Android flavor of one of our classes. This is a further attempt to shake out any problems in advance of exposing "real" Java 8 APIs (https://github.com/google/guava/issues/6567). If something goes wrong with `spliterator()`, I think we could remove it without breaking callers: Either it's an override (in which case calls to it will continue to work after it's removed) or it's not (in which case Java 8 APIs aren't available, so calls to it would never have worked.) RELNOTES=n/a PiperOrigin-RevId: 603812702 --- .../common/collect/ImmutableCollection.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/android/guava/src/com/google/common/collect/ImmutableCollection.java b/android/guava/src/com/google/common/collect/ImmutableCollection.java index 1da069a2f19b..c8167e8f61cc 100644 --- a/android/guava/src/com/google/common/collect/ImmutableCollection.java +++ b/android/guava/src/com/google/common/collect/ImmutableCollection.java @@ -36,6 +36,8 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; import javax.annotation.CheckForNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -176,6 +178,11 @@ public abstract class ImmutableCollection extends AbstractCollection imple * These are properties of the collection as a whole; SIZED and SUBSIZED are more properties of * the spliterator implementation. */ + @SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"}) + // @IgnoreJRERequirement is not necessary because this compiles down to a constant. + // (which is fortunate because Animal Sniffer doesn't look for @IgnoreJRERequirement on fields) + static final int SPLITERATOR_CHARACTERISTICS = + Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED; ImmutableCollection() {} @@ -183,6 +190,14 @@ public abstract class ImmutableCollection extends AbstractCollection imple @Override public abstract UnmodifiableIterator iterator(); + @Override + @SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"}) + @IgnoreJRERequirement // used only from APIs with Java 8 types in them + // (not used within guava-android as of this writing, but we include it in the jar as a test) + public Spliterator spliterator() { + return Spliterators.spliterator(this, SPLITERATOR_CHARACTERISTICS); + } + private static final Object[] EMPTY_ARRAY = {}; @Override