@GwtCompatible
public class StringConcatenation
extends java.lang.Object
implements java.lang.CharSequence
A StringConcatenation
allows for efficient, indentation aware concatenation of character sequences.
In addition to the methods that are specified by the implemented interface CharSequence
, there are some other
public operations that allow to modify the contents of this sequence. The string representation of arbitrary objects
can be appended to an instance of StringConcatenation
. There are overloaded variants of
append(Object, String)
that allow to pass an indentation that should be applied to each line of the appended
content. Each line break that is part of the new content will be replaced by the line delimiter that was configured
for this StringConcatenation
.
The append
-operation ignores null
arguments. This is different to what
StringBuffer
or StringBuilder
do and that's the reason why the Appendable
interface is not
fulfilled by a StringConcatenation
.
The object uses an internal List
of Strings
that is concatenated on demand to a complete
sequence. Use toString()
to get the joined version of a StringConcatenation
.
Object.equals(Object)
and Object.hashCode()
are not specialized for a StringConcatenation
, that is, the
semantics are based on identity similar to what StringBuffer
and StringBuilder
do.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_LINE_DELIMITER
The default line delimiter that is used by instances of
StringConcatenation . |
Constructor and Description |
---|
StringConcatenation()
Create a new concatenation that uses the system line delimiter.
|
StringConcatenation(java.lang.String lineDelimiter)
Create a new concatenation with the specified delimiter.
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.Object object)
Append the string representation of the given object to this sequence.
|
protected void |
append(java.lang.Object object,
int index)
Add the string representation of the given object to this sequence at the given index.
|
void |
append(java.lang.Object object,
java.lang.String indentation)
Add the string representation of the given object to this sequence.
|
protected void |
append(java.lang.Object object,
java.lang.String indentation,
int index)
Add the string representation of the given object to this sequence at the given index.
|
void |
appendImmediate(java.lang.Object object,
java.lang.String indentation)
Add the string representation of the given object to this sequence immediately.
|
protected void |
appendSegments(int index,
java.util.List<java.lang.String> otherSegments)
Add the list of segments to this sequence at the given index.
|
protected void |
appendSegments(int index,
java.util.List<java.lang.String> otherSegments,
java.lang.String otherDelimiter)
Add the list of segments to this sequence at the given index.
|
protected void |
appendSegments(java.lang.String indentation,
int index,
java.util.List<java.lang.String> otherSegments,
java.lang.String otherDelimiter)
Add the list of segments to this sequence at the given index.
|
char |
charAt(int index)
Only the significant content of this sequence is considered.
|
protected java.util.List<java.lang.String> |
getContent()
Return the actual content of this sequence, including all trailing whitespace.
|
protected java.lang.String |
getLineDelimiter()
Allows subtypes to access the configured line delimiter.
|
protected java.util.List<java.lang.String> |
getSignificantContent()
Compute the significant content of this sequence.
|
protected java.lang.String |
getStringRepresentation(java.lang.Object object)
Computes the string representation of the given object.
|
int |
length()
Only the significant content of this sequence is considered.
|
void |
newLine()
Add a newline to this sequence according to the configured lineDelimiter.
|
void |
newLineIfNotEmpty()
Add a newline to this sequence according to the configured lineDelimiter if the last line contains
something besides whitespace.
|
protected java.util.List<java.lang.String> |
splitLinesAndNewLines(java.lang.String text)
Return a list of segments where each segment is either the content of a line in the given text or a line-break
according to the configured delimiter.
|
java.lang.CharSequence |
subSequence(int start,
int end)
Only the significant content of this sequence is considered.
|
java.lang.String |
toString() |
public static final java.lang.String DEFAULT_LINE_DELIMITER
StringConcatenation
. It uses
System.getProperty("line.separator")
.public StringConcatenation()
System.getProperties()
,
System.getProperty(String)
public StringConcatenation(java.lang.String lineDelimiter)
lineDelimiter
- the used delimiter.java.lang.IllegalArgumentException
- if the lineDelimiter is null
or the empty String.public void append(java.lang.Object object)
null
.object
- the to-be-appended object.protected void append(java.lang.Object object, int index)
null
.object
- the to-be-appended object.index
- the index in the list of segments.public void append(java.lang.Object object, java.lang.String indentation)
object
- the appended object.indentation
- the indentation string that should be prepended. May not be null
.protected void append(java.lang.Object object, java.lang.String indentation, int index)
object
- the to-be-appended object.indentation
- the indentation string that should be prepended. May not be null
.index
- the index in the list of segments.protected java.lang.String getStringRepresentation(java.lang.Object object)
Object.toString()
but clients may override and specialize
the logic.object
- the object that shall be appended. Never null
.null
.public void appendImmediate(java.lang.Object object, java.lang.String indentation)
object
- the to-be-appended object.indentation
- the indentation string that should be prepended. May not be null
.protected void appendSegments(java.lang.String indentation, int index, java.util.List<java.lang.String> otherSegments, java.lang.String otherDelimiter)
indentation
- the indentation string that should be prepended. May not be null
.index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not be null
.otherDelimiter
- the line delimiter that was used in the otherSegments list.protected void appendSegments(int index, java.util.List<java.lang.String> otherSegments, java.lang.String otherDelimiter)
index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not be null
.otherDelimiter
- the line delimiter that was used in the otherSegments list.protected void appendSegments(int index, java.util.List<java.lang.String> otherSegments)
index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not be null
.public void newLine()
public void newLineIfNotEmpty()
public java.lang.String toString()
toString
in interface java.lang.CharSequence
toString
in class java.lang.Object
protected final java.util.List<java.lang.String> getContent()
StringConcatenation
will cause changes in a previously obtained
result and vice versa.null
.protected java.util.List<java.lang.String> getSignificantContent()
StringConcatenation
will cause changes in a previously obtained result and vice versa.null
.protected java.lang.String getLineDelimiter()
public int length()
Only the significant content of this sequence is considered.
length
in interface java.lang.CharSequence
public char charAt(int index)
Only the significant content of this sequence is considered.
charAt
in interface java.lang.CharSequence
public java.lang.CharSequence subSequence(int start, int end)
Only the significant content of this sequence is considered.
subSequence
in interface java.lang.CharSequence
protected java.util.List<java.lang.String> splitLinesAndNewLines(java.lang.String text)
text
- the to-be-splitted text. May be null
.null
.