Class StringConcatenation
- java.lang.Object
-
- org.eclipse.xtend2.lib.StringConcatenation
-
- All Implemented Interfaces:
java.lang.CharSequence
@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 ofStringConcatenation
. There are overloaded variants ofappend(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 thisStringConcatenation
.The
append
-operation ignoresnull
arguments. This is different to whatStringBuffer
orStringBuilder
do and that's the reason why theAppendable
interface is not fulfilled by aStringConcatenation
.The object uses an internal
List
ofStrings
that is concatenated on demand to a complete sequence. UsetoString()
to get the joined version of aStringConcatenation
.Object.equals(Object)
andObject.hashCode()
are not specialized for aStringConcatenation
, that is, the semantics are based on identity similar to whatStringBuffer
andStringBuilder
do.- Since:
- 2.3
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_LINE_DELIMITER
The default line delimiter that is used by instances ofStringConcatenation
.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
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
append(java.lang.String str)
Append the given string to this sequence.void
append(java.lang.String str, java.lang.String indentation)
Add the given string to this sequence.void
append(StringConcatenation concat)
Append the contents of a given StringConcatenation to this sequence.void
append(StringConcatenationClient client)
Append the contents of a given StringConcatenationClient to this sequence.void
append(StringConcatenationClient client, java.lang.String indentation)
Append the contents of a given StringConcatenationClient to this sequence.void
append(StringConcatenation concat, java.lang.String indentation)
Append the contents of a given StringConcatenation to this sequence.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)
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()
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)
java.lang.String
toString()
-
-
-
Field Detail
-
DEFAULT_LINE_DELIMITER
public static final java.lang.String DEFAULT_LINE_DELIMITER
The default line delimiter that is used by instances ofStringConcatenation
. It usesSystem.getProperty("line.separator")
.- Since:
- 2.3
-
-
Constructor Detail
-
StringConcatenation
public StringConcatenation()
Create a new concatenation that uses the system line delimiter.- See Also:
System.getProperties()
,System.getProperty(String)
-
StringConcatenation
public StringConcatenation(java.lang.String lineDelimiter)
Create a new concatenation with the specified delimiter.- Parameters:
lineDelimiter
- the used delimiter.- Throws:
java.lang.IllegalArgumentException
- if the lineDelimiter isnull
or the empty String.- Since:
- 2.3
-
-
Method Detail
-
append
public void append(java.lang.Object object)
Append the string representation of the given object to this sequence. Does nothing if the object isnull
.- Parameters:
object
- the to-be-appended object.
-
append
public void append(java.lang.String str)
Append the given string to this sequence. Does nothing if the string isnull
.- Parameters:
str
- the to-be-appended string.- Since:
- 2.11
-
append
public void append(StringConcatenation concat)
Append the contents of a given StringConcatenation to this sequence. Does nothing if the concatenation isnull
.- Parameters:
concat
- the to-be-appended StringConcatenation.- Since:
- 2.11
-
append
public void append(StringConcatenationClient client)
Append the contents of a given StringConcatenationClient to this sequence. Does nothing if the argument isnull
.- Parameters:
client
- the to-be-appended StringConcatenationClient.- Since:
- 2.11
-
append
protected void append(java.lang.Object object, int index)
-
append
public void append(java.lang.Object object, java.lang.String indentation)
Add the string representation of the given object to this sequence. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
object
- the appended object.indentation
- the indentation string that should be prepended. May not benull
.
-
append
public void append(java.lang.String str, java.lang.String indentation)
Add the given string to this sequence. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
str
- the appended string.indentation
- the indentation string that should be prepended. May not benull
.- Since:
- 2.11
-
append
public void append(StringConcatenation concat, java.lang.String indentation)
Append the contents of a given StringConcatenation to this sequence. Does nothing if the concatenation isnull
. The given indentation will be prepended to each line except the first one.- Parameters:
concat
- the to-be-appended StringConcatenation.indentation
- the indentation string that should be prepended. May not benull
.- Since:
- 2.11
-
append
public void append(StringConcatenationClient client, java.lang.String indentation)
Append the contents of a given StringConcatenationClient to this sequence. Does nothing if that argument isnull
. The given indentation will be prepended to each line except the first one.- Parameters:
client
- the to-be-appended StringConcatenationClient.indentation
- the indentation string that should be prepended. May not benull
.- Since:
- 2.11
-
append
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. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
object
- the to-be-appended object.indentation
- the indentation string that should be prepended. May not benull
.index
- the index in the list of segments.
-
getStringRepresentation
protected java.lang.String getStringRepresentation(java.lang.Object object)
Computes the string representation of the given object. The default implementation will just invokeObject.toString()
but clients may override and specialize the logic.- Parameters:
object
- the object that shall be appended. Nevernull
.- Returns:
- the string representation. May not be
null
. - Since:
- 2.5
-
appendImmediate
public void appendImmediate(java.lang.Object object, java.lang.String indentation)
Add the string representation of the given object to this sequence immediately. That is, all the trailing whitespace of this sequence will be ignored and the string is appended directly after the last segment that contains something besides whitespace. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
object
- the to-be-appended object.indentation
- the indentation string that should be prepended. May not benull
.
-
appendSegments
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. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
indentation
- the indentation string that should be prepended. May not benull
.index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not benull
.otherDelimiter
- the line delimiter that was used in the otherSegments list.
-
appendSegments
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. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not benull
.otherDelimiter
- the line delimiter that was used in the otherSegments list.- Since:
- 2.5
-
appendSegments
protected void appendSegments(int index, java.util.List<java.lang.String> otherSegments)
Add the list of segments to this sequence at the given index. The given indentation will be prepended to each line except the first one if the object has a multi-line string representation.- Parameters:
index
- the index in this instance's list of segments.otherSegments
- the to-be-appended segments. May not benull
.- Since:
- 2.5
-
newLine
public void newLine()
Add a newline to this sequence according to the configured lineDelimiter.
-
newLineIfNotEmpty
public void newLineIfNotEmpty()
Add a newline to this sequence according to the configured lineDelimiter if the last line contains something besides whitespace.
-
toString
public java.lang.String toString()
- Specified by:
toString
in interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
-
getContent
protected final java.util.List<java.lang.String> getContent()
Return the actual content of this sequence, including all trailing whitespace. The return value is unsafe, that is modification to thisStringConcatenation
will cause changes in a previously obtained result and vice versa.- Returns:
- the actual content of this instance. Never
null
. - Since:
- 2.8
-
getSignificantContent
protected java.util.List<java.lang.String> getSignificantContent()
Compute the significant content of this sequence. That is, trailing whitespace after the last line-break will be ignored if the last line contains only whitespace. The return value is unsafe, that is modification to thisStringConcatenation
will cause changes in a previously obtained result and vice versa.- Returns:
- the significant content of this instance. Never
null
.
-
getLineDelimiter
protected java.lang.String getLineDelimiter()
Allows subtypes to access the configured line delimiter.- Returns:
- the line delimiter
- Since:
- 2.5
-
length
public int length()
Only the significant content of this sequence is considered.
- Specified by:
length
in interfacejava.lang.CharSequence
-
charAt
public char charAt(int index)
Only the significant content of this sequence is considered.
- Specified by:
charAt
in interfacejava.lang.CharSequence
-
subSequence
public java.lang.CharSequence subSequence(int start, int end)
Only the significant content of this sequence is considered.
- Specified by:
subSequence
in interfacejava.lang.CharSequence
-
splitLinesAndNewLines
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. Existing line-breaks in the text will be replaced by this's instances delimiter.- Parameters:
text
- the to-be-splitted text. May benull
.- Returns:
- a list of segments. Is never
null
.
-
-