forked from apache/commons-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppendableJoiner.java
More file actions
315 lines (282 loc) · 11.5 KB
/
AppendableJoiner.java
File metadata and controls
315 lines (282 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringJoiner;
import java.util.function.Supplier;
import org.apache.commons.lang3.exception.UncheckedException;
import org.apache.commons.lang3.function.FailableBiConsumer;
/**
* Joins an array or {@link Iterable} into an existing {@link Appendable} like a {@link StringBuilder}; with the goal for call sites to avoid creating
* intermediary Strings. This is like {@link String#join(CharSequence, CharSequence...)}, {@link String#join(CharSequence, Iterable)}, and {@link StringJoiner}.
* <p>
* Keep an instance in a (static) variable for efficient joining into an {@link Appendable} or {@link StringBuilder} without creating temporary Strings.
* </p>
* <p>
* Use the builder and instance methods to reuse the same kind of joining prefix, suffix, delimiter, and string conversion.
* </p>
* <p>
* For example:
* </p>
*
* <pre>{@code
* // A reuseable instance
* private static final AppendableJoiner<Object> JOINER = AppendableJoiner.builder()
* .setPrefix("[")
* .setSuffix("]")
* .setDelimiter(", ")
* .get();
* }
* ...
* // Builds straight into a StringBuilder:
* StringBuilder sbuilder = new StringBuilder("1");
* JOINER.join(sbuilder, "A", "B");
* sbuilder.append("2");
* JOINER.join(sbuilder, "C", "D");
* sbuilder.append("3");
* // Returns "1[A, B]2[C, D]3"
* return sbuilder.toString();
* }</pre>
* <p>
* To provide a custom Object element to {@link CharSequence} converter, call {@link Builder#setElementAppender(FailableBiConsumer)}, for example:
* </p>
*
* <pre>{@code
* private static final AppendableJoiner<Item> JOINER = AppendableJoiner.builder()
* .setElementAppender(e -> (a, e) -> a.append(e.getFoo())
* a.append(e.getBar())
* a.append('!'))
* ...
* .get();
* }
* }</pre>
* <p>
* This class is immutable and thread-safe.
* </p>
*
* @param <T> the type of elements to join
* @see Appendable
* @see StringBuilder
* @see String#join(CharSequence, CharSequence...)
* @see String#join(CharSequence, Iterable)
* @see StringJoiner
* @since 3.15.0
*/
public final class AppendableJoiner<T> {
/**
* Builds instances of {@link AppendableJoiner}.
*
* @param <T> the type of elements to join
*/
public static final class Builder<T> implements Supplier<AppendableJoiner<T>> {
/** The sequence of characters to be used at the beginning. */
private CharSequence prefix;
/** The sequence of characters to be used at the end. */
private CharSequence suffix;
/** The delimiter that separates each element. */
private CharSequence delimiter;
/**
* The consumer used to render each element of type {@code T} onto an {@link Appendable}.
*/
private FailableBiConsumer<Appendable, T, IOException> appender;
/**
* Constructs a new instance.
*/
Builder() {
// empty
}
/**
* Gets a new instance of {@link AppendableJoiner}.
*/
@Override
public AppendableJoiner<T> get() {
return new AppendableJoiner<>(prefix, suffix, delimiter, appender);
}
/**
* Sets the delimiter that separates each element.
*
* @param delimiter the delimiter that separates each element
* @return {@code this} instance
*/
public Builder<T> setDelimiter(final CharSequence delimiter) {
this.delimiter = delimiter;
return this;
}
/**
* Sets the consumer used to render each element of type {@code T} onto an {@link Appendable}.
*
* @param appender the consumer used to render each element of type {@code T} onto an {@link Appendable}
* @return {@code this} instance
*/
public Builder<T> setElementAppender(final FailableBiConsumer<Appendable, T, IOException> appender) {
this.appender = appender;
return this;
}
/**
* Sets the sequence of characters to be used at the beginning.
*
* @param prefix the sequence of characters to be used at the beginning
* @return {@code this} instance
*/
public Builder<T> setPrefix(final CharSequence prefix) {
this.prefix = prefix;
return this;
}
/**
* Sets the sequence of characters to be used at the end.
*
* @param suffix the sequence of characters to be used at the end
* @return {@code this} instance
*/
public Builder<T> setSuffix(final CharSequence suffix) {
this.suffix = suffix;
return this;
}
}
/**
* Creates a new builder.
*
* @param <T> the type of elements
* @return a new builder
*/
public static <T> Builder<T> builder() {
return new Builder<>();
}
/** Could be public in the future, in some form. */
@SafeVarargs
static <A extends Appendable, T> A joinA(final A appendable, final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter,
final FailableBiConsumer<Appendable, T, IOException> appender, final T... elements) throws IOException {
return joinArray(appendable, prefix, suffix, delimiter, appender, elements);
}
private static <A extends Appendable, T> A joinArray(final A appendable, final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter,
final FailableBiConsumer<Appendable, T, IOException> appender, final T[] elements) throws IOException {
appendable.append(prefix);
if (elements != null) {
if (elements.length > 0) {
appender.accept(appendable, elements[0]);
}
for (int i = 1; i < elements.length; i++) {
appendable.append(delimiter);
appender.accept(appendable, elements[i]);
}
}
appendable.append(suffix);
return appendable;
}
/** Could be public in the future, in some form. */
static <T> StringBuilder joinI(final StringBuilder stringBuilder, final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter,
final FailableBiConsumer<Appendable, T, IOException> appender, final Iterable<T> elements) {
try {
return joinIterable(stringBuilder, prefix, suffix, delimiter, appender, elements);
} catch (final IOException e) {
// Cannot happen with a StringBuilder.
throw new UncheckedException(e);
}
}
private static <A extends Appendable, T> A joinIterable(final A appendable, final CharSequence prefix, final CharSequence suffix,
final CharSequence delimiter, final FailableBiConsumer<Appendable, T, IOException> appender, final Iterable<T> elements) throws IOException {
appendable.append(prefix);
if (elements != null) {
final Iterator<T> iterator = elements.iterator();
if (iterator.hasNext()) {
appender.accept(appendable, iterator.next());
}
while (iterator.hasNext()) {
appendable.append(delimiter);
appender.accept(appendable, iterator.next());
}
}
appendable.append(suffix);
return appendable;
}
/** Could be public in the future, in some form. */
@SafeVarargs
static <T> StringBuilder joinSB(final StringBuilder stringBuilder, final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter,
final FailableBiConsumer<Appendable, T, IOException> appender, final T... elements) {
try {
return joinArray(stringBuilder, prefix, suffix, delimiter, appender, elements);
} catch (final IOException e) {
// Cannot happen with a StringBuilder.
throw new UncheckedException(e);
}
}
private static CharSequence nonNull(final CharSequence value) {
return value != null ? value : StringUtils.EMPTY;
}
/** The sequence of characters to be used at the beginning. */
private final CharSequence prefix;
/** The sequence of characters to be used at the end. */
private final CharSequence suffix;
/** The delimiter that separates each element. */
private final CharSequence delimiter;
private final FailableBiConsumer<Appendable, T, IOException> appender;
/**
* Constructs a new instance.
*/
private AppendableJoiner(final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter,
final FailableBiConsumer<Appendable, T, IOException> appender) {
this.prefix = nonNull(prefix);
this.suffix = nonNull(suffix);
this.delimiter = nonNull(delimiter);
this.appender = appender != null ? appender : (a, e) -> a.append(String.valueOf(e));
}
/**
* Joins stringified objects from the given Iterable into a StringBuilder.
*
* @param stringBuilder the target
* @param elements the source
* @return the given StringBuilder
*/
public StringBuilder join(final StringBuilder stringBuilder, final Iterable<T> elements) {
return joinI(stringBuilder, prefix, suffix, delimiter, appender, elements);
}
/**
* Joins stringified objects from the given array into a StringBuilder.
*
* @param stringBuilder the target
* @param elements the source
* @return the given target StringBuilder
*/
public StringBuilder join(final StringBuilder stringBuilder, @SuppressWarnings("unchecked") final T... elements) {
return joinSB(stringBuilder, prefix, suffix, delimiter, appender, elements);
}
/**
* Joins stringified objects from the given Iterable into an Appendable.
*
* @param <A> the Appendable type
* @param appendable the target
* @param elements the source
* @return the given StringBuilder
* @throws IOException if an I/O error occurs
*/
public <A extends Appendable> A joinA(final A appendable, final Iterable<T> elements) throws IOException {
return joinIterable(appendable, prefix, suffix, delimiter, appender, elements);
}
/**
* Joins stringified objects from the given array into an Appendable.
*
* @param <A> the Appendable type
* @param appendable the target
* @param elements the source
* @return the given StringBuilder
* @throws IOException if an I/O error occurs
*/
public <A extends Appendable> A joinA(final A appendable, @SuppressWarnings("unchecked") final T... elements) throws IOException {
return joinA(appendable, prefix, suffix, delimiter, appender, elements);
}
}