Class CodeWriter
java.lang.Object
com.northconcepts.datapipeline.sourcecode.CodeWriter
A programming-language agnostic class for emitting formatted code using indentation and line breaks.
When the
pretty flag is turned off, indentation and new lines are collapsed into nonPrettyLineSeparator (defaults to a single space).-
Constructor Summary
ConstructorsConstructorDescriptionCreates a writer that keeps the code in memory forgetSourceString().CodeWriter(OutputStream out) Creates a writer for the stream, encoding text with the platform's default charset.CodeWriter(Writer writer) -
Method Summary
Modifier and TypeMethodDescriptionclose()Flushes and closes the underlying writer or stream.flush()Returns the text written once per indent level at the start of each line (defaults to two spaces).intReturns the current indent level: how many times the indentation is written at the start of each line.longReturns the number of lines ended by theprintlnmethods.Returns the text written between lines when pretty printing is off (defaults to a single space).Returns the code written so far; only supported by writers created with the no-argument constructor.indent()Increases the indent level by one for the lines that follow.booleanisPretty()Indicates if output is split into indented lines instead of joined by the non-pretty separator (default is true).newLine(boolean force) Writes a raw line break if pretty printing is on orforceis true; the line count and indentation state are unchanged.outdent()Decreases the indent level by one; throwsIllegalStateExceptionif it is already zero.print()Writes just the pending prefix of a newly started line (its indentation, or the separator when not pretty).Writessliterally, withoutString.formatprocessing; text starting a line is indented first.Writess(run throughString.formatwhen arguments are given), indenting text that starts a line.printifNotEmpty(String s, Object... args) Printsslikeprint(String, Object...)unless it is null or only whitespace.println()Writessliterally, withoutString.formatprocessing, and ends the line.Writes the text likeprint(String, Object...)and ends the line; the line break itself is only written when pretty printing.printlnif(boolean condition) printlnifNotEmpty(String s, Object... args) Printssand ends the line likeprintln(String, Object...)unless it is null or only whitespace.setFormat(boolean pretty) Deprecated.setIndentation(String indentation) Sets the text written once per indent level at the start of each line (defaults to two spaces).setIndentCount(int indentCount) Sets the current indent level: how many times the indentation is written at the start of each line.setNonPrettyLineSeparator(String nonPrettyLineSeparator) Sets the text written between lines when pretty printing is off (defaults to a single space).setPretty(boolean pretty) Indicates if output is split into indented lines instead of joined by the non-pretty separator (default is true).Deprecated.useprint(String)Deprecated.useprintln()
-
Constructor Details
-
CodeWriter
public CodeWriter()Creates a writer that keeps the code in memory forgetSourceString(). -
CodeWriter
Creates a writer for the stream, encoding text with the platform's default charset. -
CodeWriter
-
-
Method Details
-
setFormat
Deprecated. -
isPretty
public boolean isPretty()Indicates if output is split into indented lines instead of joined by the non-pretty separator (default is true). -
setPretty
Indicates if output is split into indented lines instead of joined by the non-pretty separator (default is true). -
getNonPrettyLineSeparator
Returns the text written between lines when pretty printing is off (defaults to a single space). -
setNonPrettyLineSeparator
Sets the text written between lines when pretty printing is off (defaults to a single space). -
getIndentation
Returns the text written once per indent level at the start of each line (defaults to two spaces). -
setIndentation
Sets the text written once per indent level at the start of each line (defaults to two spaces). -
getIndentCount
public int getIndentCount()Returns the current indent level: how many times the indentation is written at the start of each line. -
setIndentCount
Sets the current indent level: how many times the indentation is written at the start of each line. -
indent
Increases the indent level by one for the lines that follow. -
getLineCount
public long getLineCount()Returns the number of lines ended by theprintlnmethods. -
outdent
Decreases the indent level by one; throwsIllegalStateExceptionif it is already zero. -
print
Writess(run throughString.formatwhen arguments are given), indenting text that starts a line. -
printif
-
printifNotEmpty
Printsslikeprint(String, Object...)unless it is null or only whitespace. -
write
Deprecated.useprint(String) -
print
Writessliterally, withoutString.formatprocessing; text starting a line is indented first. -
printif
-
print
Writes just the pending prefix of a newly started line (its indentation, or the separator when not pretty). -
newLine
Writes a raw line break if pretty printing is on orforceis true; the line count and indentation state are unchanged. -
println
Writes the text likeprint(String, Object...)and ends the line; the line break itself is only written when pretty printing. -
printlnif
-
printlnifNotEmpty
Printssand ends the line likeprintln(String, Object...)unless it is null or only whitespace. -
println
Writessliterally, withoutString.formatprocessing, and ends the line. -
printlnif
-
printlnif
-
writeNewLine
Deprecated.useprintln() -
println
-
flush
-
close
Flushes and closes the underlying writer or stream. -
getSourceString
Returns the code written so far; only supported by writers created with the no-argument constructor.
-
setPretty(boolean)