org.tbull.util.dev
Class IndentableLineBuffer

Object
  extended by org.tbull.util.dev.IndentableLineBuffer
All Implemented Interfaces:
Appendable, Cloneable, DataDumpable

public class IndentableLineBuffer
extends Object
implements Cloneable, Appendable, DataDumpable

Provides a line buffer of individually indentable lines. Maintains the notion of a current line. Once the current line is dismissed (by beginning a new line), the line is no longer individually accessible, and its contents (text and, for the most part, the individual indent level [REALLY?]) are considered immutable for user operations. Detail on indent levels: base [indent] level individual [indent] level resulting [indent] level default [individual] [line] [indent] level If the resulting indent level is less than 0, it is assumed to be 0. add/appendLines(): The notion is, that the sub-buffer is *integrated* into the main buffer, thus the sub-buffer is expected to be dismissed thereafter. Therefore it does not hurt to screw it up (by messing with the indent levels). Use clone(), if you don't like it. Unless otherwise documented, all methods return this same object to facilitate [BANDWURM invocations (hibernate: method chaining)]. implements java.lang.Appendable, so the buffer can be specified as the destination of a java.util.Formatter. However, the buffer will not properly deal with newlines in the appended char sequences, so don't use them. Use newLine() then, e.g. like lb.format(...).newLine().format(...)


Constructor Summary
IndentableLineBuffer()
          Constructs a new LineBuffer, initially consisting of no lines (empty).
IndentableLineBuffer(int base_indent_level)
          Constructs a new LineBuffer, initially empty, with the given base indent level.
IndentableLineBuffer(String s)
          Constructs a new LineBuffer, initially consisting of a line given by s.
 
Method Summary
 IndentableLineBuffer addLine(int indent, String s)
          Begins a new line with the given indent level and copies the contents of s into it.
 IndentableLineBuffer addLine(int indent, StringBuilder sb)
          Begins a new line with the given indent level and copies the contents of sb into it.
 IndentableLineBuffer addLine(String s)
          Begins a new line and copies the contents of s into it.
 IndentableLineBuffer addLine(StringBuilder sb)
          Begins a new line and copies the contents of sb into it.
 IndentableLineBuffer addLines(IndentableLineBuffer other)
          Adds the lines of the other line buffer to this one.
 IndentableLineBuffer append(char c)
          Appends the specified character to the current last line.
 IndentableLineBuffer append(CharSequence csq)
          Appends the specified character sequence to the current last line.
 IndentableLineBuffer append(CharSequence csq, int start, int end)
          Appends a subsequence of the specified character sequence to the current last line.
 IndentableLineBuffer append(String s)
          Appends the contents of s to the current last line.
 IndentableLineBuffer append(StringBuilder sb)
          Appends the contents of sb to the current last line.
 IndentableLineBuffer appendLines(IndentableLineBuffer other)
          Adds the lines of the other line buffer to this one, concatenating the [letzte und erste (adjoining?)] lines.
 IndentableLineBuffer clone()
          Creates a deep copy of this object.
 IndentableLineBuffer dumpData()
           
 IndentableLineBuffer format(Locale l, String format, Object... args)
          Append a formatted string to the current last line using the specified locale, format string, and arguments.
 IndentableLineBuffer format(String format, Object... args)
          Append a formatted string to the current last line using the specified format string and arguments.
 IndentableLineBuffer indent(int indent)
          Indents existing and future lines by modifing the base indent level.
 IndentableLineBuffer indentCurrentLine(int indent)
          Indents the current line.
 IndentableLineBuffer indentExisting(int indent)
          Indents existing lines by modifying each line's individual indent level.
 IndentableLineBuffer newLine()
          Begins a new (empty) line.
 IndentableLineBuffer newLine(int indent)
          Begins a new (empty) line with the given indent level.
 IndentableLineBuffer setBaseIndentLevel(int indent_level)
          Sets the base indent level.
 IndentableLineBuffer setDefaultLineIndentLevel(int indent_level)
          Sets the default individual line indent level.
 IndentableLineBuffer setIndentation(String indentation)
          Sets the indentation string to use on stringification.
 IndentableLineBuffer setLineSeparator(String sep)
          Sets the line separator used by toString().
 String toString()
          Returns all lines in the LineBuffer as String, separated by newlines.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndentableLineBuffer

public IndentableLineBuffer()
Constructs a new LineBuffer, initially consisting of no lines (empty).


IndentableLineBuffer

public IndentableLineBuffer(String s)
Constructs a new LineBuffer, initially consisting of a line given by s.


IndentableLineBuffer

public IndentableLineBuffer(int base_indent_level)
Constructs a new LineBuffer, initially empty, with the given base indent level.

Method Detail

dumpData

public IndentableLineBuffer dumpData()
Specified by:
dumpData in interface DataDumpable

setIndentation

public IndentableLineBuffer setIndentation(String indentation)
Sets the indentation string to use on stringification. The string represents one level of indentation. Popular values are a single tab character ("\t") or 4 consecutive spaces.


setLineSeparator

public IndentableLineBuffer setLineSeparator(String sep)
Sets the line separator used by toString().


setBaseIndentLevel

public IndentableLineBuffer setBaseIndentLevel(int indent_level)
Sets the base indent level.


setDefaultLineIndentLevel

public IndentableLineBuffer setDefaultLineIndentLevel(int indent_level)
Sets the default individual line indent level.


indent

public IndentableLineBuffer indent(int indent)
Indents existing and future lines by modifing the base indent level. This affects both, already existing as well as subsequently added lines.


indentExisting

public IndentableLineBuffer indentExisting(int indent)
Indents existing lines by modifying each line's individual indent level.


indentCurrentLine

public IndentableLineBuffer indentCurrentLine(int indent)
Indents the current line.


newLine

public IndentableLineBuffer newLine()
Begins a new (empty) line.


newLine

public IndentableLineBuffer newLine(int indent)
Begins a new (empty) line with the given indent level.


append

public IndentableLineBuffer append(String s)
Appends the contents of s to the current last line.


append

public IndentableLineBuffer append(StringBuilder sb)
Appends the contents of sb to the current last line.


append

public IndentableLineBuffer append(char c)
Appends the specified character to the current last line. This is part of the java.lang.Appendable interface, which enables the IndentableLineBuffer to be specified as the destination of a Formatter.

Specified by:
append in interface Appendable

append

public IndentableLineBuffer append(CharSequence csq)
Appends the specified character sequence to the current last line. This is part of the java.lang.Appendable interface, which enables the IndentableLineBuffer to be specified as the destination of a Formatter.

Specified by:
append in interface Appendable

append

public IndentableLineBuffer append(CharSequence csq,
                                   int start,
                                   int end)
Appends a subsequence of the specified character sequence to the current last line. This is part of the java.lang.Appendable interface, which enables the IndentableLineBuffer to be specified as the destination of a Formatter.

Specified by:
append in interface Appendable

format

public IndentableLineBuffer format(String format,
                                   Object... args)
                            throws IllegalFormatException,
                                   FormatterClosedException
Append a formatted string to the current last line using the specified format string and arguments. Constructs a Formatter, whose output is connected to this IndentableLineBuffer, and passes all arguments to its Formatter.format(String, Object...) method. See there for a description of the parameters and the format string.

Throws:
IllegalFormatException
FormatterClosedException

format

public IndentableLineBuffer format(Locale l,
                                   String format,
                                   Object... args)
                            throws IllegalFormatException,
                                   FormatterClosedException
Append a formatted string to the current last line using the specified locale, format string, and arguments. Constructs a Formatter, whose output is connected to this IndentableLineBuffer, and passes all arguments to its Formatter.format(Locale, String, Object...) method. See there for a description of the parameters and the format string.

Throws:
IllegalFormatException
FormatterClosedException

addLine

public IndentableLineBuffer addLine(String s)
Begins a new line and copies the contents of s into it. The individual indent level of the new line is take from the default indent level.


addLine

public IndentableLineBuffer addLine(int indent,
                                    String s)
Begins a new line with the given indent level and copies the contents of s into it.


addLine

public IndentableLineBuffer addLine(StringBuilder sb)
Begins a new line and copies the contents of sb into it. The individual indent level of the new line is take from the default indent level.


addLine

public IndentableLineBuffer addLine(int indent,
                                    StringBuilder sb)
Begins a new line with the given indent level and copies the contents of sb into it.


addLines

public IndentableLineBuffer addLines(IndentableLineBuffer other)
Adds the lines of the other line buffer to this one. The individual lines are /not/ copied, they are taken over. Each line is assigned a new individual indent level, made up of its former resulting (base + individual) indent level. The new resulting indent level then is the sum of the new individual level plus this buffer's base level. This buffer's default level is not taken into account.
new resulting = this base + new individual
              = this base + former resulting
              = this base + other base + former individual
After this operation the other buffer is pretty messed up. The idea is, the other buffer is merged into this one and then forgotten. If you don't like this, clone() the buffer in advance.


appendLines

public IndentableLineBuffer appendLines(IndentableLineBuffer other)
Adds the lines of the other line buffer to this one, concatenating the [letzte und erste (adjoining?)] lines. Works much like addLines(IndentableLineBuffer), except that the first line of the other line buffer is appended to the current last line of this line buffer, ignoring the indent level of the other line. The individual lines are /not/ copied, they are taken over. The other buffer has to be considered destroyed after this operation. If you don't like this, clone() the buffer in advance.


toString

public String toString()
Returns all lines in the LineBuffer as String, separated by newlines. The last line has a newline appended, too.

Overrides:
toString in class Object

clone

public IndentableLineBuffer clone()
Creates a deep copy of this object. The clone is completely independent of this line buffer object.

Overrides:
clone in class Object
Returns:
The newly created clone.