001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.util.Objects.requireNonNull;
019
020import com.google.common.annotations.GwtCompatible;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.io.IOException;
023import java.util.AbstractList;
024import java.util.Arrays;
025import java.util.Iterator;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029import org.jspecify.annotations.Nullable;
030
031/**
032 * An object which joins pieces of text (specified as an array, {@link Iterable}, varargs or even a
033 * {@link Map}) with a separator. It either appends the results to an {@link Appendable} or returns
034 * them as a {@link String}. Example:
035 *
036 * <pre>{@code
037 * Joiner joiner = Joiner.on("; ").skipNulls();
038 *  . . .
039 * return joiner.join("Harry", null, "Ron", "Hermione");
040 * }</pre>
041 *
042 * <p>This returns the string {@code "Harry; Ron; Hermione"}. Note that all input elements are
043 * converted to strings using {@link Object#toString()} before being appended.
044 *
045 * <p>If neither {@link #skipNulls()} nor {@link #useForNull(String)} is specified, the joining
046 * methods will throw {@link NullPointerException} if any given element is null.
047 *
048 * <p><b>Warning: joiner instances are always immutable</b>; a configuration method such as {@code
049 * useForNull} has no effect on the instance it is invoked on! You must store and use the new joiner
050 * instance returned by the method. This makes joiners thread-safe, and safe to store as {@code
051 * static final} constants.
052 *
053 * <pre>{@code
054 * // Bad! Do not do this!
055 * Joiner joiner = Joiner.on(',');
056 * joiner.skipNulls(); // does nothing!
057 * return joiner.join("wrong", null, "wrong");
058 * }</pre>
059 *
060 * <p>See the Guava User Guide article on <a
061 * href="https://github.com/google/guava/wiki/StringsExplained#joiner">{@code Joiner}</a>.
062 *
063 * @author Kevin Bourrillion
064 * @since 2.0
065 */
066@GwtCompatible
067public class Joiner {
068  /** Returns a joiner which automatically places {@code separator} between consecutive elements. */
069  public static Joiner on(String separator) {
070    return new Joiner(separator);
071  }
072
073  /** Returns a joiner which automatically places {@code separator} between consecutive elements. */
074  public static Joiner on(char separator) {
075    return new Joiner(String.valueOf(separator));
076  }
077
078  private final String separator;
079
080  private Joiner(String separator) {
081    this.separator = checkNotNull(separator);
082  }
083
084  private Joiner(Joiner prototype) {
085    this.separator = prototype.separator;
086  }
087
088  /**
089   * Appends the string representation of each of {@code parts}, using the previously configured
090   * separator between each, to {@code appendable}.
091   */
092  @CanIgnoreReturnValue
093  public <A extends Appendable> A appendTo(A appendable, Iterable<?> parts) throws IOException {
094    return appendTo(appendable, parts.iterator());
095  }
096
097  /**
098   * Appends the string representation of each of {@code parts}, using the previously configured
099   * separator between each, to {@code appendable}.
100   *
101   * @since 11.0
102   */
103  @CanIgnoreReturnValue
104  public <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) throws IOException {
105    checkNotNull(appendable);
106    if (parts.hasNext()) {
107      appendable.append(toString(parts.next()));
108      while (parts.hasNext()) {
109        appendable.append(separator);
110        appendable.append(toString(parts.next()));
111      }
112    }
113    return appendable;
114  }
115
116  /**
117   * Appends the string representation of each of {@code parts}, using the previously configured
118   * separator between each, to {@code appendable}.
119   */
120  @CanIgnoreReturnValue
121  public final <A extends Appendable> A appendTo(A appendable, @Nullable Object[] parts)
122      throws IOException {
123    @SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
124    List<?> partsList = Arrays.<@Nullable Object>asList(parts);
125    return appendTo(appendable, partsList);
126  }
127
128  /** Appends to {@code appendable} the string representation of each of the remaining arguments. */
129  @CanIgnoreReturnValue
130  public final <A extends Appendable> A appendTo(
131      A appendable, @Nullable Object first, @Nullable Object second, @Nullable Object... rest)
132      throws IOException {
133    return appendTo(appendable, iterable(first, second, rest));
134  }
135
136  /**
137   * Appends the string representation of each of {@code parts}, using the previously configured
138   * separator between each, to {@code builder}. Identical to {@link #appendTo(Appendable,
139   * Iterable)}, except that it does not throw {@link IOException}.
140   */
141  @CanIgnoreReturnValue
142  public final StringBuilder appendTo(StringBuilder builder, Iterable<?> parts) {
143    return appendTo(builder, parts.iterator());
144  }
145
146  /**
147   * Appends the string representation of each of {@code parts}, using the previously configured
148   * separator between each, to {@code builder}. Identical to {@link #appendTo(Appendable,
149   * Iterable)}, except that it does not throw {@link IOException}.
150   *
151   * @since 11.0
152   */
153  @CanIgnoreReturnValue
154  public final StringBuilder appendTo(StringBuilder builder, Iterator<?> parts) {
155    try {
156      appendTo((Appendable) builder, parts);
157    } catch (IOException impossible) {
158      throw new AssertionError(impossible);
159    }
160    return builder;
161  }
162
163  /**
164   * Appends the string representation of each of {@code parts}, using the previously configured
165   * separator between each, to {@code builder}. Identical to {@link #appendTo(Appendable,
166   * Iterable)}, except that it does not throw {@link IOException}.
167   */
168  @CanIgnoreReturnValue
169  public final StringBuilder appendTo(StringBuilder builder, @Nullable Object[] parts) {
170    @SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
171    List<?> partsList = Arrays.<@Nullable Object>asList(parts);
172    return appendTo(builder, partsList);
173  }
174
175  /**
176   * Appends to {@code builder} the string representation of each of the remaining arguments.
177   * Identical to {@link #appendTo(Appendable, Object, Object, Object...)}, except that it does not
178   * throw {@link IOException}.
179   */
180  @CanIgnoreReturnValue
181  public final StringBuilder appendTo(
182      StringBuilder builder,
183      @Nullable Object first,
184      @Nullable Object second,
185      @Nullable Object... rest) {
186    return appendTo(builder, iterable(first, second, rest));
187  }
188
189  /**
190   * Returns a string containing the string representation of each of {@code parts}, using the
191   * previously configured separator between each.
192   */
193  public String join(Iterable<?> parts) {
194    /*
195     * If we can quickly determine how many elements there are likely to be, then we can use the
196     * fastest possible implementation, which delegates to the array overload of String.join.
197     *
198     * In theory, we can quickly determine the size of any Collection. However, thanks to
199     * regrettable implementations like our own Sets.filter, Collection.size() is sometimes a
200     * linear-time operation, and it can even have side effects. Thus, we limit the special case to
201     * List, which is _even more likely_ to have size() implemented to be fast and side-effect-free.
202     *
203     * We could consider recognizing specific other collections as safe (like ImmutableCollection,
204     * except ContiguousSet!) or as not worth this optimization (CopyOnWriteArrayList?).
205     */
206    if (parts instanceof List) {
207      int size = ((List<?>) parts).size();
208      if (size == 0) {
209        return "";
210      }
211      CharSequence[] toJoin = new CharSequence[size];
212      int i = 0;
213      for (Object part : parts) {
214        if (i == toJoin.length) {
215          /*
216           * We first initialized toJoin to the size of the input collection. However, that size can
217           * go out of date (for a collection like CopyOnWriteArrayList, which may have been safely
218           * modified concurrently), or it might have been only an estimate to begin with (for a
219           * collection like ConcurrentHashMap, which sums up several counters that may not be in
220           * sync with one another). We accommodate that by resizing as necessary.
221           */
222          toJoin = Arrays.copyOf(toJoin, expandedCapacity(toJoin.length, toJoin.length + 1));
223        }
224        toJoin[i++] = toString(part);
225      }
226      // We might not have seen the expected number of elements, as discussed above.
227      if (i != toJoin.length) {
228        toJoin = Arrays.copyOf(toJoin, i);
229      }
230      // What we care about is Android, under which this method is always desugared:
231      // https://r8.googlesource.com/r8/+/05ba76883518bff06496d6d7df5f06b94a88fb00/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java#831
232      @SuppressWarnings("Java7ApiChecker")
233      String result = String.join(separator, toJoin);
234      return result;
235    }
236    return join(parts.iterator());
237  }
238
239  /*
240   * TODO: b/381289911 - Make the Iterator overload use StringJoiner (including Android or not)—or
241   * some other optimization, given that StringJoiner can over-allocate:
242   * https://bugs.openjdk.org/browse/JDK-8305774
243   */
244
245  // TODO: b/381289911 - Optimize MapJoiner similarly to Joiner (including Android or not).
246
247  /**
248   * Returns a string containing the string representation of each of {@code parts}, using the
249   * previously configured separator between each.
250   *
251   * @since 11.0
252   */
253  public final String join(Iterator<?> parts) {
254    return appendTo(new StringBuilder(), parts).toString();
255  }
256
257  /**
258   * Returns a string containing the string representation of each of {@code parts}, using the
259   * previously configured separator between each.
260   */
261  public final String join(@Nullable Object[] parts) {
262    @SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
263    List<?> partsList = Arrays.<@Nullable Object>asList(parts);
264    return join(partsList);
265  }
266
267  /**
268   * Returns a string containing the string representation of each argument, using the previously
269   * configured separator between each.
270   */
271  public final String join(
272      @Nullable Object first, @Nullable Object second, @Nullable Object... rest) {
273    return join(iterable(first, second, rest));
274  }
275
276  /**
277   * Returns a joiner with the same behavior as this one, except automatically substituting {@code
278   * nullText} for any provided null elements.
279   */
280  public Joiner useForNull(String nullText) {
281    checkNotNull(nullText);
282    return new Joiner(this) {
283      @Override
284      CharSequence toString(@Nullable Object part) {
285        return (part == null) ? nullText : Joiner.this.toString(part);
286      }
287
288      @Override
289      public Joiner useForNull(String nullText) {
290        throw new UnsupportedOperationException("already specified useForNull");
291      }
292
293      @Override
294      public Joiner skipNulls() {
295        throw new UnsupportedOperationException("already specified useForNull");
296      }
297    };
298  }
299
300  /**
301   * Returns a joiner with the same behavior as this joiner, except automatically skipping over any
302   * provided null elements.
303   */
304  public Joiner skipNulls() {
305    return new Joiner(this) {
306      @Override
307      @SuppressWarnings("JoinIterableIterator") // suggests infinite recursion
308      public String join(Iterable<? extends @Nullable Object> parts) {
309        return join(parts.iterator());
310      }
311
312      @Override
313      public <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) throws IOException {
314        checkNotNull(appendable, "appendable");
315        checkNotNull(parts, "parts");
316        while (parts.hasNext()) {
317          Object part = parts.next();
318          if (part != null) {
319            appendable.append(Joiner.this.toString(part));
320            break;
321          }
322        }
323        while (parts.hasNext()) {
324          Object part = parts.next();
325          if (part != null) {
326            appendable.append(separator);
327            appendable.append(Joiner.this.toString(part));
328          }
329        }
330        return appendable;
331      }
332
333      @Override
334      public Joiner useForNull(String nullText) {
335        throw new UnsupportedOperationException("already specified skipNulls");
336      }
337
338      @Override
339      public MapJoiner withKeyValueSeparator(String kvs) {
340        throw new UnsupportedOperationException("can't use .skipNulls() with maps");
341      }
342    };
343  }
344
345  /**
346   * Returns a {@code MapJoiner} using the given key-value separator, and the same configuration as
347   * this {@code Joiner} otherwise.
348   *
349   * @since 20.0
350   */
351  public MapJoiner withKeyValueSeparator(char keyValueSeparator) {
352    return withKeyValueSeparator(String.valueOf(keyValueSeparator));
353  }
354
355  /**
356   * Returns a {@code MapJoiner} using the given key-value separator, and the same configuration as
357   * this {@code Joiner} otherwise.
358   */
359  public MapJoiner withKeyValueSeparator(String keyValueSeparator) {
360    return new MapJoiner(this, keyValueSeparator);
361  }
362
363  /**
364   * An object that joins map entries in the same manner as {@code Joiner} joins iterables and
365   * arrays. Like {@code Joiner}, it is thread-safe and immutable.
366   *
367   * <p>In addition to operating on {@code Map} instances, {@code MapJoiner} can operate on {@code
368   * Multimap} entries in two distinct modes:
369   *
370   * <ul>
371   *   <li>To output a separate entry for each key-value pair, pass {@code multimap.entries()} to a
372   *       {@code MapJoiner} method that accepts entries as input, and receive output of the form
373   *       {@code key1=A&key1=B&key2=C}.
374   *   <li>To output a single entry for each key, pass {@code multimap.asMap()} to a {@code
375   *       MapJoiner} method that accepts a map as input, and receive output of the form {@code
376   *       key1=[A, B]&key2=C}.
377   * </ul>
378   *
379   * @since 2.0
380   */
381  public static final class MapJoiner {
382    private final Joiner joiner;
383    private final String keyValueSeparator;
384
385    private MapJoiner(Joiner joiner, String keyValueSeparator) {
386      this.joiner = joiner; // only "this" is ever passed, so don't checkNotNull
387      this.keyValueSeparator = checkNotNull(keyValueSeparator);
388    }
389
390    /**
391     * Appends the string representation of each entry of {@code map}, using the previously
392     * configured separator and key-value separator, to {@code appendable}.
393     */
394    @CanIgnoreReturnValue
395    public <A extends Appendable> A appendTo(A appendable, Map<?, ?> map) throws IOException {
396      return appendTo(appendable, map.entrySet());
397    }
398
399    /**
400     * Appends the string representation of each entry of {@code map}, using the previously
401     * configured separator and key-value separator, to {@code builder}. Identical to {@link
402     * #appendTo(Appendable, Map)}, except that it does not throw {@link IOException}.
403     */
404    @CanIgnoreReturnValue
405    public StringBuilder appendTo(StringBuilder builder, Map<?, ?> map) {
406      return appendTo(builder, map.entrySet());
407    }
408
409    /**
410     * Appends the string representation of each entry in {@code entries}, using the previously
411     * configured separator and key-value separator, to {@code appendable}.
412     *
413     * @since 10.0
414     */
415    @CanIgnoreReturnValue
416    public <A extends Appendable> A appendTo(A appendable, Iterable<? extends Entry<?, ?>> entries)
417        throws IOException {
418      return appendTo(appendable, entries.iterator());
419    }
420
421    /**
422     * Appends the string representation of each entry in {@code entries}, using the previously
423     * configured separator and key-value separator, to {@code appendable}.
424     *
425     * @since 11.0
426     */
427    @CanIgnoreReturnValue
428    public <A extends Appendable> A appendTo(A appendable, Iterator<? extends Entry<?, ?>> parts)
429        throws IOException {
430      checkNotNull(appendable);
431      if (parts.hasNext()) {
432        Entry<?, ?> entry = parts.next();
433        appendable.append(joiner.toString(entry.getKey()));
434        appendable.append(keyValueSeparator);
435        appendable.append(joiner.toString(entry.getValue()));
436        while (parts.hasNext()) {
437          appendable.append(joiner.separator);
438          Entry<?, ?> e = parts.next();
439          appendable.append(joiner.toString(e.getKey()));
440          appendable.append(keyValueSeparator);
441          appendable.append(joiner.toString(e.getValue()));
442        }
443      }
444      return appendable;
445    }
446
447    /**
448     * Appends the string representation of each entry in {@code entries}, using the previously
449     * configured separator and key-value separator, to {@code builder}. Identical to {@link
450     * #appendTo(Appendable, Iterable)}, except that it does not throw {@link IOException}.
451     *
452     * @since 10.0
453     */
454    @CanIgnoreReturnValue
455    public StringBuilder appendTo(StringBuilder builder, Iterable<? extends Entry<?, ?>> entries) {
456      return appendTo(builder, entries.iterator());
457    }
458
459    /**
460     * Appends the string representation of each entry in {@code entries}, using the previously
461     * configured separator and key-value separator, to {@code builder}. Identical to {@link
462     * #appendTo(Appendable, Iterable)}, except that it does not throw {@link IOException}.
463     *
464     * @since 11.0
465     */
466    @CanIgnoreReturnValue
467    public StringBuilder appendTo(StringBuilder builder, Iterator<? extends Entry<?, ?>> entries) {
468      try {
469        appendTo((Appendable) builder, entries);
470      } catch (IOException impossible) {
471        throw new AssertionError(impossible);
472      }
473      return builder;
474    }
475
476    /**
477     * Returns a string containing the string representation of each entry of {@code map}, using the
478     * previously configured separator and key-value separator.
479     */
480    public String join(Map<?, ?> map) {
481      return join(map.entrySet());
482    }
483
484    /**
485     * Returns a string containing the string representation of each entry in {@code entries}, using
486     * the previously configured separator and key-value separator.
487     *
488     * @since 10.0
489     */
490    public String join(Iterable<? extends Entry<?, ?>> entries) {
491      return join(entries.iterator());
492    }
493
494    /**
495     * Returns a string containing the string representation of each entry in {@code entries}, using
496     * the previously configured separator and key-value separator.
497     *
498     * @since 11.0
499     */
500    public String join(Iterator<? extends Entry<?, ?>> entries) {
501      return appendTo(new StringBuilder(), entries).toString();
502    }
503
504    /**
505     * Returns a map joiner with the same behavior as this one, except automatically substituting
506     * {@code nullText} for any provided null keys or values.
507     */
508    public MapJoiner useForNull(String nullText) {
509      return new MapJoiner(joiner.useForNull(nullText), keyValueSeparator);
510    }
511  }
512
513  // TODO(cpovirk): Rename to "toCharSequence."
514  CharSequence toString(@Nullable Object part) {
515    /*
516     * requireNonNull is not safe: Joiner.on(...).join(somethingThatContainsNull) will indeed throw.
517     * However, Joiner.on(...).useForNull(...).join(somethingThatContainsNull) *is* safe -- because
518     * it returns a subclass of Joiner that overrides this method to tolerate null inputs.
519     *
520     * Unfortunately, we don't distinguish between these two cases in our public API: Joiner.on(...)
521     * and Joiner.on(...).useForNull(...) both declare the same return type: plain Joiner. To ensure
522     * that users *can* pass null arguments to Joiner, we annotate it as if it always tolerates null
523     * inputs, rather than as if it never tolerates them.
524     *
525     * We rely on checkers to implement special cases to catch dangerous calls to join(), etc. based
526     * on what they know about the particular Joiner instances the calls are performed on.
527     *
528     * (In addition to useForNull, we also offer skipNulls. It, too, tolerates null inputs, but its
529     * tolerance is implemented differently: Its implementation avoids calling this toString(Object)
530     * method in the first place.)
531     */
532    requireNonNull(part);
533    return (part instanceof CharSequence) ? (CharSequence) part : part.toString();
534  }
535
536  private static Iterable<@Nullable Object> iterable(
537      @Nullable Object first, @Nullable Object second, @Nullable Object[] rest) {
538    checkNotNull(rest);
539    return new AbstractList<@Nullable Object>() {
540      @Override
541      public int size() {
542        return rest.length + 2;
543      }
544
545      @Override
546      public @Nullable Object get(int index) {
547        switch (index) {
548          case 0:
549            return first;
550          case 1:
551            return second;
552          default:
553            return rest[index - 2];
554        }
555      }
556    };
557  }
558
559  // cloned from ImmutableCollection
560  private static int expandedCapacity(int oldCapacity, int minCapacity) {
561    if (minCapacity < 0) {
562      throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements");
563    } else if (minCapacity <= oldCapacity) {
564      return oldCapacity;
565    }
566    // careful of overflow!
567    int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
568    if (newCapacity < minCapacity) {
569      newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
570    }
571    if (newCapacity < 0) {
572      newCapacity = Integer.MAX_VALUE;
573      // guaranteed to be >= newCapacity
574    }
575    return newCapacity;
576  }
577}