Iterable into an existing Appendable like a StringBuilder; with the goal for call sites to avoid creating
intermediary Strings. This is like String#join(CharSequence, CharSequence...) , String#join(CharSequence, Iterable), and StringJoiner.
Keep an instance in a (static) variable for efficient joining into an Appendable or StringBuilder without creating temporary Strings.
Use the builder and instance methods to reuse the same kind of joining prefix, suffix, delimiter, and string conversion.
For example:
// 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();
}
To provide a custom Object element to CharSequence converter, call Builder#setElementAppender(FailableBiConsumer), for example:
private static final AppendableJoiner<Item> JOINER = AppendableJoiner.builder()
.setElementAppender(e -> (a, e) -> a.append(e.getFoo())
a.append(e.getBar())
a.append('!'))
...
.get();
}
This class is immutable and thread-safe.
Appendable, StringBuilder, String#join(CharSequence, CharSequence...) , String#join(CharSequence, Iterable), StringJoiner
| Modifier and Type | Class and Description |
|---|---|
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| private final FailableBiConsumer | |
| private final CharSequence | delimiter
The delimiter that separates each element. |
| private final CharSequence | prefix
The sequence of characters to be used at the beginning. |
| private final CharSequence | suffix
The sequence of characters to be used at the end. |
| Access | Constructor and Description |
|---|---|
| private | AppendableJoiner(final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter, final FailableBiConsumer<Appendable, T, IOException> appender)
Constructs a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| public static < The type of elements. T> AppendableJoiner. | |
| public StringBuilder | Returns: The given StringBuilder.The target. stringBuilder, final Iterable<T> The source. elements)Joins stringified objects from the given Iterable into a StringBuilder. |
| public StringBuilder | Returns: the given target StringBuilder.The target. stringBuilder, final T... The source. elements)Joins stringified objects from the given array into a StringBuilder. |
| pack-priv 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)
Could be public in the future, in some form. |
| public < the Appendable type. A extends Appendable> A | |
| public < the Appendable type. A extends Appendable> A | Returns: The given StringBuilder.The target. appendable, final T... The source. elements)Joins stringified objects from the given array into an Appendable. |
| 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)
|
| pack-priv 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)
Could be public in the future, in some form. |
| 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)
|
| pack-priv 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)
Could be public in the future, in some form. |
| private static CharSequence |
| appender | back to summary |
|---|---|
| private final FailableBiConsumer<Appendable, T, IOException> appender | |
| delimiter | back to summary |
|---|---|
| private final CharSequence delimiter The delimiter that separates each element. | |
| prefix | back to summary |
|---|---|
| private final CharSequence prefix The sequence of characters to be used at the beginning. | |
| suffix | back to summary |
|---|---|
| private final CharSequence suffix The sequence of characters to be used at the end. | |
| AppendableJoiner | back to summary |
|---|---|
| private AppendableJoiner(final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter, final FailableBiConsumer<Appendable, T, IOException> appender) Constructs a new instance. | |
| builder | back to summary |
|---|---|
| public static <T> AppendableJoiner. Creates a new builder.
| |
| join | back to summary |
|---|---|
| public StringBuilder join(final StringBuilder stringBuilder, final Iterable<T> elements) Joins stringified objects from the given Iterable into a StringBuilder.
| |
| join | back to summary |
|---|---|
| public StringBuilder join(final StringBuilder stringBuilder, final T... elements) Joins stringified objects from the given array into a StringBuilder.
| |
| joinA | back to summary |
|---|---|
| pack-priv 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 Could be public in the future, in some form.
| |
| joinA | back to summary |
|---|---|
| public <A extends Appendable> A joinA(final A appendable, final Iterable<T> elements) throws IOException Joins stringified objects from the given Iterable into an Appendable.
| |
| joinA | back to summary |
|---|---|
| public <A extends Appendable> A joinA(final A appendable, final T... elements) throws IOException Joins stringified objects from the given array into an Appendable.
| |
| joinArray | back to summary |
|---|---|
| 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 | |
| joinI | back to summary |
|---|---|
| pack-priv 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) Could be public in the future, in some form. | |
| joinIterable | back to summary |
|---|---|
| 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 | |
| joinSB | back to summary |
|---|---|
| pack-priv 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) Could be public in the future, in some form.
| |
| nonNull | back to summary |
|---|---|
| private static CharSequence nonNull(final CharSequence value) | |
AppendableJoiner.
| Modifier and Type | Field and Description |
|---|---|
| private FailableBiConsumer | appender
The consumer used to render each element of type |
| private CharSequence | delimiter
The delimiter that separates each element. |
| private CharSequence | prefix
The sequence of characters to be used at the beginning. |
| private CharSequence | suffix
The sequence of characters to be used at the end. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public AppendableJoiner | |
| public AppendableJoiner. | Returns: this instance.The delimiter that separates each element. delimiter)Sets the delimiter that separates each element. |
| public AppendableJoiner. | Returns: this instance.The consumer used to render each element of type appender)T onto an Appendable.Sets the consumer used to render each element of type |
| public AppendableJoiner. | Returns: this instance.The sequence of characters to be used at the beginning. prefix)Sets the sequence of characters to be used at the beginning. |
| public AppendableJoiner. | Returns: this instance.The sequence of characters to be used at the end. suffix)Sets the sequence of characters to be used at the end. |
| appender | back to summary |
|---|---|
| private FailableBiConsumer<Appendable, T, IOException> appender The consumer used to render each element of type | |
| delimiter | back to summary |
|---|---|
| private CharSequence delimiter The delimiter that separates each element. | |
| prefix | back to summary |
|---|---|
| private CharSequence prefix The sequence of characters to be used at the beginning. | |
| suffix | back to summary |
|---|---|
| private CharSequence suffix The sequence of characters to be used at the end. | |
| Builder | back to summary |
|---|---|
| pack-priv Builder() Constructs a new instance. | |
| get | back to summary |
|---|---|
| public AppendableJoiner Implements java. Gets a new instance of
| |
| setDelimiter | back to summary |
|---|---|
| public AppendableJoiner. Sets the delimiter that separates each element.
| |
| setElementAppender | back to summary |
|---|---|
| public AppendableJoiner. Sets the consumer used to render each element of type
| |
| setPrefix | back to summary |
|---|---|
| public AppendableJoiner. Sets the sequence of characters to be used at the beginning.
| |
| setSuffix | back to summary |
|---|---|
| public AppendableJoiner. Sets the sequence of characters to be used at the end.
| |