public class Joining extends Object
Collector. This collector is
capable to join the input CharSequence elements with given delimiter
optionally wrapping into given prefix and suffix and optionally limiting the
length of the resulting string (in Unicode code units, code points or
grapheme clusters) adding the specified ellipsis sequence. This collector
supersedes the standard JDK
Collectors.joining(CharSequence, CharSequence, CharSequence)
collectors family.
This collector is short-circuiting when the string length is limited in either of ways. Otherwise it's not short-circuiting.
Every specific collector represented by this class is immutable, so you can share it. A bunch of methods is provided to create a new collector based on this one.
To create Joining collector use with(CharSequence) static
method and specify the delimiter. For further setup use specific instance
methods which return new Joining objects like this:
StreamEx.of(source).collect(Joining.with(", ").wrap("[", "]")
.maxCodePoints(100).cutAtWord());
The intermediate accumulation type of this collector is the implementation
detail and not exposed to the API. If you want to cast it to
Collector type, use ? as accumulator type variable:
Collector<CharSequence, ?, String> joining = Joining.with(", ");
Collector.Characteristics| Modifier and Type | Method and Description |
|---|---|
BiConsumer<Joining.Accumulator,CharSequence> |
accumulator()
A function that folds a value into a mutable result container.
|
Set<Collector.Characteristics> |
characteristics()
Returns a
Set of Collector.Characteristics indicating
the characteristics of this Collector. |
BinaryOperator<Joining.Accumulator> |
combiner()
A function that accepts two partial results and merges them.
|
Joining |
cutAfterDelimiter()
Returns a
Collector which behaves like this collector, but cuts
the resulting string after the delimiter when limit is reached. |
Joining |
cutAnywhere()
Returns a
Collector which behaves like this collector, but cuts
the resulting string at any point when limit is reached. |
Joining |
cutAtCodePoint()
Returns a
Collector which behaves like this collector, but cuts
the resulting string between any code points when limit is reached. |
Joining |
cutAtGrapheme()
Returns a
Collector which behaves like this collector, but cuts
the resulting string at grapheme cluster boundary when limit is reached. |
Joining |
cutAtWord()
Returns a
Collector which behaves like this collector, but cuts
the resulting string at word boundary when limit is reached. |
Joining |
cutBeforeDelimiter()
Returns a
Collector which behaves like this collector, but cuts
the resulting string before the delimiter when limit is reached. |
Joining |
ellipsis(CharSequence ellipsis)
Returns a
Collector which behaves like this collector, but uses
the specified ellipsis CharSequence instead of default
"..." when the string limit (if specified) is reached. |
Function<Joining.Accumulator,String> |
finisher()
Perform the final transformation from the intermediate accumulation type
A to the final result type R. |
Joining |
maxChars(int limit)
Returns a
Collector which behaves like this collector, but sets
the maximal length of the resulting string to the specified number of
UTF-16 characters (or Unicode code units). |
Joining |
maxCodePoints(int limit)
Returns a
Collector which behaves like this collector, but sets
the maximal number of Unicode code points of the resulting string. |
Joining |
maxGraphemes(int limit)
Returns a
Collector which behaves like this collector, but sets
the maximal number of grapheme clusters. |
Supplier<Joining.Accumulator> |
supplier()
A function that creates and returns a new mutable result container.
|
static Joining |
with(CharSequence delimiter)
Returns a
Collector that concatenates the input elements,
separated by the specified delimiter, in encounter order. |
Joining |
wrap(CharSequence prefix,
CharSequence suffix)
Returns a
Collector which behaves like this collector, but
additionally wraps the result with the specified prefix and suffix. |
public static Joining with(CharSequence delimiter)
Collector that concatenates the input elements,
separated by the specified delimiter, in encounter order.
This collector is similar to Collectors.joining(CharSequence),
but can be further set up in a flexible way, for example, specifying the
maximal allowed length of the resulting String.
delimiter - the delimiter to be used between each elementCollector which concatenates CharSequence elements,
separated by the specified delimiter, in encounter orderCollectors.joining(CharSequence)public Joining wrap(CharSequence prefix, CharSequence suffix)
Collector which behaves like this collector, but
additionally wraps the result with the specified prefix and suffix.
The collector returned by
Joining.with(delimiter).wrap(prefix, suffix) is equivalent to
Collectors.joining(CharSequence, CharSequence, CharSequence), but
can be further set up in a flexible way, for example, specifying the
maximal allowed length of the resulting String.
If length limit is specified for the collector, the prefix length and the
suffix length are also counted towards this limit. If the length of the
prefix and the suffix exceed the limit, the resulting collector will not
accumulate any elements and produce the same output. For example,
stream.collect(Joining.with(",").wrap("prefix", "suffix").maxChars(9))
will produce "prefixsuf" string regardless of the input stream
content.
You may wrap several times:
Joining.with(",").wrap("[", "]").wrap("(", ")") is equivalent to
Joining.with(",").wrap("([", "])").
prefix - the sequence of characters to be used at the beginning of
the joined resultsuffix - the sequence of characters to be used at the end of the
joined resultCollector which wraps the result with the specified
prefix and suffix.public Joining ellipsis(CharSequence ellipsis)
Collector which behaves like this collector, but uses
the specified ellipsis CharSequence instead of default
"..." when the string limit (if specified) is reached.ellipsis - the sequence of characters to be used at the end of the
joined result to designate that not all of the input elements are
joined due to the specified string length restriction.Collector which will use the specified ellipsis
instead of current setting.public Joining maxChars(int limit)
Collector which behaves like this collector, but sets
the maximal length of the resulting string to the specified number of
UTF-16 characters (or Unicode code units). This setting overwrites any
limit previously set by maxChars(int),
maxCodePoints(int) or maxGraphemes(int) call.
The String produced by the resulting collector is guaranteed to
have length which does not exceed the specified
limit. An ellipsis sequence (by default "...") is used to
designate whether the limit was reached. Use
ellipsis(CharSequence) to set custom ellipsis sequence.
The collector returned by this method is short-circuiting: it may not process all the input elements if the limit is reached.
limit - the maximal number of UTF-16 characters in the resulting
String.Collector which will produce String no longer than
given limit.public Joining maxCodePoints(int limit)
Collector which behaves like this collector, but sets
the maximal number of Unicode code points of the resulting string. This
setting overwrites any limit previously set by maxChars(int),
maxCodePoints(int) or maxGraphemes(int) call.
The String produced by the resulting collector is guaranteed to
have no more code points than the specified limit. An ellipsis sequence
(by default "...") is used to designate whether the limit was
reached. Use ellipsis(CharSequence) to set custom ellipsis
sequence.
The collector returned by this method is short-circuiting: it may not process all the input elements if the limit is reached.
limit - the maximal number of code points in the resulting String.Collector which will produce String no longer than
given limit.public Joining maxGraphemes(int limit)
Collector which behaves like this collector, but sets
the maximal number of grapheme clusters. This setting overwrites any
limit previously set by maxChars(int),
maxCodePoints(int) or maxGraphemes(int) call.
The grapheme cluster is defined in Unicode Text Segmentation technical report. Basically, it counts
base character and the following combining characters as single object.
The String produced by the resulting collector is guaranteed to
have no more grapheme clusters than the specified limit. An ellipsis
sequence (by default "...") is used to designate whether the
limit was reached. Use ellipsis(CharSequence) to set custom
ellipsis sequence.
The collector returned by this method is short-circuiting: it may not process all the input elements if the limit is reached.
limit - the maximal number of grapheme clusters in the resulting
String.Collector which will produce String no longer than
given limit.public Joining cutAnywhere()
Collector which behaves like this collector, but cuts
the resulting string at any point when limit is reached.
The resulting collector will produce String which length is
exactly equal to the specified limit if the limit is reached. If used
with maxChars(int), the resulting string may be cut in the
middle of surrogate pair.
Collector which cuts the resulting string at any
point when limit is reached.public Joining cutAtCodePoint()
Collector which behaves like this collector, but cuts
the resulting string between any code points when limit is reached.
The resulting collector will not split the surrogate pair when used with
maxChars(int) or maxCodePoints(int). However it may
remove the combining character which may result in incorrect rendering of
the last displayed grapheme.
Collector which cuts the resulting string between
code points.public Joining cutAtGrapheme()
Collector which behaves like this collector, but cuts
the resulting string at grapheme cluster boundary when limit is reached.
This is the default behavior.
The grapheme cluster is defined in Unicode Text Segmentation technical report. Thus the resulting collector will not split the surrogate pair and will preserve any combining characters or remove them with the base character.
Collector which cuts the resulting string at
grapheme cluster boundary.public Joining cutAtWord()
Collector which behaves like this collector, but cuts
the resulting string at word boundary when limit is reached.
The beginning and end of every input stream element or delimiter is
always considered as word boundary, so the stream of
"one", "two three" collected with
Joining.with("").maxChars(n).ellipsis("").cutAtWord() may produce
the following strings depending on n:
""
"one"
"onetwo"
"onetwo "
"onetwo three"
Collector which cuts the resulting string at word
boundary.public Joining cutBeforeDelimiter()
Collector which behaves like this collector, but cuts
the resulting string before the delimiter when limit is reached.Collector which cuts the resulting string at before
the delimiter.public Joining cutAfterDelimiter()
Collector which behaves like this collector, but cuts
the resulting string after the delimiter when limit is reached.Collector which cuts the resulting string at after
the delimiter.public Supplier<Joining.Accumulator> supplier()
java.util.stream.Collectorpublic BiConsumer<Joining.Accumulator,CharSequence> accumulator()
java.util.stream.Collectorpublic BinaryOperator<Joining.Accumulator> combiner()
java.util.stream.Collectorpublic Function<Joining.Accumulator,String> finisher()
java.util.stream.CollectorA to the final result type R.
If the characteristic IDENTITY_TRANSFORM is
set, this function may be presumed to be an identity transform with an
unchecked cast from A to R.
public Set<Collector.Characteristics> characteristics()
java.util.stream.CollectorSet of Collector.Characteristics indicating
the characteristics of this Collector. This set should be immutable.Copyright © 2017. All rights reserved.