author | henryjen |
Mon, 08 Jul 2013 15:46:26 -0400 | |
changeset 18825 | 06636235cd12 |
parent 18778 | 7214a903b084 |
child 19071 | bc096b85d91d |
permissions | -rw-r--r-- |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
1 |
/* |
17333 | 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 |
*/ |
18778 | 117 |
public StringJoiner(CharSequence delimiter, |
118 |
CharSequence prefix, |
|
119 |
CharSequence suffix) { |
|
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
// make defensive copies of arguments |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
124 |
this.prefix = prefix.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
125 |
this.delimiter = delimiter.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
126 |
this.suffix = suffix.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
127 |
this.emptyValue = this.prefix + this.suffix; |
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 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
131 |
* 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
|
132 |
* 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
|
133 |
* 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
|
134 |
* 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
|
135 |
* 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
|
136 |
* 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
|
137 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
138 |
* @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
|
139 |
* {@code StringJoiner} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
140 |
* @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
|
141 |
* @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
|
142 |
* {@code null} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
143 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
144 |
public StringJoiner setEmptyValue(CharSequence emptyValue) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
145 |
this.emptyValue = Objects.requireNonNull(emptyValue, |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
146 |
"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
|
147 |
return this; |
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 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
151 |
* 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
|
152 |
* 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
|
153 |
* 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
|
154 |
* {@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
|
155 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
156 |
* @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
|
157 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
158 |
@Override |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
159 |
public String toString() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
160 |
if (value == null) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
161 |
return emptyValue; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
162 |
} else { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
163 |
if (suffix.equals("")) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
164 |
return value.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
165 |
} else { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
166 |
int initialLength = value.length(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
167 |
String result = value.append(suffix).toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
168 |
// reset value to pre-append initialLength |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
169 |
value.setLength(initialLength); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
170 |
return result; |
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 |
/** |
18778 | 176 |
* 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
|
177 |
* 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
|
178 |
* {@code null}, then {@code "null"} is added. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
179 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
180 |
* @param newElement The element to add |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
181 |
* @return a reference to this {@code StringJoiner} |
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 |
public StringJoiner add(CharSequence newElement) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
184 |
prepareBuilder().append(newElement); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
185 |
return this; |
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 |
|
18778 | 188 |
/** |
189 |
* Adds the contents of the given {@code StringJoiner} without prefix and |
|
190 |
* suffix as the next element if it is non-empty. If the given {@code |
|
191 |
* StringJoiner} is empty, the call has no effect. |
|
192 |
* |
|
193 |
* <p>A {@code StringJoiner} is empty if {@link #add(CharSequence) add()} |
|
194 |
* has never been called, and if {@code merge()} has never been called |
|
195 |
* with a non-empty {@code StringJoiner} argument. |
|
196 |
* |
|
197 |
* <p>If the other {@code StringJoiner} is using a different delimiter, |
|
198 |
* then elements from the other {@code StringJoiner} are concatenated with |
|
199 |
* that delimiter and the result is appended to this {@code StringJoiner} |
|
200 |
* as a single element. |
|
201 |
* |
|
202 |
* @param other The {@code StringJoiner} whose contents should be merged |
|
203 |
* into this one |
|
204 |
* @throws NullPointerException if the other {@code StringJoiner} is null |
|
205 |
*/ |
|
206 |
public StringJoiner merge(StringJoiner other) { |
|
207 |
Objects.requireNonNull(other); |
|
208 |
if (other.value != null) { |
|
209 |
StringBuilder builder = prepareBuilder(); |
|
210 |
StringBuilder otherBuilder = other.value; |
|
211 |
if (other.prefix.length() < otherBuilder.length()) { |
|
212 |
builder.append(otherBuilder, other.prefix.length(), otherBuilder.length()); |
|
213 |
} |
|
214 |
} |
|
215 |
return this; |
|
216 |
} |
|
217 |
||
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
218 |
private StringBuilder prepareBuilder() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
219 |
if (value != null) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
220 |
value.append(delimiter); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
221 |
} else { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
222 |
value = new StringBuilder().append(prefix); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
223 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
224 |
return value; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
225 |
} |
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 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
228 |
* 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
|
229 |
* {@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
|
230 |
* 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
|
231 |
* 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
|
232 |
* 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
|
233 |
* {@code toString().length()}. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
234 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
235 |
* @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
|
236 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
237 |
public int length() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
238 |
// 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
|
239 |
// 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
|
240 |
// 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
|
241 |
return (value != null ? value.length() + suffix.length() : |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
242 |
emptyValue.length()); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
243 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
244 |
} |