Class IndentingStringBuilder

  • Direct Known Subclasses:
    PlainStringBuilder, PrependingStringBuilder, PrettyStringBuilder

    public abstract class IndentingStringBuilder
    extends java.lang.Object
    Abstract class for building wrappers around StringBuilder that can be used to pre-format pretty-printed output for debugging purposes. The IndentingStringBuilder class is defined in such a way as to support writing indented output to a StringBuilder, providing primitives to indent and outdent. Derived classes such as the PrettyStringBuilder honor the spacing, which the PlainStringBuilder ignore it, and all requests for whitespace (such as nextLine()), result in just a single space begin appended to the String. The IndentingStringBuilder interface has a fluent-style definition, allowing it to be used repeatedly in expressions.
    • Field Detail

      • sb

        protected final java.lang.StringBuilder sb
    • Constructor Detail

      • IndentingStringBuilder

        public IndentingStringBuilder​(java.lang.StringBuilder sb)
        Constructor.
        Parameters:
        sb - the StringBuilder to write to
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • length

        public int length()
        Return the length of the String written so far.
        Returns:
        the length of the String written so far
      • append

        public IndentingStringBuilder append​(java.lang.String v)
        Append a String.
        Parameters:
        v - the string to append
        Returns:
        the IndentingStringBuilder
      • append

        public IndentingStringBuilder append​(char v)
        Append a char.
        Parameters:
        v - the char to append
        Returns:
        the IndentingStringBuilder
      • append

        public IndentingStringBuilder append​(long v)
        Append a long value, as text.
        Parameters:
        v - the long to append
        Returns:
        the IndentingStringBuilder
      • nextLine

        public abstract IndentingStringBuilder nextLine()
        Emit a newline, along with any necessary indentation.
        Returns:
        this
      • pushIndent

        public abstract IndentingStringBuilder pushIndent()
        Add a new level of indentation. This has no effect until the next call to nextLine().
        Returns:
        this
      • popIndent

        public abstract IndentingStringBuilder popIndent()
        Remove a level of indentation. This has no effect until the next call to nextLine(). It is an error to unindent when there are no levels of indentation active; this is indicated with a call to addComment(String) with a warning that the caller has unindented too much.
        Returns:
        this
      • addComment

        public abstract IndentingStringBuilder addComment​(java.lang.String comment)
        Add a c-style comment. The provided text is emitted surrounded by "/o " and " o/" (where the o's are asterisks). The comment will only appear in "pretty text." For plain text, this emits a single blank.
        Parameters:
        comment - the comment
        Returns: