Package com.pnfsoftware.jeb.util.format
Class TextBuilder
java.lang.Object
com.pnfsoftware.jeb.util.format.TextBuilder
- All Implemented Interfaces:
Appendable
- Direct Known Subclasses:
XmlBuilder
A text buffer builder supporting indentation. Can be seen as an alternative to
StringBuilder to easily create structured documents.
This class may be sub-classed, in particular, refer to XmlBuilder for XML documents.
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a builder with a default indentation size of 4 characters and an initial capacity of 16 characters.TextBuilder(int indentationSize) Create a builder with an initial capacity of 16 characters.TextBuilder(int indentationSize, int initialCapacity) Create a builder. -
Method Summary
Modifier and TypeMethodDescriptionappend(byte v) Append a byte value.append(char v) append(double v) Append a double value.append(float v) Append a float value.append(int v) Append an integer value.append(long v) Append a long value.append(short v) Append a short value.append(CharSequence csq) append(CharSequence csq, int start, int end) Append the string representation of an object.Append formatted text.Append a line separator.appendLine(boolean noIndent) Append a line separator.appendLine(String format, Object... args) Append formatted text followed by a line separator.voidclear()Clear the buffer content while preserving indentation settings.eol()Append a line separator.indent()Increase the indentation level.indent(boolean followedByEol) Increase the indentation level.intlength()Get the current number of characters in the buffer.voidremoveChar(int pos) Remove one character from the buffer.voidremoveChars(int start, int end) Remove a range of characters from the buffer.voidRemove the last character from the buffer.voidRemove the last line from the buffer.space()Append one space character.toString()unindent()Decrease the indentation level.voidupdateIndentation(boolean doIndent) Increase or decrease the current indentation level.
-
Constructor Details
-
TextBuilder
public TextBuilder()Create a builder with a default indentation size of 4 characters and an initial capacity of 16 characters. -
TextBuilder
public TextBuilder(int indentationSize) Create a builder with an initial capacity of 16 characters.- Parameters:
indentationSize- number of spaces per indentation level
-
TextBuilder
public TextBuilder(int indentationSize, int initialCapacity) Create a builder.- Parameters:
indentationSize- number of spaces per indentation levelinitialCapacity- initial capacity of the underlying character buffer
-
-
Method Details
-
length
public int length()Get the current number of characters in the buffer.- Returns:
- the current buffer length
-
clear
public void clear()Clear the buffer content while preserving indentation settings. -
updateIndentation
public void updateIndentation(boolean doIndent) Increase or decrease the current indentation level.- Parameters:
doIndent- true to indent, false to unindent
-
indent
Increase the indentation level.- Parameters:
followedByEol- true to append a line separator after changing indentation- Returns:
- this builder
-
indent
Increase the indentation level.- Returns:
- this builder
-
unindent
Decrease the indentation level.- Returns:
- this builder
- Throws:
IllegalStateException- if the indentation level is already zero
-
append
- Specified by:
appendin interfaceAppendable
-
append
- Specified by:
appendin interfaceAppendable
-
append
- Specified by:
appendin interfaceAppendable
-
append
Append a byte value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append a short value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append an integer value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append a long value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append a float value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append a double value.- Parameters:
v- value to append- Returns:
- this builder
-
append
Append the string representation of an object.- Parameters:
o- object to append; null is rendered as"null"- Returns:
- this builder
-
append
Append formatted text.- Parameters:
format- format stringargs- format arguments- Returns:
- this builder
-
appendLine
Append formatted text followed by a line separator.- Parameters:
format- format stringargs- format arguments- Returns:
- this builder
-
appendLine
Append a line separator.- Parameters:
noIndent- true to suppress automatic indentation for the empty line- Returns:
- this builder
-
appendLine
Append a line separator.- Returns:
- this builder
-
eol
Append a line separator.- Returns:
- this builder
-
space
Append one space character.- Returns:
- this builder
-
removeChar
public void removeChar(int pos) Remove one character from the buffer.- Parameters:
pos- position of the character to remove
-
removeChars
public void removeChars(int start, int end) Remove a range of characters from the buffer.- Parameters:
start- start index, inclusiveend- end index, exclusive
-
removeLastChar
public void removeLastChar()Remove the last character from the buffer. -
removeLastLine
public void removeLastLine()Remove the last line from the buffer. -
toString
-