Skip to content

Commit 6082782

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Make Splitter.splitToStream available to Android users.
RELNOTES=`base`: Made `Splitter.splitToStream` available to Android users. PiperOrigin-RevId: 687039853
1 parent 99be0a4 commit 6082782

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

android/guava-tests/test/com/google/common/base/SplitterTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.base;
1818

1919
import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows;
20+
import static com.google.common.collect.ImmutableList.toImmutableList;
2021
import static com.google.common.truth.Truth.assertThat;
2122

2223
import com.google.common.annotations.GwtCompatible;
@@ -63,6 +64,12 @@ public void testCharacterSimpleSplitToList() {
6364
assertThat(letters).containsExactly("a", "b", "c").inOrder();
6465
}
6566

67+
public void testCharacterSimpleSplitToStream() {
68+
String simple = "a,b,c";
69+
List<String> letters = COMMA_SPLITTER.splitToStream(simple).collect(toImmutableList());
70+
assertThat(letters).containsExactly("a", "b", "c").inOrder();
71+
}
72+
6673
public void testToString() {
6774
assertEquals("[]", COMMA_SPLITTER.split("").toString());
6875
assertEquals("[a, b, c]", COMMA_SPLITTER.split("a,b,c").toString());

android/guava/src/com/google/common/base/Splitter.java

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.regex.Pattern;
29+
import java.util.stream.Stream;
30+
import java.util.stream.StreamSupport;
2931
import javax.annotation.CheckForNull;
3032

3133
/**
@@ -423,6 +425,23 @@ public List<String> splitToList(CharSequence sequence) {
423425
return Collections.unmodifiableList(result);
424426
}
425427

428+
/**
429+
* Splits {@code sequence} into string components and makes them available through an {@link
430+
* Stream}, which may be lazily evaluated. If you want an eagerly computed {@link List}, use
431+
* {@link #splitToList(CharSequence)}.
432+
*
433+
* @param sequence the sequence of characters to split
434+
* @return a stream over the segments split from the parameter
435+
* @since NEXT (but since 28.2 in the JRE flavor)
436+
*/
437+
@SuppressWarnings("Java7ApiChecker")
438+
// If users use this when they shouldn't, we hope that NewApi will catch subsequent Stream calls.
439+
@IgnoreJRERequirement
440+
public Stream<String> splitToStream(CharSequence sequence) {
441+
// Can't use Streams.stream() from base
442+
return StreamSupport.stream(split(sequence).spliterator(), false);
443+
}
444+
426445
/**
427446
* Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries
428447
* into keys and values using the specified separator.

guava/src/com/google/common/base/Splitter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public List<String> splitToList(CharSequence sequence) {
432432
*
433433
* @param sequence the sequence of characters to split
434434
* @return a stream over the segments split from the parameter
435-
* @since 28.2
435+
* @since 28.2 (but only since 33.4.0 in the Android flavor)
436436
*/
437437
public Stream<String> splitToStream(CharSequence sequence) {
438438
// Can't use Streams.stream() from base

0 commit comments

Comments
 (0)