jdk/src/share/classes/java/util/StringJoiner.java
author katleman
Thu, 09 May 2013 15:04:56 -0700
changeset 17333 3cfbb50b9fb7
parent 17181 e3d13a15c5c0
child 17934 4045b37aef13
permissions -rw-r--r--
8014289: JDK8 b89 source with GPL header errors Reviewed-by: mchung, mduigou, tbell, dsamersoff
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
 * For example, the String {@code "[George:Sally:Fred]"} may
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    33
 * be constructed as follows:
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    34
 * <pre> {@code
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    35
 *     StringJoiner sj = new StringJoiner(":", "[", "]");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    36
 *     sj.add("George").add("Sally").add("Fred");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    37
 *     String desiredString = sj.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    38
 * }</pre>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    39
 * <p>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    40
 * 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
    41
 * {@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
    42
 * 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
    43
 * 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
    44
 * 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
    45
 * <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
    46
 * {@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
    47
 * {@code StringJoiner}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    48
 * <p>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    49
 * A {@code StringJoiner} may be employed to create formatted output from a
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    50
 * collection using lambda expressions as shown in the following example.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    51
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    52
 * <pre> {@code
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    53
 *     List<Person> people = ...
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    54
 *     String commaSeparatedNames =
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    55
 *         people.map(p -> p.getName()).into(new StringJoiner(", ")).toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    56
 * }</pre>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    57
 *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    58
 * @author Jim Gish
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    59
 * @since  1.8
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    60
*/
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    61
public final class StringJoiner {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    62
    private final String prefix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    63
    private final String delimiter;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    64
    private final String suffix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    65
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    66
    /*
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    67
     * 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
    68
     * 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
    69
     * 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
    70
     * the suffix each time.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    71
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    72
    private StringBuilder value;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    73
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
     * 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
    76
     * 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
    77
     * 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
    78
     * other value including the empty String.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    79
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    80
    private String emptyValue;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    81
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
     * 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
    84
     * {@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
    85
     * {@code delimiter}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    86
     * 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
    87
     * 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
    88
     * {@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
    89
     * unless {@code setEmptyValue} has first been called.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    90
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    91
     * @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
    92
     *         element added to the {@code StringJoiner} value
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    93
     * @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
    94
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    95
    public StringJoiner(CharSequence delimiter) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    96
        this(delimiter, "", "");
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
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    99
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   100
     * 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
   101
     * 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
   102
     * 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
   103
     * 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
   104
     * {@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
   105
     * {@code setEmptyValue} has first been called.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   106
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   107
     * @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
   108
     *         element added to the {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   109
     * @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
   110
     * @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
   111
     * @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
   112
     *         {@code suffix} is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   113
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   114
    public StringJoiner(CharSequence delimiter, CharSequence prefix,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   115
            CharSequence suffix) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   116
        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
   117
        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
   118
        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
   119
        // make defensive copies of arguments
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   120
        this.prefix = prefix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   121
        this.delimiter = delimiter.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   122
        this.suffix = suffix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   123
        this.emptyValue = this.prefix + this.suffix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   124
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   125
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   126
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   127
     * 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
   128
     * 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
   129
     * 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
   130
     * 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
   131
     * 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
   132
     * 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
   133
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   134
     * @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
   135
     *         {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   136
     * @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
   137
     * @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
   138
     *         {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   139
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   140
    public StringJoiner setEmptyValue(CharSequence emptyValue) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   141
        this.emptyValue = Objects.requireNonNull(emptyValue,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   142
            "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
   143
        return this;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   144
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   145
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   146
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   147
     * 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
   148
     * 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
   149
     * 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
   150
     * {@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
   151
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   152
     * @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
   153
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   154
    @Override
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   155
    public String toString() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   156
        if (value == null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   157
            return emptyValue;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   158
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   159
            if (suffix.equals("")) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   160
                return value.toString();
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
                int initialLength = value.length();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   163
                String result = value.append(suffix).toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   164
                // reset value to pre-append initialLength
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   165
                value.setLength(initialLength);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   166
                return result;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   167
            }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   168
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   169
    }
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
     * 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
   173
     * 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
   174
     * {@code null}, then {@code "null"} is added.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   175
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   176
     * @param  newElement The element to add
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   177
     * @return a reference to this {@code StringJoiner}
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
    public StringJoiner add(CharSequence newElement) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   180
        prepareBuilder().append(newElement);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   181
        return this;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   182
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   183
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   184
    private StringBuilder prepareBuilder() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   185
        if (value != null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   186
            value.append(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   187
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   188
            value = new StringBuilder().append(prefix);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   189
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   190
        return value;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   191
    }
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
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   194
     * 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
   195
     * {@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
   196
     * 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
   197
     * 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
   198
     * 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
   199
     * {@code toString().length()}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   200
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   201
     * @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
   202
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   203
    public int length() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   204
        // 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
   205
        // 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
   206
        // 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
   207
        return (value != null ? value.length() + suffix.length() :
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   208
                emptyValue.length());
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   209
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   210
}