Skip to content

Commit

Permalink
Make SeparatedOptionListConverter declare that it returns an Immutabl…
Browse files Browse the repository at this point in the history
…eList.

PiperOrigin-RevId: 435790755
  • Loading branch information
janakdr authored and copybara-github committed Mar 19, 2022
1 parent 987bf12 commit 49625ea
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ static String joinEnglishList(Iterable<?> choices) {
return buf.length() == 0 ? "nothing" : buf.toString();
}

public static class SeparatedOptionListConverter implements Converter<List<String>> {

/** Converter for a list of options, separated by some separator character. */
public static class SeparatedOptionListConverter implements Converter<ImmutableList<String>> {
private final String separatorDescription;
private final Splitter splitter;
private final boolean allowEmptyValues;
Expand All @@ -264,8 +264,8 @@ protected SeparatedOptionListConverter(
}

@Override
public List<String> convert(String input) throws OptionsParsingException {
List<String> result =
public ImmutableList<String> convert(String input) throws OptionsParsingException {
ImmutableList<String> result =
input.isEmpty() ? ImmutableList.of() : ImmutableList.copyOf(splitter.split(input));
if (!allowEmptyValues && result.contains("")) {
// If the list contains exactly the empty string, it means an empty value was passed and we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
package com.google.devtools.common.options;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import java.util.List;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** A test for {@link Converters.CommaSeparatedOptionListConverter}. */
@RunWith(JUnit4.class)
public class CommaSeparatedOptionListConverterTest {

private Converter<List<String>> converter =
private final Converter<ImmutableList<String>> converter =
new Converters.CommaSeparatedOptionListConverter();

@Test
Expand Down Expand Up @@ -65,13 +63,4 @@ public void multiWords() throws Exception {
public void spaceIsIgnored() throws Exception {
assertThat(converter.convert("one two three")).containsExactly("one two three");
}

@Test
public void valueisUnmodifiable() throws Exception {
assertThrows(
"could modify value",
UnsupportedOperationException.class,
() -> converter.convert("value").add("other"));
}

}

0 comments on commit 49625ea

Please sign in to comment.