Skip to content

Commit

Permalink
Make Splitter.splitToStream available to Android users.
Browse files Browse the repository at this point in the history
RELNOTES=`base`: Made `Splitter.splitToStream` available to Android users.
PiperOrigin-RevId: 687039853
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 17, 2024
1 parent 99be0a4 commit 6082782
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,12 @@ public void testCharacterSimpleSplitToList() {
assertThat(letters).containsExactly("a", "b", "c").inOrder();
}

public void testCharacterSimpleSplitToStream() {
String simple = "a,b,c";
List<String> 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());
Expand Down
19 changes: 19 additions & 0 deletions android/guava/src/com/google/common/base/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -423,6 +425,23 @@ public List<String> 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<String> 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.
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/base/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public List<String> 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<String> splitToStream(CharSequence sequence) {
// Can't use Streams.stream() from base
Expand Down

0 comments on commit 6082782

Please sign in to comment.