Skip to content

Commit

Permalink
Open source Splitter#splitToList.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=43611289
  • Loading branch information
lowasser committed Mar 8, 2013
1 parent ad8f574 commit 9a76276
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Expand Up @@ -22,9 +22,11 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckReturnValue;
Expand Down Expand Up @@ -316,7 +318,8 @@ public Splitter trimResults(CharMatcher trimmer) {

/**
* Splits {@code sequence} into string components and makes them available
* through an {@link Iterator}, which may be lazily evaluated.
* through an {@link Iterator}, 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 an iteration over the segments split from the parameter.
Expand All @@ -341,6 +344,29 @@ private Iterator<String> spliterator(CharSequence sequence) {
return strategy.iterator(this, sequence);
}

/**
* Splits {@code sequence} into string components and returns them as
* an immutable list. If you want an {@link Iterable} which may be lazily
* evaluated, use {@link #split(CharSequence)}.
*
* @param sequence the sequence of characters to split
* @return an immutable list of the segments split from the parameter
* @since 15.0
*/
@Beta
public List<String> splitToList(CharSequence sequence) {
checkNotNull(sequence);

Iterator<String> iterator = spliterator(sequence);
List<String> result = new ArrayList<String>();

while (iterator.hasNext()) {
result.add(iterator.next());
}

return Collections.unmodifiableList(result);
}

/**
* Returns a {@code MapSplitter} which splits entries based on this splitter,
* and splits entries into keys and values using the specified separator.
Expand Down Expand Up @@ -531,3 +557,4 @@ protected SplittingIterator(Splitter splitter, CharSequence toSplit) {
}
}
}

14 changes: 14 additions & 0 deletions guava-tests/test/com/google/common/base/SplitterTest.java
Expand Up @@ -54,6 +54,20 @@ public void testCharacterSimpleSplit() {
ASSERT.that(letters).iteratesOverSequence("a", "b", "c");
}

/**
* All of the infrastructure of split and splitToString is identical, so we
* do one test of splitToString. All other cases should be covered by testing
* of split.
*
* <p>TODO(user): It would be good to make all the relevant tests run on
* both split and splitToString automatically.
*/
public void testCharacterSimpleSplitToList() {
String simple = "a,b,c";
List<String> letters = COMMA_SPLITTER.splitToList(simple);
ASSERT.that(letters).iteratesOverSequence("a", "b", "c");
}

public void testToString() {
assertEquals("[]", Splitter.on(',').split("").toString());
assertEquals("[a, b, c]", Splitter.on(',').split("a,b,c").toString());
Expand Down
28 changes: 27 additions & 1 deletion guava/src/com/google/common/base/Splitter.java
Expand Up @@ -23,9 +23,11 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -374,7 +376,8 @@ public Splitter trimResults(CharMatcher trimmer) {

/**
* Splits {@code sequence} into string components and makes them available
* through an {@link Iterator}, which may be lazily evaluated.
* through an {@link Iterator}, 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 an iteration over the segments split from the parameter.
Expand All @@ -399,6 +402,29 @@ private Iterator<String> spliterator(CharSequence sequence) {
return strategy.iterator(this, sequence);
}

/**
* Splits {@code sequence} into string components and returns them as
* an immutable list. If you want an {@link Iterable} which may be lazily
* evaluated, use {@link #split(CharSequence)}.
*
* @param sequence the sequence of characters to split
* @return an immutable list of the segments split from the parameter
* @since 15.0
*/
@Beta
public List<String> splitToList(CharSequence sequence) {
checkNotNull(sequence);

Iterator<String> iterator = spliterator(sequence);
List<String> result = new ArrayList<String>();

while (iterator.hasNext()) {
result.add(iterator.next());
}

return Collections.unmodifiableList(result);
}

/**
* Returns a {@code MapSplitter} which splits entries based on this splitter,
* and splits entries into keys and values using the specified separator.
Expand Down

0 comments on commit 9a76276

Please sign in to comment.