public final class Document
extends java.lang.Object
One of the main goals with this class, is to create String
s containing well-formatted text, that can subsequently be written to disk etc.
To write a Hello World program in Java, consider the following example:
Document document = new Document(); document.line("public class HelloWorld {"); document.indent(); document.line("public static void main(String[] args) {"); document.indent(); document.line("System.out.println(\"Hello, World!\");"); document.outdent(); document.line("}"); document.outdent(); document.line("}"); System.out.print(document);
This class is thread-safe and thus suitable for concurrent use without external synchronization. Although, calling any of the methods, with the exception for toString()
, from multiple Thread
s at the same
time, may produce unexpected results.
Constructor and Description |
---|
Document()
Constructs a new
Document instance. |
Modifier and Type | Method and Description |
---|---|
Document |
indent()
Performs an indentation operation on this
Document instance. |
Document |
line()
Appends an empty line of text to this
Document instance. |
Document |
line(java.lang.String textAfterIndentation)
Appends a line of text to this
Document instance. |
Document |
line(java.lang.String textAfterIndentation,
java.lang.String textBeforeIndentation)
Appends a line of text to this
Document instance. |
Document |
linef(java.lang.String textAfterIndentationFormat,
java.lang.Object... objects)
Appends a line of text to this
Document instance. |
Document |
outdent()
Performs an outdentation operation on this
Document instance. |
java.lang.String |
toString()
Returns a
String representation of this Document instance. |
public Document indent()
Document
instance.
Returns the Document
itself, such that method chaining is possible.
Document
itself, such that method chaining is possiblepublic Document line()
Document
instance.
If either textAfterIndentation
or textBeforeIndentation
contains one or more line separators, they will be split into multiple lines of text. The one of the two with most line separators will decide
how many lines to create. If one of the two has less line separators than the other, an empty String
literal will be used where no corresponding text can be found.
Calling this method is equivalent to calling line("")
.
Returns the Document
itself, such that method chaining is possible.
Document
itself, such that method chaining is possiblepublic Document line(java.lang.String textAfterIndentation)
Document
instance.
The line of text consists of textAfterIndentation
, which is appended after the indentation of the given line.
If either textAfterIndentation
or textBeforeIndentation
contains one or more line separators, they will be split into multiple lines of text. The one of the two with most line separators will decide
how many lines to create. If one of the two has less line separators than the other, an empty String
literal will be used where no corresponding text can be found.
Calling this method is equivalent to calling line(textAfterIndentation, "")
.
Returns the Document
itself, such that method chaining is possible.
If textAfterIndentation
is null
, a NullPointerException
will be thrown.
textAfterIndentation
- the text to append after the indentationDocument
itself, such that method chaining is possiblejava.lang.NullPointerException
- thrown if, and only if, textAfterIndentation
is null
public Document line(java.lang.String textAfterIndentation, java.lang.String textBeforeIndentation)
Document
instance.
The line of text consists of three parts. The first part comes from textBeforeIndentation
. The second part is the indentation itself, if any. The third part comes from textAfterIndentation
.
If either textAfterIndentation
or textBeforeIndentation
contains one or more line separators, they will be split into multiple lines of text. The one of the two with most line separators will decide
how many lines to create. If one of the two has less line separators than the other, an empty String
literal will be used where no corresponding text can be found.
Returns the Document
itself, such that method chaining is possible.
If either textAfterIndentation
or textBeforeIndentation
are null
, a NullPointerException
will be thrown.
textAfterIndentation
- the text to append after the indentationtextBeforeIndentation
- the text to prepend before the indentationDocument
itself, such that method chaining is possiblejava.lang.NullPointerException
- thrown if, and only if, either textAfterIndentation
or textBeforeIndentation
are null
public Document linef(java.lang.String textAfterIndentationFormat, java.lang.Object... objects)
Document
instance.
The line of text consists of String.format(textAfterIndentationFormat, objects)
, which is appended after the indentation of the given line.
If either textAfterIndentation
or textBeforeIndentation
contains one or more line separators, they will be split into multiple lines of text. The one of the two with most line separators will decide
how many lines to create. If one of the two has less line separators than the other, an empty String
literal will be used where no corresponding text can be found.
Calling this method is equivalent to calling line(String.format(textAfterIndentationFormat, objects))
, with the exception that null
references are not allowed.
Returns the Document
itself, such that method chaining is possible.
If either textAfterIndentationFormat
or objects
are null
, a NullPointerException
will be thrown.
textAfterIndentationFormat
- a format String
for the text to append after the indentationobjects
- an array of Object
s to use in the format String
Document
itself, such that method chaining is possiblejava.lang.NullPointerException
- thrown if, and only if, either textAfterIndentationFormat
or objects
are null
public Document outdent()
Document
instance.
Returns the Document
itself, such that method chaining is possible.
Document
itself, such that method chaining is possiblepublic java.lang.String toString()
String
representation of this Document
instance.
The String
representation is the text that you appended to it with all lines and indentations etc.
This method and the String
returned are both part of the contract for this class and will not change.
toString
in class java.lang.Object
String
representation of this Document
instance