jdk/src/share/classes/java/util/StringJoiner.java
author psandoz
Mon, 03 Jun 2013 17:37:51 +0200
changeset 17934 4045b37aef13
parent 17333 3cfbb50b9fb7
child 18778 7214a903b084
permissions -rw-r--r--
8014383: StringJoiner example in class description not in sync with streams API Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     1
/*
17333
3cfbb50b9fb7 8014289: JDK8 b89 source with GPL header errors
katleman
parents: 17181
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     4
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    10
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    15
 * accompanied this code).
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    16
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    20
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    23
 * questions.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    24
 */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    25
package java.util;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    26
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    27
/**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    28
 * {@code StringJoiner} is used to construct a sequence of characters separated
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    29
 * by a delimiter and optionally starting with a supplied prefix
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    30
 * and ending with a supplied suffix.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    31
 * <p>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    32
 * Prior to adding something to the {@code StringJoiner}, its
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    33
 * {@code sj.toString()} method will, by default, return {@code prefix + suffix}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    34
 * However, if the {@code setEmptyValue} method is called, the {@code emptyValue}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    35
 * supplied will be returned instead. This can be used, for example, when
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    36
 * creating a string using set notation to indicate an empty set, i.e.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    37
 * <code>"{}"</code>, where the {@code prefix} is <code>"{"</code>, the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    38
 * {@code suffix} is <code>"}"</code> and nothing has been added to the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    39
 * {@code StringJoiner}.
17934
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    40
 *
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    41
 * @apiNote
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    42
 * <p>The String {@code "[George:Sally:Fred]"} may be constructed as follows:
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    43
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    44
 * <pre> {@code
17934
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    45
 * StringJoiner sj = new StringJoiner(":", "[", "]");
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    46
 * sj.add("George").add("Sally").add("Fred");
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    47
 * String desiredString = sj.toString();
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    48
 * }</pre>
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    49
 * <p>
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    50
 * A {@code StringJoiner} may be employed to create formatted output from a
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    51
 * {@link java.util.stream.Stream} using
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    52
 * {@link java.util.stream.Collectors#toStringJoiner}. For example:
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    53
 *
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    54
 * <pre> {@code
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    55
 * List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    56
 * String commaSeparatedNumbers = numbers.stream()
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    57
 *     .map(i -> i.toString())
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    58
 *     .collect(Collectors.toStringJoiner(", ")).toString();
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    59
 * }</pre>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    60
 *
17934
4045b37aef13 8014383: StringJoiner example in class description not in sync with streams API
psandoz
parents: 17333
diff changeset
    61
 * @see java.util.stream.Collectors#toStringJoiner
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    62
 * @since  1.8
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    63
*/
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    64
public final class StringJoiner {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    65
    private final String prefix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    66
    private final String delimiter;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    67
    private final String suffix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    68
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    69
    /*
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    70
     * StringBuilder value -- at any time, the characters constructed from the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    71
     * prefix, the added element separated by the delimiter, but without the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    72
     * suffix, so that we can more easily add elements without having to jigger
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    73
     * the suffix each time.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    74
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    75
    private StringBuilder value;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    76
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    77
    /*
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    78
     * By default, the string consisting of prefix+suffix, returned by
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    79
     * toString(), or properties of value, when no elements have yet been added,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    80
     * i.e. when it is empty.  This may be overridden by the user to be some
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    81
     * other value including the empty String.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    82
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    83
    private String emptyValue;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    84
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    85
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    86
     * Constructs a {@code StringJoiner} with no characters in it, with no
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    87
     * {@code prefix} or {@code suffix}, and a copy of the supplied
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    88
     * {@code delimiter}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    89
     * If no characters are added to the {@code StringJoiner} and methods
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    90
     * accessing the value of it are invoked, it will not return a
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    91
     * {@code prefix} or {@code suffix} (or properties thereof) in the result,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    92
     * unless {@code setEmptyValue} has first been called.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    93
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    94
     * @param  delimiter the sequence of characters to be used between each
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    95
     *         element added to the {@code StringJoiner} value
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    96
     * @throws NullPointerException if {@code delimiter} is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    97
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    98
    public StringJoiner(CharSequence delimiter) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    99
        this(delimiter, "", "");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   100
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   101
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   102
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   103
     * Constructs a {@code StringJoiner} with no characters in it using copies
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   104
     * of the supplied {@code prefix}, {@code delimiter} and {@code suffix}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   105
     * If no characters are added to the {@code StringJoiner} and methods
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   106
     * accessing the string value of it are invoked, it will return the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   107
     * {@code prefix + suffix} (or properties thereof) in the result, unless
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   108
     * {@code setEmptyValue} has first been called.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   109
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   110
     * @param  delimiter the sequence of characters to be used between each
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   111
     *         element added to the {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   112
     * @param  prefix the sequence of characters to be used at the beginning
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   113
     * @param  suffix the sequence of characters to be used at the end
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   114
     * @throws NullPointerException if {@code prefix}, {@code delimiter}, or
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   115
     *         {@code suffix} is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   116
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   117
    public StringJoiner(CharSequence delimiter, CharSequence prefix,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   118
            CharSequence suffix) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   119
        Objects.requireNonNull(prefix, "The prefix must not be null");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   120
        Objects.requireNonNull(delimiter, "The delimiter must not be null");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   121
        Objects.requireNonNull(suffix, "The suffix must not be null");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   122
        // make defensive copies of arguments
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   123
        this.prefix = prefix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   124
        this.delimiter = delimiter.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   125
        this.suffix = suffix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   126
        this.emptyValue = this.prefix + this.suffix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   127
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   128
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   129
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   130
     * Sets the sequence of characters to be used when determining the string
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   131
     * representation of this {@code StringJoiner} and no elements have been
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   132
     * added yet, i.e. when it is empty.  A copy of the {@code emptyValue}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   133
     * parameter is made for this purpose. Note that once an add method has been
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   134
     * called, the {@code StringJoiner} is no longer considered empty, even if
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   135
     * the element(s) added correspond to the empty {@code String}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   136
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   137
     * @param  emptyValue the characters to return as the value of an empty
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   138
     *         {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   139
     * @return this {@code StringJoiner} itself so the calls may be chained
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   140
     * @throws NullPointerException when the {@code emptyValue} parameter is
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   141
     *         {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   142
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   143
    public StringJoiner setEmptyValue(CharSequence emptyValue) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   144
        this.emptyValue = Objects.requireNonNull(emptyValue,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   145
            "The empty value must not be null").toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   146
        return this;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   147
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   148
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   149
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   150
     * Returns the current value, consisting of the {@code prefix}, the values
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   151
     * added so far separated by the {@code delimiter}, and the {@code suffix},
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   152
     * unless no elements have been added in which case, the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   153
     * {@code prefix + suffix} or the {@code emptyValue} characters are returned
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   154
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   155
     * @return the string representation of this {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   156
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   157
    @Override
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   158
    public String toString() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   159
        if (value == null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   160
            return emptyValue;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   161
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   162
            if (suffix.equals("")) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   163
                return value.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   164
            } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   165
                int initialLength = value.length();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   166
                String result = value.append(suffix).toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   167
                // reset value to pre-append initialLength
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   168
                value.setLength(initialLength);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   169
                return result;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   170
            }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   171
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   172
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   173
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   174
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   175
     * Add the a copy of the supplied {@code CharSequence} value as the next
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   176
     * element of the {@code StringJoiner} value. If {@code newElement} is
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   177
     * {@code null}, then {@code "null"} is added.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   178
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   179
     * @param  newElement The element to add
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   180
     * @return a reference to this {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   181
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   182
    public StringJoiner add(CharSequence newElement) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   183
        prepareBuilder().append(newElement);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   184
        return this;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   185
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   186
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   187
    private StringBuilder prepareBuilder() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   188
        if (value != null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   189
            value.append(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   190
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   191
            value = new StringBuilder().append(prefix);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   192
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   193
        return value;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   194
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   195
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   196
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   197
     * The length of the {@code StringJoiner} value, i.e. the length of
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   198
     * {@code String} representation of the {@code StringJoiner}. Note that if
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   199
     * no add methods have been called, then the length of the {@code String}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   200
     * representation (either {@code prefix + suffix} or {@code emptyValue})
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   201
     * will be returned. The value should be equivalent to
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   202
     * {@code toString().length()}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   203
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   204
     * @return the length of the current value of {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   205
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   206
    public int length() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   207
        // Remember that we never actually append the suffix unless we return
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   208
        // the full (present) value or some sub-string or length of it, so that
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   209
        // we can add on more if we need to.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   210
        return (value != null ? value.length() + suffix.length() :
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   211
                emptyValue.length());
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   212
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   213
}