org.eclipse.xtext.xtend2.lib
Class StringConcatenation

java.lang.Object
  extended by org.eclipse.xtext.xtend2.lib.StringConcatenation
All Implemented Interfaces:
java.lang.CharSequence

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.

Author:
Sebastian Zarnekow - Initial contribution and API

Field Summary
static java.lang.String DEFAULT_LINE_DELIMITER
           
 
Constructor Summary
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
 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(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> getSignificantContent()
          Compute the significant content of this sequence.
 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 lineDelimiter.
 java.lang.CharSequence subSequence(int start, int end)
           Only the significant content of this sequence is considered.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_LINE_DELIMITER

public static final java.lang.String DEFAULT_LINE_DELIMITER
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 is null or the empty String.
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 is null.

Parameters:
object - the to-be-appended object.

append

protected void append(java.lang.Object object,
                      int index)
Add the string representation of the given object to this sequence at the given index. Does nothing if the object is null.

Parameters:
object - the to-be-appended object.
index - the index in the list of segments.

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:
lineDelimiter - the used delimiter.
indentation - the indentation string that should be prepended. May not be null.

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 be null.
index - the index in the list of segments.

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 be null.

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

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 interface java.lang.CharSequence
Overrides:
toString in class java.lang.Object

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.


length

public int length()

Only the significant content of this sequence is considered.

Specified by:
length in interface java.lang.CharSequence

charAt

public char charAt(int index)

Only the significant content of this sequence is considered.

Specified by:
charAt in interface java.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 interface java.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 lineDelimiter. Existing line-breaks in the text will be replaced by this's instances delimiter.

Parameters:
text - the to-be-splitted text. May be null.
Returns:
a list of segments. Is never null.