Top Description Inners Fields Constructors Methods
org.apache.commons.lang3

public final Class AppendableJoiner<T>

extends Object
Class Inheritance
Type Parameters
<T>
the type of elements to join.
Imports
java.io.IOException, java.util.Iterator, .StringJoiner, java.util.function.Supplier, org.apache.commons.lang3.exception.UncheckedException, org.apache.commons.lang3.function.FailableBiConsumer

Joins an array or 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.

Since
3.15.0
See Also
Appendable, StringBuilder, String#join(CharSequence, CharSequence...), String#join(CharSequence, Iterable), StringJoiner

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
AppendableJoiner.Builder<
the type of elements to join.
T
>

Builds instances of AppendableJoiner.

Field Summary

Modifier and TypeField and Description
private final FailableBiConsumer<Appendable, T, IOException>
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.

Constructor Summary

AccessConstructor and Description
private
AppendableJoiner(final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter, final FailableBiConsumer<Appendable, T, IOException> appender)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static <
The type of elements.
T
>
AppendableJoiner.Builder<T>

Returns:

a new builder.
builder
()

Creates a new builder.

public StringBuilder

Returns:

The given StringBuilder.
join
(final 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.
join
(final 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

Returns:

The given StringBuilder.
joinA
(final A
The target.
appendable
,
final Iterable<T>
The source.
elements
)

Joins stringified objects from the given Iterable into an Appendable.

public <
the Appendable type.
A extends Appendable
>
A

Returns:

The given StringBuilder.
joinA
(final A
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
nonNull(final CharSequence value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

appenderback to summary
private final FailableBiConsumer<Appendable, T, IOException> appender
delimiterback to summary
private final CharSequence delimiter

The delimiter that separates each element.

prefixback to summary
private final CharSequence prefix

The sequence of characters to be used at the beginning.

suffixback to summary
private final CharSequence suffix

The sequence of characters to be used at the end.

Constructor Detail

AppendableJoinerback to summary
private AppendableJoiner(final CharSequence prefix, final CharSequence suffix, final CharSequence delimiter, final FailableBiConsumer<Appendable, T, IOException> appender)

Constructs a new instance.

Method Detail

builderback to summary
public static <T> AppendableJoiner.Builder<T> builder()

Creates a new builder.

Parameters
<T>
The type of elements.
Returns:AppendableJoiner.Builder<T>

a new builder.

joinback to summary
public StringBuilder join(final StringBuilder stringBuilder, final Iterable<T> elements)

Joins stringified objects from the given Iterable into a StringBuilder.

Parameters
stringBuilder:StringBuilder

The target.

elements:Iterable<T>

The source.

Returns:StringBuilder

The given StringBuilder.

joinback to summary
public StringBuilder join(final StringBuilder stringBuilder, final T... elements)

Joins stringified objects from the given array into a StringBuilder.

Parameters
stringBuilder:StringBuilder

The target.

elements:T[]

The source.

Returns:StringBuilder

the given target StringBuilder.

joinAback 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.

Annotations
@SafeVarargs
joinAback 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.

Parameters
<A>
the Appendable type.
appendable:A

The target.

elements:Iterable<T>

The source.

Returns:A

The given StringBuilder.

Exceptions
IOException:
If an I/O error occurs
joinAback 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.

Parameters
<A>
the Appendable type.
appendable:A

The target.

elements:T[]

The source.

Returns:A

The given StringBuilder.

Exceptions
IOException:
If an I/O error occurs
joinArrayback 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
joinIback 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.

joinIterableback 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
joinSBback 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.

Annotations
@SafeVarargs
nonNullback to summary
private static CharSequence nonNull(final CharSequence value)
org.apache.commons.lang3 back to summary

public final Class AppendableJoiner.Builder<T>

extends Object
implements Supplier<AppendableJoiner<T>>
Class Inheritance
All Implemented Interfaces
java.util.function.Supplier
Type Parameters
<T>
the type of elements to join.

Builds instances of AppendableJoiner.

Field Summary

Modifier and TypeField and Description
private FailableBiConsumer<Appendable, T, IOException>
appender

The consumer used to render each element of type T onto an Appendable.

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.

Constructor Summary

AccessConstructor and Description
pack-priv
Builder()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public AppendableJoiner<T>
get()

Implements java.util.function.Supplier.get.

Gets a new instance of AppendableJoiner.

public AppendableJoiner.Builder<T>

Returns:

this instance.
setDelimiter
(final CharSequence
The delimiter that separates each element.
delimiter
)

Sets the delimiter that separates each element.

public AppendableJoiner.Builder<T>

Returns:

this instance.
setElementAppender
(final FailableBiConsumer<Appendable, T, IOException>
The consumer used to render each element of type T onto an Appendable.
appender
)

Sets the consumer used to render each element of type T onto an Appendable.

public AppendableJoiner.Builder<T>

Returns:

this instance.
setPrefix
(final CharSequence
The sequence of characters to be used at the beginning.
prefix
)

Sets the sequence of characters to be used at the beginning.

public AppendableJoiner.Builder<T>

Returns:

this instance.
setSuffix
(final CharSequence
The sequence of characters to be used at the end.
suffix
)

Sets the sequence of characters to be used at the end.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

appenderback to summary
private FailableBiConsumer<Appendable, T, IOException> appender

The consumer used to render each element of type T onto an Appendable.

delimiterback to summary
private CharSequence delimiter

The delimiter that separates each element.

prefixback to summary
private CharSequence prefix

The sequence of characters to be used at the beginning.

suffixback to summary
private CharSequence suffix

The sequence of characters to be used at the end.

Constructor Detail

Builderback to summary
pack-priv Builder()

Constructs a new instance.

Method Detail

getback to summary
public AppendableJoiner<T> get()

Implements java.util.function.Supplier.get.

Gets a new instance of AppendableJoiner.

Annotations
@Override
setDelimiterback to summary
public AppendableJoiner.Builder<T> setDelimiter(final CharSequence delimiter)

Sets the delimiter that separates each element.

Parameters
delimiter:CharSequence

The delimiter that separates each element.

Returns:AppendableJoiner.Builder<T>

this instance.

setElementAppenderback to summary
public AppendableJoiner.Builder<T> setElementAppender(final FailableBiConsumer<Appendable, T, IOException> appender)

Sets the consumer used to render each element of type T onto an Appendable.

Parameters
appender:FailableBiConsumer<Appendable, T, IOException>

The consumer used to render each element of type T onto an Appendable.

Returns:AppendableJoiner.Builder<T>

this instance.

setPrefixback to summary
public AppendableJoiner.Builder<T> setPrefix(final CharSequence prefix)

Sets the sequence of characters to be used at the beginning.

Parameters
prefix:CharSequence

The sequence of characters to be used at the beginning.

Returns:AppendableJoiner.Builder<T>

this instance.

setSuffixback to summary
public AppendableJoiner.Builder<T> setSuffix(final CharSequence suffix)

Sets the sequence of characters to be used at the end.

Parameters
suffix:CharSequence

The sequence of characters to be used at the end.

Returns:AppendableJoiner.Builder<T>

this instance.