001    /*
002     * Copyright (C) 2009 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.base;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    import static com.google.common.base.Preconditions.checkNotNull;
021    
022    import com.google.common.annotations.Beta;
023    import com.google.common.annotations.GwtCompatible;
024    import com.google.common.annotations.GwtIncompatible;
025    
026    import java.util.Collections;
027    import java.util.Iterator;
028    import java.util.LinkedHashMap;
029    import java.util.Map;
030    import java.util.regex.Matcher;
031    import java.util.regex.Pattern;
032    
033    import javax.annotation.CheckReturnValue;
034    
035    /**
036     * An object that divides strings (or other instances of {@code CharSequence})
037     * into substrings, by recognizing a <i>separator</i> (a.k.a. "delimiter")
038     * which can be expressed as a single character, literal string, regular
039     * expression, {@code CharMatcher}, or by using a fixed substring length. This
040     * class provides the complementary functionality to {@link Joiner}.
041     *
042     * <p>Here is the most basic example of {@code Splitter} usage: <pre>   {@code
043     *
044     *   Splitter.on(',').split("foo,bar")}</pre>
045     *
046     * This invocation returns an {@code Iterable<String>} containing {@code "foo"}
047     * and {@code "bar"}, in that order.
048     *
049     * <p>By default {@code Splitter}'s behavior is very simplistic: <pre>   {@code
050     *
051     *   Splitter.on(',').split("foo,,bar, quux")}</pre>
052     *
053     * This returns an iterable containing {@code ["foo", "", "bar", " quux"]}.
054     * Notice that the splitter does not assume that you want empty strings removed,
055     * or that you wish to trim whitespace. If you want features like these, simply
056     * ask for them: <pre> {@code
057     *
058     *   private static final Splitter MY_SPLITTER = Splitter.on(',')
059     *       .trimResults()
060     *       .omitEmptyStrings();}</pre>
061     *
062     * Now {@code MY_SPLITTER.split("foo, ,bar, quux,")} returns an iterable
063     * containing just {@code ["foo", "bar", "quux"]}. Note that the order in which
064     * the configuration methods are called is never significant; for instance,
065     * trimming is always applied first before checking for an empty result,
066     * regardless of the order in which the {@link #trimResults()} and
067     * {@link #omitEmptyStrings()} methods were invoked.
068     *
069     * <p><b>Warning: splitter instances are always immutable</b>; a configuration
070     * method such as {@code omitEmptyStrings} has no effect on the instance it
071     * is invoked on! You must store and use the new splitter instance returned by
072     * the method. This makes splitters thread-safe, and safe to store as {@code
073     * static final} constants (as illustrated above). <pre>   {@code
074     *
075     *   // Bad! Do not do this!
076     *   Splitter splitter = Splitter.on('/');
077     *   splitter.trimResults(); // does nothing!
078     *   return splitter.split("wrong / wrong / wrong");}</pre>
079     *
080     * The separator recognized by the splitter does not have to be a single
081     * literal character as in the examples above. See the methods {@link
082     * #on(String)}, {@link #on(Pattern)} and {@link #on(CharMatcher)} for examples
083     * of other ways to specify separators.
084     *
085     * <p><b>Note:</b> this class does not mimic any of the quirky behaviors of
086     * similar JDK methods; for instance, it does not silently discard trailing
087     * separators, as does {@link String#split(String)}, nor does it have a default
088     * behavior of using five particular whitespace characters as separators, like
089     * {@link java.util.StringTokenizer}.
090     *
091     * <p>See the Guava User Guide article on <a href=
092     * "http://code.google.com/p/guava-libraries/wiki/StringsExplained#Splitter">
093     * {@code Splitter}</a>.
094     *
095     * @author Julien Silland
096     * @author Jesse Wilson
097     * @author Kevin Bourrillion
098     * @author Louis Wasserman
099     * @since 1.0
100     */
101    @GwtCompatible(emulated = true)
102    public final class Splitter {
103      private final CharMatcher trimmer;
104      private final boolean omitEmptyStrings;
105      private final Strategy strategy;
106      private final int limit;
107    
108      private Splitter(Strategy strategy) {
109        this(strategy, false, CharMatcher.NONE, Integer.MAX_VALUE);
110      }
111    
112      private Splitter(Strategy strategy, boolean omitEmptyStrings,
113          CharMatcher trimmer, int limit) {
114        this.strategy = strategy;
115        this.omitEmptyStrings = omitEmptyStrings;
116        this.trimmer = trimmer;
117        this.limit = limit;
118      }
119    
120      /**
121       * Returns a splitter that uses the given single-character separator. For
122       * example, {@code Splitter.on(',').split("foo,,bar")} returns an iterable
123       * containing {@code ["foo", "", "bar"]}.
124       *
125       * @param separator the character to recognize as a separator
126       * @return a splitter, with default settings, that recognizes that separator
127       */
128      public static Splitter on(char separator) {
129        return on(CharMatcher.is(separator));
130      }
131    
132      /**
133       * Returns a splitter that considers any single character matched by the
134       * given {@code CharMatcher} to be a separator. For example, {@code
135       * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an
136       * iterable containing {@code ["foo", "", "bar", "quux"]}.
137       *
138       * @param separatorMatcher a {@link CharMatcher} that determines whether a
139       *     character is a separator
140       * @return a splitter, with default settings, that uses this matcher
141       */
142      public static Splitter on(final CharMatcher separatorMatcher) {
143        checkNotNull(separatorMatcher);
144    
145        return new Splitter(new Strategy() {
146          @Override public SplittingIterator iterator(
147              Splitter splitter, final CharSequence toSplit) {
148            return new SplittingIterator(splitter, toSplit) {
149              @Override int separatorStart(int start) {
150                return separatorMatcher.indexIn(toSplit, start);
151              }
152    
153              @Override int separatorEnd(int separatorPosition) {
154                return separatorPosition + 1;
155              }
156            };
157          }
158        });
159      }
160    
161      /**
162       * Returns a splitter that uses the given fixed string as a separator. For
163       * example, {@code Splitter.on(", ").split("foo, bar, baz,qux")} returns an
164       * iterable containing {@code ["foo", "bar", "baz,qux"]}.
165       *
166       * @param separator the literal, nonempty string to recognize as a separator
167       * @return a splitter, with default settings, that recognizes that separator
168       */
169      public static Splitter on(final String separator) {
170        checkArgument(separator.length() != 0,
171            "The separator may not be the empty string.");
172    
173        return new Splitter(new Strategy() {
174          @Override public SplittingIterator iterator(
175              Splitter splitter, CharSequence toSplit) {
176            return new SplittingIterator(splitter, toSplit) {
177              @Override public int separatorStart(int start) {
178                int delimeterLength = separator.length();
179    
180                positions:
181                for (int p = start, last = toSplit.length() - delimeterLength;
182                    p <= last; p++) {
183                  for (int i = 0; i < delimeterLength; i++) {
184                    if (toSplit.charAt(i + p) != separator.charAt(i)) {
185                      continue positions;
186                    }
187                  }
188                  return p;
189                }
190                return -1;
191              }
192    
193              @Override public int separatorEnd(int separatorPosition) {
194                return separatorPosition + separator.length();
195              }
196            };
197          }
198        });
199      }
200    
201      /**
202       * Returns a splitter that considers any subsequence matching {@code
203       * pattern} to be a separator. For example, {@code
204       * Splitter.on(Pattern.compile("\r?\n")).split(entireFile)} splits a string
205       * into lines whether it uses DOS-style or UNIX-style line terminators.
206       *
207       * @param separatorPattern the pattern that determines whether a subsequence
208       *     is a separator. This pattern may not match the empty string.
209       * @return a splitter, with default settings, that uses this pattern
210       * @throws IllegalArgumentException if {@code separatorPattern} matches the
211       *     empty string
212       */
213      @GwtIncompatible("java.util.regex")
214      public static Splitter on(final Pattern separatorPattern) {
215        checkNotNull(separatorPattern);
216        checkArgument(!separatorPattern.matcher("").matches(),
217            "The pattern may not match the empty string: %s", separatorPattern);
218    
219        return new Splitter(new Strategy() {
220          @Override public SplittingIterator iterator(
221              final Splitter splitter, CharSequence toSplit) {
222            final Matcher matcher = separatorPattern.matcher(toSplit);
223            return new SplittingIterator(splitter, toSplit) {
224              @Override public int separatorStart(int start) {
225                return matcher.find(start) ? matcher.start() : -1;
226              }
227    
228              @Override public int separatorEnd(int separatorPosition) {
229                return matcher.end();
230              }
231            };
232          }
233        });
234      }
235    
236      /**
237       * Returns a splitter that considers any subsequence matching a given
238       * pattern (regular expression) to be a separator. For example, {@code
239       * Splitter.onPattern("\r?\n").split(entireFile)} splits a string into lines
240       * whether it uses DOS-style or UNIX-style line terminators. This is
241       * equivalent to {@code Splitter.on(Pattern.compile(pattern))}.
242       *
243       * @param separatorPattern the pattern that determines whether a subsequence
244       *     is a separator. This pattern may not match the empty string.
245       * @return a splitter, with default settings, that uses this pattern
246       * @throws java.util.regex.PatternSyntaxException if {@code separatorPattern}
247       *     is a malformed expression
248       * @throws IllegalArgumentException if {@code separatorPattern} matches the
249       *     empty string
250       */
251      @GwtIncompatible("java.util.regex")
252      public static Splitter onPattern(String separatorPattern) {
253        return on(Pattern.compile(separatorPattern));
254      }
255    
256      /**
257       * Returns a splitter that divides strings into pieces of the given length.
258       * For example, {@code Splitter.fixedLength(2).split("abcde")} returns an
259       * iterable containing {@code ["ab", "cd", "e"]}. The last piece can be
260       * smaller than {@code length} but will never be empty.
261       *
262       * @param length the desired length of pieces after splitting
263       * @return a splitter, with default settings, that can split into fixed sized
264       *     pieces
265       */
266      public static Splitter fixedLength(final int length) {
267        checkArgument(length > 0, "The length may not be less than 1");
268    
269        return new Splitter(new Strategy() {
270          @Override public SplittingIterator iterator(
271              final Splitter splitter, CharSequence toSplit) {
272            return new SplittingIterator(splitter, toSplit) {
273              @Override public int separatorStart(int start) {
274                int nextChunkStart = start + length;
275                return (nextChunkStart < toSplit.length() ? nextChunkStart : -1);
276              }
277    
278              @Override public int separatorEnd(int separatorPosition) {
279                return separatorPosition;
280              }
281            };
282          }
283        });
284      }
285    
286      /**
287       * Returns a splitter that behaves equivalently to {@code this} splitter, but
288       * automatically omits empty strings from the results. For example, {@code
289       * Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,")} returns an
290       * iterable containing only {@code ["a", "b", "c"]}.
291       *
292       * <p>If either {@code trimResults} option is also specified when creating a
293       * splitter, that splitter always trims results first before checking for
294       * emptiness. So, for example, {@code
295       * Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ")} returns
296       * an empty iterable.
297       *
298       * <p>Note that it is ordinarily not possible for {@link #split(CharSequence)}
299       * to return an empty iterable, but when using this option, it can (if the
300       * input sequence consists of nothing but separators).
301       *
302       * @return a splitter with the desired configuration
303       */
304      @CheckReturnValue
305      public Splitter omitEmptyStrings() {
306        return new Splitter(strategy, true, trimmer, limit);
307      }
308    
309      /**
310       * Returns a splitter that behaves equivalently to {@code this} splitter but
311       * stops splitting after it reaches the limit.
312       * The limit defines the maximum number of items returned by the iterator.
313       *
314       * <p>For example,
315       * {@code Splitter.on(',').limit(3).split("a,b,c,d")} returns an iterable
316       * containing {@code ["a", "b", "c,d"]}.  When omitting empty strings, the
317       * omitted strings do no count.  Hence,
318       * {@code Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d")}
319       * returns an iterable containing {@code ["a", "b", "c,d"}.
320       * When trim is requested, all entries, including the last are trimmed.  Hence
321       * {@code Splitter.on(',').limit(3).trimResults().split(" a , b , c , d ")}
322       * results in @{code ["a", "b", "c , d"]}.
323       *
324       * @param limit the maximum number of items returns
325       * @return a splitter with the desired configuration
326       * @since 9.0
327       */
328      @CheckReturnValue
329      public Splitter limit(int limit) {
330        checkArgument(limit > 0, "must be greater than zero: %s", limit);
331        return new Splitter(strategy, omitEmptyStrings, trimmer, limit);
332      }
333    
334      /**
335       * Returns a splitter that behaves equivalently to {@code this} splitter, but
336       * automatically removes leading and trailing {@linkplain
337       * CharMatcher#WHITESPACE whitespace} from each returned substring; equivalent
338       * to {@code trimResults(CharMatcher.WHITESPACE)}. For example, {@code
339       * Splitter.on(',').trimResults().split(" a, b ,c ")} returns an iterable
340       * containing {@code ["a", "b", "c"]}.
341       *
342       * @return a splitter with the desired configuration
343       */
344      @CheckReturnValue
345      public Splitter trimResults() {
346        return trimResults(CharMatcher.WHITESPACE);
347      }
348    
349      /**
350       * Returns a splitter that behaves equivalently to {@code this} splitter, but
351       * removes all leading or trailing characters matching the given {@code
352       * CharMatcher} from each returned substring. For example, {@code
353       * Splitter.on(',').trimResults(CharMatcher.is('_')).split("_a ,_b_ ,c__")}
354       * returns an iterable containing {@code ["a ", "b_ ", "c"]}.
355       *
356       * @param trimmer a {@link CharMatcher} that determines whether a character
357       *     should be removed from the beginning/end of a subsequence
358       * @return a splitter with the desired configuration
359       */
360      // TODO(kevinb): throw if a trimmer was already specified!
361      @CheckReturnValue
362      public Splitter trimResults(CharMatcher trimmer) {
363        checkNotNull(trimmer);
364        return new Splitter(strategy, omitEmptyStrings, trimmer, limit);
365      }
366    
367      /**
368       * Splits {@code sequence} into string components and makes them available
369       * through an {@link Iterator}, which may be lazily evaluated.
370       *
371       * @param sequence the sequence of characters to split
372       * @return an iteration over the segments split from the parameter.
373       */
374      public Iterable<String> split(final CharSequence sequence) {
375        checkNotNull(sequence);
376    
377        return new Iterable<String>() {
378          @Override public Iterator<String> iterator() {
379            return spliterator(sequence);
380          }
381        };
382      }
383    
384      private Iterator<String> spliterator(CharSequence sequence) {
385        return strategy.iterator(this, sequence);
386      }
387    
388      /**
389       * Returns a {@code MapSplitter} which splits entries based on this splitter,
390       * and splits entries into keys and values using the specified separator.
391       *
392       * @since 10.0
393       */
394      @CheckReturnValue
395      @Beta
396      public MapSplitter withKeyValueSeparator(String separator) {
397        return withKeyValueSeparator(on(separator));
398      }
399    
400      /**
401       * Returns a {@code MapSplitter} which splits entries based on this splitter,
402       * and splits entries into keys and values using the specified key-value
403       * splitter.
404       *
405       * @since 10.0
406       */
407      @CheckReturnValue
408      @Beta
409      public MapSplitter withKeyValueSeparator(Splitter keyValueSplitter) {
410        return new MapSplitter(this, keyValueSplitter);
411      }
412    
413      /**
414       * An object that splits strings into maps as {@code Splitter} splits
415       * iterables and lists. Like {@code Splitter}, it is thread-safe and
416       * immutable.
417       *
418       * @since 10.0
419       */
420      @Beta
421      public static final class MapSplitter {
422        private static final String INVALID_ENTRY_MESSAGE =
423            "Chunk [%s] is not a valid entry";
424        private final Splitter outerSplitter;
425        private final Splitter entrySplitter;
426    
427        private MapSplitter(Splitter outerSplitter, Splitter entrySplitter) {
428          this.outerSplitter = outerSplitter; // only "this" is passed
429          this.entrySplitter = checkNotNull(entrySplitter);
430        }
431    
432        /**
433         * Splits {@code sequence} into substrings, splits each substring into
434         * an entry, and returns an unmodifiable map with each of the entries. For
435         * example, <code>
436         * Splitter.on(';').trimResults().withKeyValueSeparator("=>")
437         * .split("a=>b ; c=>b")
438         * </code> will return a mapping from {@code "a"} to {@code "b"} and
439         * {@code "c"} to {@code b}.
440         *
441         * <p>The returned map preserves the order of the entries from
442         * {@code sequence}.
443         *
444         * @throws IllegalArgumentException if the specified sequence does not split
445         *         into valid map entries, or if there are duplicate keys
446         */
447        public Map<String, String> split(CharSequence sequence) {
448          Map<String, String> map = new LinkedHashMap<String, String>();
449          for (String entry : outerSplitter.split(sequence)) {
450            Iterator<String> entryFields = entrySplitter.spliterator(entry);
451    
452            checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
453            String key = entryFields.next();
454            checkArgument(!map.containsKey(key), "Duplicate key [%s] found.", key);
455    
456            checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
457            String value = entryFields.next();
458            map.put(key, value);
459    
460            checkArgument(!entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
461          }
462          return Collections.unmodifiableMap(map);
463        }
464      }
465    
466      private interface Strategy {
467        Iterator<String> iterator(Splitter splitter, CharSequence toSplit);
468      }
469    
470      private abstract static class SplittingIterator
471          extends AbstractIterator<String> {
472        final CharSequence toSplit;
473        final CharMatcher trimmer;
474        final boolean omitEmptyStrings;
475    
476        /**
477         * Returns the first index in {@code toSplit} at or after {@code start}
478         * that contains the separator.
479         */
480        abstract int separatorStart(int start);
481    
482        /**
483         * Returns the first index in {@code toSplit} after {@code
484         * separatorPosition} that does not contain a separator. This method is only
485         * invoked after a call to {@code separatorStart}.
486         */
487        abstract int separatorEnd(int separatorPosition);
488    
489        int offset = 0;
490        int limit;
491    
492        protected SplittingIterator(Splitter splitter, CharSequence toSplit) {
493          this.trimmer = splitter.trimmer;
494          this.omitEmptyStrings = splitter.omitEmptyStrings;
495          this.limit = splitter.limit;
496          this.toSplit = toSplit;
497        }
498    
499        @Override protected String computeNext() {
500          /*
501           * The returned string will be from the end of the last match to the
502           * beginning of the next one. nextStart is the start position of the
503           * returned substring, while offset is the place to start looking for a
504           * separator.
505           */
506          int nextStart = offset;
507          while (offset != -1) {
508            int start = nextStart;
509            int end;
510    
511            int separatorPosition = separatorStart(offset);
512            if (separatorPosition == -1) {
513              end = toSplit.length();
514              offset = -1;
515            } else {
516              end = separatorPosition;
517              offset = separatorEnd(separatorPosition);
518            }
519            if (offset == nextStart) {
520              /*
521               * This occurs when some pattern has an empty match, even if it
522               * doesn't match the empty string -- for example, if it requires
523               * lookahead or the like. The offset must be increased to look for
524               * separators beyond this point, without changing the start position
525               * of the next returned substring -- so nextStart stays the same.
526               */
527              offset++;
528              if (offset >= toSplit.length()) {
529                offset = -1;
530              }
531              continue;
532            }
533    
534            while (start < end && trimmer.matches(toSplit.charAt(start))) {
535              start++;
536            }
537            while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
538              end--;
539            }
540    
541            if (omitEmptyStrings && start == end) {
542              // Don't include the (unused) separator in next split string.
543              nextStart = offset;
544              continue;
545            }
546    
547            if (limit == 1) {
548              // The limit has been reached, return the rest of the string as the
549              // final item.  This is tested after empty string removal so that
550              // empty strings do not count towards the limit.
551              end = toSplit.length();
552              offset = -1;
553              // Since we may have changed the end, we need to trim it again.
554              while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
555                end--;
556              }
557            } else {
558              limit--;
559            }
560    
561            return toSplit.subSequence(start, end).toString();
562          }
563          return endOfData();
564        }
565      }
566    }