Class TextBuilder

java.lang.Object
com.pnfsoftware.jeb.util.format.TextBuilder
All Implemented Interfaces:
Appendable
Direct Known Subclasses:
XmlBuilder

public class TextBuilder extends Object implements Appendable
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 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 level
      initialCapacity - 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

      public TextBuilder indent(boolean followedByEol)
      Increase the indentation level.
      Parameters:
      followedByEol - true to append a line separator after changing indentation
      Returns:
      this builder
    • indent

      public TextBuilder indent()
      Increase the indentation level.
      Returns:
      this builder
    • unindent

      public TextBuilder unindent()
      Decrease the indentation level.
      Returns:
      this builder
      Throws:
      IllegalStateException - if the indentation level is already zero
    • append

      public TextBuilder append(CharSequence csq)
      Specified by:
      append in interface Appendable
    • append

      public TextBuilder append(CharSequence csq, int start, int end)
      Specified by:
      append in interface Appendable
    • append

      public TextBuilder append(char v)
      Specified by:
      append in interface Appendable
    • append

      public TextBuilder append(byte v)
      Append a byte value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(short v)
      Append a short value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(int v)
      Append an integer value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(long v)
      Append a long value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(float v)
      Append a float value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(double v)
      Append a double value.
      Parameters:
      v - value to append
      Returns:
      this builder
    • append

      public TextBuilder append(Object o)
      Append the string representation of an object.
      Parameters:
      o - object to append; null is rendered as "null"
      Returns:
      this builder
    • append

      public TextBuilder append(String format, Object... args)
      Append formatted text.
      Parameters:
      format - format string
      args - format arguments
      Returns:
      this builder
    • appendLine

      public TextBuilder appendLine(String format, Object... args)
      Append formatted text followed by a line separator.
      Parameters:
      format - format string
      args - format arguments
      Returns:
      this builder
    • appendLine

      public TextBuilder appendLine(boolean noIndent)
      Append a line separator.
      Parameters:
      noIndent - true to suppress automatic indentation for the empty line
      Returns:
      this builder
    • appendLine

      public TextBuilder appendLine()
      Append a line separator.
      Returns:
      this builder
    • eol

      public TextBuilder eol()
      Append a line separator.
      Returns:
      this builder
    • space

      public TextBuilder 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, inclusive
      end - 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

      public String toString()
      Overrides:
      toString in class Object