jdk/src/share/classes/java/util/StringJoiner.java
author yhuang
Wed, 14 Aug 2013 22:49:54 -0700
changeset 19408 a40090569cc5
parent 19214 e5901820c3c1
child 21965 a7e08a4d1e02
permissions -rw-r--r--
8021121: ISO 4217 Amendment Number 156 Reviewed-by: naoto
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
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19074
diff changeset
    52
 * {@link java.util.stream.Collectors#joining(CharSequence)}. For example:
17934
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())
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19074
diff changeset
    58
 *     .collect(Collectors.joining(", "));
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
 *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19074
diff changeset
    61
 * @see java.util.stream.Collectors#joining(CharSequence)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19074
diff changeset
    62
 * @see java.util.stream.Collectors#joining(CharSequence, CharSequence, CharSequence)
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    63
 * @since  1.8
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    64
*/
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    65
public final class StringJoiner {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    66
    private final String prefix;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    67
    private final String delimiter;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    68
    private final String suffix;
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
    /*
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    71
     * 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
    72
     * 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
    73
     * 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
    74
     * the suffix each time.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    75
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    76
    private StringBuilder value;
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
    /*
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    79
     * 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
    80
     * 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
    81
     * 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
    82
     * other value including the empty String.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    83
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    84
    private String emptyValue;
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
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    87
     * 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
    88
     * {@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
    89
     * {@code delimiter}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    90
     * 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
    91
     * 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
    92
     * {@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
    93
     * unless {@code setEmptyValue} has first been called.
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
     * @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
    96
     *         element added to the {@code StringJoiner} value
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    97
     * @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
    98
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
    99
    public StringJoiner(CharSequence delimiter) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   100
        this(delimiter, "", "");
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
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   104
     * 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
   105
     * 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
   106
     * 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
   107
     * 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
   108
     * {@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
   109
     * {@code setEmptyValue} has first been called.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   110
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   111
     * @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
   112
     *         element added to the {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   113
     * @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
   114
     * @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
   115
     * @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
   116
     *         {@code suffix} is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   117
     */
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   118
    public StringJoiner(CharSequence delimiter,
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   119
                        CharSequence prefix,
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   120
                        CharSequence suffix) {
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   121
        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
   122
        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
   123
        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
   124
        // make defensive copies of arguments
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   125
        this.prefix = prefix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   126
        this.delimiter = delimiter.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   127
        this.suffix = suffix.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   128
        this.emptyValue = this.prefix + this.suffix;
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
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   131
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   132
     * 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
   133
     * 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
   134
     * 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
   135
     * 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
   136
     * 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
   137
     * 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
   138
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   139
     * @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
   140
     *         {@code StringJoiner}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   141
     * @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
   142
     * @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
   143
     *         {@code null}
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
    public StringJoiner setEmptyValue(CharSequence emptyValue) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   146
        this.emptyValue = Objects.requireNonNull(emptyValue,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   147
            "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
   148
        return this;
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
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
     * 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
   153
     * 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
   154
     * 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
   155
     * {@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
   156
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   157
     * @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
   158
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   159
    @Override
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   160
    public String toString() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   161
        if (value == null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   162
            return emptyValue;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   163
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   164
            if (suffix.equals("")) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   165
                return value.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   166
            } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   167
                int initialLength = value.length();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   168
                String result = value.append(suffix).toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   169
                // reset value to pre-append initialLength
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   170
                value.setLength(initialLength);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   171
                return result;
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
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   176
    /**
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   177
     * Adds a copy of the given {@code CharSequence} value as the next
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   178
     * 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
   179
     * {@code null}, then {@code "null"} is added.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   180
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   181
     * @param  newElement The element to add
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   182
     * @return a reference to this {@code StringJoiner}
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
    public StringJoiner add(CharSequence newElement) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   185
        prepareBuilder().append(newElement);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   186
        return this;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   187
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   188
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   189
    /**
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   190
     * Adds the contents of the given {@code StringJoiner} without prefix and
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   191
     * suffix as the next element if it is non-empty. If the given {@code
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   192
     * StringJoiner} is empty, the call has no effect.
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   193
     *
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   194
     * <p>A {@code StringJoiner} is empty if {@link #add(CharSequence) add()}
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   195
     * has never been called, and if {@code merge()} has never been called
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   196
     * with a non-empty {@code StringJoiner} argument.
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   197
     *
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   198
     * <p>If the other {@code StringJoiner} is using a different delimiter,
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   199
     * then elements from the other {@code StringJoiner} are concatenated with
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   200
     * that delimiter and the result is appended to this {@code StringJoiner}
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   201
     * as a single element.
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   202
     *
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   203
     * @param other The {@code StringJoiner} whose contents should be merged
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   204
     *              into this one
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   205
     * @throws NullPointerException if the other {@code StringJoiner} is null
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 19071
diff changeset
   206
     * @return This {@code StringJoiner}
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   207
     */
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   208
    public StringJoiner merge(StringJoiner other) {
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   209
        Objects.requireNonNull(other);
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   210
        if (other.value != null) {
19071
bc096b85d91d 8020977: StringJoiner merges with itself not as expected
henryjen
parents: 18778
diff changeset
   211
            final int length = other.value.length();
bc096b85d91d 8020977: StringJoiner merges with itself not as expected
henryjen
parents: 18778
diff changeset
   212
            // lock the length so that we can seize the data to be appended
bc096b85d91d 8020977: StringJoiner merges with itself not as expected
henryjen
parents: 18778
diff changeset
   213
            // before initiate copying to avoid interference, especially when
bc096b85d91d 8020977: StringJoiner merges with itself not as expected
henryjen
parents: 18778
diff changeset
   214
            // merge 'this'
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   215
            StringBuilder builder = prepareBuilder();
19071
bc096b85d91d 8020977: StringJoiner merges with itself not as expected
henryjen
parents: 18778
diff changeset
   216
            builder.append(other.value, other.prefix.length(), length);
18778
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   217
        }
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   218
        return this;
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   219
    }
7214a903b084 8017231: Add StringJoiner.merge
alanb
parents: 17934
diff changeset
   220
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   221
    private StringBuilder prepareBuilder() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   222
        if (value != null) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   223
            value.append(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   224
        } else {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   225
            value = new StringBuilder().append(prefix);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   226
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   227
        return value;
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   228
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   229
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   230
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   231
     * 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
   232
     * {@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
   233
     * 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
   234
     * 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
   235
     * 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
   236
     * {@code toString().length()}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   237
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   238
     * @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
   239
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   240
    public int length() {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   241
        // 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
   242
        // 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
   243
        // 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
   244
        return (value != null ? value.length() + suffix.length() :
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   245
                emptyValue.length());
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   246
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff changeset
   247
}