diff --git a/android/guava-tests/test/com/google/common/base/SplitterTest.java b/android/guava-tests/test/com/google/common/base/SplitterTest.java index b9bf3e8e82a4..71a37a5f08e0 100644 --- a/android/guava-tests/test/com/google/common/base/SplitterTest.java +++ b/android/guava-tests/test/com/google/common/base/SplitterTest.java @@ -17,6 +17,7 @@ package com.google.common.base; import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.truth.Truth.assertThat; import com.google.common.annotations.GwtCompatible; @@ -63,6 +64,12 @@ public void testCharacterSimpleSplitToList() { assertThat(letters).containsExactly("a", "b", "c").inOrder(); } + public void testCharacterSimpleSplitToStream() { + String simple = "a,b,c"; + List letters = COMMA_SPLITTER.splitToStream(simple).collect(toImmutableList()); + assertThat(letters).containsExactly("a", "b", "c").inOrder(); + } + public void testToString() { assertEquals("[]", COMMA_SPLITTER.split("").toString()); assertEquals("[a, b, c]", COMMA_SPLITTER.split("a,b,c").toString()); diff --git a/android/guava/src/com/google/common/base/Splitter.java b/android/guava/src/com/google/common/base/Splitter.java index 383425346ed9..cda13dfd91e4 100644 --- a/android/guava/src/com/google/common/base/Splitter.java +++ b/android/guava/src/com/google/common/base/Splitter.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import javax.annotation.CheckForNull; /** @@ -423,6 +425,23 @@ public List splitToList(CharSequence sequence) { return Collections.unmodifiableList(result); } + /** + * Splits {@code sequence} into string components and makes them available through an {@link + * Stream}, which may be lazily evaluated. If you want an eagerly computed {@link List}, use + * {@link #splitToList(CharSequence)}. + * + * @param sequence the sequence of characters to split + * @return a stream over the segments split from the parameter + * @since NEXT (but since 28.2 in the JRE flavor) + */ + @SuppressWarnings("Java7ApiChecker") + // If users use this when they shouldn't, we hope that NewApi will catch subsequent Stream calls. + @IgnoreJRERequirement + public Stream splitToStream(CharSequence sequence) { + // Can't use Streams.stream() from base + return StreamSupport.stream(split(sequence).spliterator(), false); + } + /** * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries * into keys and values using the specified separator. diff --git a/guava/src/com/google/common/base/Splitter.java b/guava/src/com/google/common/base/Splitter.java index c775b3553bc4..9ba2d8131245 100644 --- a/guava/src/com/google/common/base/Splitter.java +++ b/guava/src/com/google/common/base/Splitter.java @@ -432,7 +432,7 @@ public List splitToList(CharSequence sequence) { * * @param sequence the sequence of characters to split * @return a stream over the segments split from the parameter - * @since 28.2 + * @since 28.2 (but only since 33.4.0 in the Android flavor) */ public Stream splitToStream(CharSequence sequence) { // Can't use Streams.stream() from base