< prev index next >

src/java.base/share/classes/java/util/StringJoiner.java

Print this page




  60  *
  61  * @see java.util.stream.Collectors#joining(CharSequence)
  62  * @see java.util.stream.Collectors#joining(CharSequence, CharSequence, CharSequence)
  63  * @since  1.8
  64 */
  65 public final class StringJoiner {
  66     private final String prefix;
  67     private final String delimiter;
  68     private final String suffix;
  69 
  70     /** Contains all the string components added so far. */
  71     private String[] elts;
  72 
  73     /** The number of string components added so far. */
  74     private int size;
  75 
  76     /** Total length in chars so far, excluding prefix and suffix. */
  77     private int len;
  78 
  79     /**
  80      * When overridden by the user to be non-null via {@link setEmptyValue}, the
  81      * string returned by toString() when no elements have yet been added.
  82      * When null, prefix + suffix is used as the empty value.
  83      */
  84     private String emptyValue;
  85 
  86     /**
  87      * Constructs a {@code StringJoiner} with no characters in it, with no
  88      * {@code prefix} or {@code suffix}, and a copy of the supplied
  89      * {@code delimiter}.
  90      * If no characters are added to the {@code StringJoiner} and methods
  91      * accessing the value of it are invoked, it will not return a
  92      * {@code prefix} or {@code suffix} (or properties thereof) in the result,
  93      * unless {@code setEmptyValue} has first been called.
  94      *
  95      * @param  delimiter the sequence of characters to be used between each
  96      *         element added to the {@code StringJoiner} value
  97      * @throws NullPointerException if {@code delimiter} is {@code null}
  98      */
  99     public StringJoiner(CharSequence delimiter) {
 100         this(delimiter, "", "");




  60  *
  61  * @see java.util.stream.Collectors#joining(CharSequence)
  62  * @see java.util.stream.Collectors#joining(CharSequence, CharSequence, CharSequence)
  63  * @since  1.8
  64 */
  65 public final class StringJoiner {
  66     private final String prefix;
  67     private final String delimiter;
  68     private final String suffix;
  69 
  70     /** Contains all the string components added so far. */
  71     private String[] elts;
  72 
  73     /** The number of string components added so far. */
  74     private int size;
  75 
  76     /** Total length in chars so far, excluding prefix and suffix. */
  77     private int len;
  78 
  79     /**
  80      * When overridden by the user to be non-null via {@link #setEmptyValue(CharSequence)}, the
  81      * string returned by toString() when no elements have yet been added.
  82      * When null, prefix + suffix is used as the empty value.
  83      */
  84     private String emptyValue;
  85 
  86     /**
  87      * Constructs a {@code StringJoiner} with no characters in it, with no
  88      * {@code prefix} or {@code suffix}, and a copy of the supplied
  89      * {@code delimiter}.
  90      * If no characters are added to the {@code StringJoiner} and methods
  91      * accessing the value of it are invoked, it will not return a
  92      * {@code prefix} or {@code suffix} (or properties thereof) in the result,
  93      * unless {@code setEmptyValue} has first been called.
  94      *
  95      * @param  delimiter the sequence of characters to be used between each
  96      *         element added to the {@code StringJoiner} value
  97      * @throws NullPointerException if {@code delimiter} is {@code null}
  98      */
  99     public StringJoiner(CharSequence delimiter) {
 100         this(delimiter, "", "");


< prev index next >