Package com.landawn.abacus.util
Class Joiner
- java.lang.Object
-
- com.landawn.abacus.util.Joiner
-
- All Implemented Interfaces:
AutoCloseable
public class Joiner extends Object implements AutoCloseable
The Class Joiner.- Since:
- 1.3
- Author:
- haiyangl
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_DELIMITER
The Constant DEFAULT_DELIMITER.static String
DEFAULT_KEY_VALUE_DELIMITER
The Constant DEFAULT_KEY_VALUE_DELIMITER.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Joiner
append(boolean element)
Joiner
append(char element)
Joiner
append(char[] element)
Joiner
append(char[] element, int offset, int len)
Joiner
append(double element)
Joiner
append(float element)
Joiner
append(int element)
Joiner
append(long element)
Joiner
append(CharSequence element)
Joiner
append(CharSequence element, int start, int end)
Joiner
append(Object element)
Joiner
append(String element)
Joiner
append(StringBuffer element)
Joiner
appendAll(boolean[] a)
Joiner
appendAll(boolean[] a, int fromIndex, int toIndex)
Joiner
appendAll(byte[] a)
Joiner
appendAll(byte[] a, int fromIndex, int toIndex)
Joiner
appendAll(char[] a)
Joiner
appendAll(char[] a, int fromIndex, int toIndex)
Joiner
appendAll(double[] a)
Joiner
appendAll(double[] a, int fromIndex, int toIndex)
Joiner
appendAll(float[] a)
Joiner
appendAll(float[] a, int fromIndex, int toIndex)
Joiner
appendAll(int[] a)
Joiner
appendAll(int[] a, int fromIndex, int toIndex)
Joiner
appendAll(long[] a)
Joiner
appendAll(long[] a, int fromIndex, int toIndex)
Joiner
appendAll(short[] a)
Joiner
appendAll(short[] a, int fromIndex, int toIndex)
Joiner
appendAll(BooleanList c)
Joiner
appendAll(BooleanList c, int fromIndex, int toIndex)
Joiner
appendAll(ByteList c)
Joiner
appendAll(ByteList c, int fromIndex, int toIndex)
Joiner
appendAll(CharList c)
Joiner
appendAll(CharList c, int fromIndex, int toIndex)
Joiner
appendAll(DoubleList c)
Joiner
appendAll(DoubleList c, int fromIndex, int toIndex)
Joiner
appendAll(FloatList c)
Joiner
appendAll(FloatList c, int fromIndex, int toIndex)
Joiner
appendAll(IntList c)
Joiner
appendAll(IntList c, int fromIndex, int toIndex)
Joiner
appendAll(LongList c)
Joiner
appendAll(LongList c, int fromIndex, int toIndex)
Joiner
appendAll(ShortList c)
Joiner
appendAll(ShortList c, int fromIndex, int toIndex)
Joiner
appendAll(Object[] a)
Joiner
appendAll(Object[] a, int fromIndex, int toIndex)
Joiner
appendAll(Collection<?> c)
Joiner
appendAll(Collection<?> c, int fromIndex, int toIndex)
Joiner
appendEntries(Object entity)
Joiner
appendEntries(Map<?,?> m)
Joiner
appendEntries(Map<?,?> m, int fromIndex, int toIndex)
Joiner
appendEntry(String key, boolean value)
Joiner
appendEntry(String key, char value)
Joiner
appendEntry(String key, char[] value)
Joiner
appendEntry(String key, double value)
Joiner
appendEntry(String key, float value)
Joiner
appendEntry(String key, int value)
Joiner
appendEntry(String key, long value)
Joiner
appendEntry(String key, CharSequence value)
Joiner
appendEntry(String key, Object value)
Joiner
appendEntry(String key, String value)
Joiner
appendEntry(String key, StringBuffer value)
Joiner
appendEntry(Map.Entry<?,?> entry)
Joiner
appendIf(boolean b, Supplier<?> supplier)
Joiner
appendIfNotNull(Object element)
Append if not null.void
close()
Close.static Joiner
defauLt()
int
length()
<T,E extends Exception>
Tmap(Throwables.Function<? super String,T,E> mapper)
The underlineStringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.<T,E extends Exception>
u.Nullable<T>mapIfNotEmpty(Throwables.Function<? super String,T,E> mapper)
Callmapper
only if at least one element/object/entry is appended.Joiner
merge(Joiner other)
Adds the contents of the givenStringJoiner
without prefix and suffix as the next element if it is non-empty.Joiner
repeat(Object obj, int n)
Joiner
repeat(String str, int n)
Joiner
reuseCachedBuffer(boolean useCachedBuffer)
Improving performance by setuseCachedBuffer=true
, and must remember to calltoString()/map()/mapIfNotEmpty()/stream()/streamIfNotEmpty()
orclose()
to recycle the cached buffer.Joiner
setEmptyValue(CharSequence emptyValue)
Sets the empty value.Joiner
skipNull(boolean skipNull)
Ignore thenull
element/value forkey/value, Map, Entity
when the specifiedelement
orvalue
isnull
if it's set totrue
.Stream<String>
stream()
The underlineStringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.Stream<String>
streamIfNotEmpty()
Returns a stream with the String value generated bytoString()
if at least one element/object/entry is appended, otherwise an emptyStream
is returned.String
toString()
Returns the current value, consisting of theprefix
, the values added so far separated by thedelimiter
, and thesuffix
, unless no elements have been added in which case, theprefix + suffix
or theemptyValue
characters are returnedJoiner
trim(boolean trim)
Joiner
useForNull(String nullText)
Use for null.static Joiner
with(CharSequence delimiter)
static Joiner
with(CharSequence delimiter, CharSequence keyValueDelimiter)
static Joiner
with(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
static Joiner
with(CharSequence delimiter, CharSequence keyValueDelimiter, CharSequence prefix, CharSequence suffix)
-
-
-
Field Detail
-
DEFAULT_DELIMITER
public static final String DEFAULT_DELIMITER
The Constant DEFAULT_DELIMITER.
-
DEFAULT_KEY_VALUE_DELIMITER
public static final String DEFAULT_KEY_VALUE_DELIMITER
The Constant DEFAULT_KEY_VALUE_DELIMITER.- See Also:
- Constant Field Values
-
-
Method Detail
-
defauLt
public static Joiner defauLt()
- Returns:
-
with
public static Joiner with(CharSequence delimiter)
- Parameters:
delimiter
-- Returns:
-
with
public static Joiner with(CharSequence delimiter, CharSequence keyValueDelimiter)
- Parameters:
delimiter
-keyValueDelimiter
-- Returns:
-
with
public static Joiner with(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
- Parameters:
delimiter
-prefix
-suffix
-- Returns:
-
with
public static Joiner with(CharSequence delimiter, CharSequence keyValueDelimiter, CharSequence prefix, CharSequence suffix)
- Parameters:
delimiter
-keyValueDelimiter
-prefix
-suffix
-- Returns:
-
setEmptyValue
public Joiner setEmptyValue(CharSequence emptyValue)
Sets the empty value.- Parameters:
emptyValue
-- Returns:
-
trim
public Joiner trim(boolean trim)
- Parameters:
trim
-- Returns:
-
skipNull
public Joiner skipNull(boolean skipNull)
Ignore thenull
element/value forkey/value, Map, Entity
when the specifiedelement
orvalue
isnull
if it's set totrue
.- Parameters:
skipNull
-- Returns:
-
reuseCachedBuffer
public Joiner reuseCachedBuffer(boolean useCachedBuffer)
Improving performance by setuseCachedBuffer=true
, and must remember to calltoString()/map()/mapIfNotEmpty()/stream()/streamIfNotEmpty()
orclose()
to recycle the cached buffer.- Parameters:
useCachedBuffer
-- Returns:
-
append
public Joiner append(boolean element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(char element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(int element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(long element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(float element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(double element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(CharSequence element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(CharSequence element, int start, int end)
- Parameters:
element
-start
-end
-- Returns:
-
append
public Joiner append(StringBuffer element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(char[] element)
- Parameters:
element
-- Returns:
-
append
public Joiner append(char[] element, int offset, int len)
- Parameters:
element
-offset
-len
-- Returns:
-
appendIfNotNull
public Joiner appendIfNotNull(Object element)
Append if not null.- Parameters:
element
-- Returns:
-
appendIf
public Joiner appendIf(boolean b, Supplier<?> supplier)
- Parameters:
b
-supplier
-- Returns:
-
appendAll
public Joiner appendAll(boolean[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(boolean[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(char[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(char[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(byte[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(byte[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(short[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(short[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(int[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(int[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(long[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(long[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(float[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(float[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(double[] a)
- Parameters:
a
-- Returns:
-
appendAll
public Joiner appendAll(double[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(Object[] a, int fromIndex, int toIndex)
- Parameters:
a
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(BooleanList c)
- Parameters:
c
-- Returns:
-
appendAll
public Joiner appendAll(BooleanList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(CharList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(ByteList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(ShortList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(IntList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(LongList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(FloatList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(DoubleList c)
- Parameters:
c
-- Returns:
-
appendAll
public Joiner appendAll(DoubleList c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendAll
public Joiner appendAll(Collection<?> c)
- Parameters:
c
-- Returns:
-
appendAll
public Joiner appendAll(Collection<?> c, int fromIndex, int toIndex)
- Parameters:
c
-fromIndex
-toIndex
-- Returns:
-
appendEntry
public Joiner appendEntry(String key, boolean value)
- Parameters:
key
-value
-- Returns:
-
appendEntry
public Joiner appendEntry(String key, CharSequence value)
- Parameters:
key
-value
-- Returns:
-
appendEntry
public Joiner appendEntry(String key, StringBuffer value)
- Parameters:
key
-value
-- Returns:
-
appendEntries
public Joiner appendEntries(Map<?,?> m, int fromIndex, int toIndex)
- Parameters:
m
-fromIndex
-toIndex
-- Returns:
-
appendEntries
public Joiner appendEntries(Object entity)
- Parameters:
entity
- entity class with getter/setter methods.- Returns:
-
merge
public Joiner merge(Joiner other)
Adds the contents of the givenStringJoiner
without prefix and suffix as the next element if it is non-empty. If the givenStringJoiner
is empty, the call has no effect.A
StringJoiner
is empty ifadd()
has never been called, and ifmerge()
has never been called with a non-emptyStringJoiner
argument.If the other
StringJoiner
is using a different delimiter, then elements from the otherStringJoiner
are concatenated with that delimiter and the result is appended to thisStringJoiner
as a single element.- Parameters:
other
- TheStringJoiner
whose contents should be merged into this one- Returns:
- This
StringJoiner
- Throws:
NullPointerException
- if the otherStringJoiner
is null
-
length
public int length()
- Returns:
-
toString
public String toString()
Returns the current value, consisting of theprefix
, the values added so far separated by thedelimiter
, and thesuffix
, unless no elements have been added in which case, theprefix + suffix
or theemptyValue
characters are returnedThe underline
StringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.
-
map
public <T,E extends Exception> T map(Throwables.Function<? super String,T,E> mapper) throws E extends Exception
The underline
StringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.- Type Parameters:
T
-E
-- Parameters:
mapper
-- Returns:
- Throws:
E
- the eE extends Exception
-
mapIfNotEmpty
public <T,E extends Exception> u.Nullable<T> mapIfNotEmpty(Throwables.Function<? super String,T,E> mapper) throws E extends Exception
Callmapper
only if at least one element/object/entry is appended.The underline
StringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.- Type Parameters:
T
-E
-- Parameters:
mapper
-- Returns:
- Throws:
E
- the eE extends Exception
-
stream
public Stream<String> stream()
The underline
StringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.- Returns:
-
streamIfNotEmpty
public Stream<String> streamIfNotEmpty()
Returns a stream with the String value generated bytoString()
if at least one element/object/entry is appended, otherwise an emptyStream
is returned.The underline
StringBuilder
will be recycled after this method is called ifresueStringBuilder
is set totrue
, and should not continue to this instance.- Returns:
-
close
public void close()
Close.- Specified by:
close
in interfaceAutoCloseable
-
-