/** * Returns a splitter that behaves equivalently to {@code this} splitter, but automatically omits * empty strings from the results. For example, {@code * Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,")} returns an iterable containing only * {@code ["a", "b", "c"]}. * * <p>If either {@code trimResults} option is also specified when creating a splitter, that * splitter always trims results first before checking for emptiness. So, for example, {@code * Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ")} returns an empty iterable. * * <p>Note that it is ordinarily not possible for {@link #split(CharSequence)} to return an empty * iterable, but when using this option, it can (if the input sequence consists of nothing but * separators). * * @return a splitter with the desired configuration */ public Splitter omitEmptyStrings() { return new Splitter(strategy, true, trimmer, limit); }
/** * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries * into keys and values using the specified separator. * * @since 14.0 */ @Beta public MapSplitter withKeyValueSeparator(char separator) { return withKeyValueSeparator(on(separator)); }
/** * Returns an iterator over the lines in the string. If the string ends in a newline, a final * empty string is not included, to match the behavior of BufferedReader/LineReader.readLine(). */ private Iterator<String> linesIterator() { return new AbstractIterator<String>() { Iterator<String> lines = LINE_SPLITTER.split(seq).iterator(); @Override protected String computeNext() { if (lines.hasNext()) { String next = lines.next(); // skip last line if it's empty if (lines.hasNext() || !next.isEmpty()) { return next; } } return endOfData(); } }; }
Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname); List<String> path = new ArrayList<>();
/** * Converts a field mask from a Proto3 JSON string, that is splitting the paths along commas and * converting from camel case to snake case. */ public static FieldMask fromJsonString(String value) { Iterable<String> paths = Splitter.on(FIELD_PATH_SEPARATOR).split(value); FieldMask.Builder builder = FieldMask.newBuilder(); for (String path : paths) { if (path.isEmpty()) { continue; } builder.addPaths(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, path)); } return builder.build(); }
/** * Returns a splitter that uses the given single-character separator. For example, {@code * Splitter.on(',').split("foo,,bar")} returns an iterable containing {@code ["foo", "", "bar"]}. * * @param separator the character to recognize as a separator * @return a splitter, with default settings, that recognizes that separator */ public static Splitter on(char separator) { return on(CharMatcher.is(separator)); }
/** * Returns a splitter that behaves equivalently to {@code this} splitter, but automatically * removes leading and trailing {@linkplain CharMatcher#whitespace whitespace} from each returned * substring; equivalent to {@code trimResults(CharMatcher.whitespace())}. For example, {@code * Splitter.on(',').trimResults().split(" a, b ,c ")} returns an iterable containing {@code ["a", * "b", "c"]}. * * @return a splitter with the desired configuration */ public Splitter trimResults() { return trimResults(CharMatcher.whitespace()); }
/** * 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 = splittingIterator(sequence); List<String> result = new ArrayList<>(); while (iterator.hasNext()) { result.add(iterator.next()); } return Collections.unmodifiableList(result); }
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ @VisibleForTesting // TODO(b/65488446): Make this a public API. static ImmutableList<URL> parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { logger.log(WARNING, "malformed classpath entry: " + entry, e); } } return urls.build(); }
/** * Returns a splitter that considers any subsequence matching {@code pattern} to be a separator. * For example, {@code Splitter.on(Pattern.compile("\r?\n")).split(entireFile)} splits a string * into lines whether it uses DOS-style or UNIX-style line terminators. * * @param separatorPattern the pattern that determines whether a subsequence is a separator. This * pattern may not match the empty string. * @return a splitter, with default settings, that uses this pattern * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string */ @GwtIncompatible // java.util.regex public static Splitter on(Pattern separatorPattern) { return on(new JdkPattern(separatorPattern)); }
/** * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries * into keys and values using the specified separator. * * @since 10.0 */ @Beta public MapSplitter withKeyValueSeparator(String separator) { return withKeyValueSeparator(on(separator)); }
@NullableDecl private static byte[] textToNumericFormatV4(String ipString) { byte[] bytes = new byte[IPV4_PART_COUNT]; int i = 0; try { for (String octet : IPV4_SPLITTER.split(ipString)) { bytes[i++] = parseOctet(octet); } } catch (NumberFormatException ex) { return null; } return i == IPV4_PART_COUNT ? bytes : null; }
/** * Returns a splitter that behaves equivalently to {@code this} splitter, but removes all leading * or trailing characters matching the given {@code CharMatcher} from each returned substring. For * example, {@code Splitter.on(',').trimResults(CharMatcher.is('_')).split("_a ,_b_ ,c__")} * returns an iterable containing {@code ["a ", "b_ ", "c"]}. * * @param trimmer a {@link CharMatcher} that determines whether a character should be removed from * the beginning/end of a subsequence * @return a splitter with the desired configuration */ // TODO(kevinb): throw if a trimmer was already specified! public Splitter trimResults(CharMatcher trimmer) { checkNotNull(trimmer); return new Splitter(strategy, omitEmptyStrings, trimmer, limit); }
/** * Returns a splitter that considers any subsequence matching a given pattern (regular expression) * to be a separator. For example, {@code Splitter.onPattern("\r?\n").split(entireFile)} splits a * string into lines whether it uses DOS-style or UNIX-style line terminators. This is equivalent * to {@code Splitter.on(Pattern.compile(pattern))}. * * @param separatorPattern the pattern that determines whether a subsequence is a separator. This * pattern may not match the empty string. * @return a splitter, with default settings, that uses this pattern * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string or is a * malformed expression */ @GwtIncompatible // java.util.regex public static Splitter onPattern(String separatorPattern) { return on(Platform.compilePattern(separatorPattern)); }
/** * Splits {@code sequence} into substrings, splits each substring into an entry, and returns an * unmodifiable map with each of the entries. For example, {@code * Splitter.on(';').trimResults().withKeyValueSeparator("=>").split("a=>b ; c=>b")} will return * a mapping from {@code "a"} to {@code "b"} and {@code "c"} to {@code "b"}. * * <p>The returned map preserves the order of the entries from {@code sequence}. * * @throws IllegalArgumentException if the specified sequence does not split into valid map * entries, or if there are duplicate keys */ public Map<String, String> split(CharSequence sequence) { Map<String, String> map = new LinkedHashMap<>(); for (String entry : outerSplitter.split(sequence)) { Iterator<String> entryFields = entrySplitter.splittingIterator(entry); checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); String key = entryFields.next(); checkArgument(!map.containsKey(key), "Duplicate key [%s] found.", key); checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); String value = entryFields.next(); map.put(key, value); checkArgument(!entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); } return Collections.unmodifiableMap(map); } }
/** * Returns a splitter that behaves equivalently to {@code this} splitter but stops splitting after * it reaches the limit. The limit defines the maximum number of items returned by the iterator, * or the maximum size of the list returned by {@link #splitToList}. * * <p>For example, {@code Splitter.on(',').limit(3).split("a,b,c,d")} returns an iterable * containing {@code ["a", "b", "c,d"]}. When omitting empty strings, the omitted strings do not * count. Hence, {@code Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d")} returns * an iterable containing {@code ["a", "b", "c,d"}. When trim is requested, all entries are * trimmed, including the last. Hence {@code Splitter.on(',').limit(3).trimResults().split(" a , b * , c , d ")} results in {@code ["a", "b", "c , d"]}. * * @param limit the maximum number of items returned * @return a splitter with the desired configuration * @since 9.0 */ public Splitter limit(int limit) { checkArgument(limit > 0, "must be greater than zero: %s", limit); return new Splitter(strategy, omitEmptyStrings, trimmer, limit); }
/** * Creates a CacheBuilderSpec from a string. * * @param cacheBuilderSpecification the string form */ public static CacheBuilderSpec parse(String cacheBuilderSpecification) { CacheBuilderSpec spec = new CacheBuilderSpec(cacheBuilderSpecification); if (!cacheBuilderSpecification.isEmpty()) { for (String keyValuePair : KEYS_SPLITTER.split(cacheBuilderSpecification)) { List<String> keyAndValue = ImmutableList.copyOf(KEY_VALUE_SPLITTER.split(keyValuePair)); checkArgument(!keyAndValue.isEmpty(), "blank key-value pair"); checkArgument( keyAndValue.size() <= 2, "key-value pair %s with more than one equals sign", keyValuePair); // Find the ValueParser for the current key. String key = keyAndValue.get(0); ValueParser valueParser = VALUE_PARSERS.get(key); checkArgument(valueParser != null, "unknown key %s", key); String value = keyAndValue.size() == 1 ? null : keyAndValue.get(1); valueParser.parse(spec, key, value); } } return spec; }
/** * Returns a splitter that considers any single character matched by the given {@code CharMatcher} * to be a separator. For example, {@code * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an iterable containing * {@code ["foo", "", "bar", "quux"]}. * * @param separatorMatcher a {@link CharMatcher} that determines whether a character is a * separator * @return a splitter, with default settings, that uses this matcher */ public static Splitter on(final CharMatcher separatorMatcher) { checkNotNull(separatorMatcher); return new Splitter( new Strategy() { @Override public SplittingIterator iterator(Splitter splitter, final CharSequence toSplit) { return new SplittingIterator(splitter, toSplit) { @Override int separatorStart(int start) { return separatorMatcher.indexIn(toSplit, start); } @Override int separatorEnd(int separatorPosition) { return separatorPosition + 1; } }; } }); }
manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH.toString()); if (classpathAttribute != null) { for (String path : CLASS_PATH_ATTRIBUTE_SEPARATOR.split(classpathAttribute)) { URL url; try {