Class CodeWriter

java.lang.Object
com.northconcepts.datapipeline.sourcecode.CodeWriter

public class CodeWriter extends Object
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 Details

    • CodeWriter

      public CodeWriter()
      Creates a writer that keeps the code in memory for getSourceString().
    • CodeWriter

      public CodeWriter(OutputStream out)
      Creates a writer for the stream, encoding text with the platform's default charset.
    • CodeWriter

      public CodeWriter(Writer writer)
  • Method Details

    • setFormat

      @Deprecated public CodeWriter setFormat(boolean pretty)
      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

      public CodeWriter setPretty(boolean pretty)
      Indicates if output is split into indented lines instead of joined by the non-pretty separator (default is true).
    • getNonPrettyLineSeparator

      public String getNonPrettyLineSeparator()
      Returns the text written between lines when pretty printing is off (defaults to a single space).
    • setNonPrettyLineSeparator

      public CodeWriter setNonPrettyLineSeparator(String nonPrettyLineSeparator)
      Sets the text written between lines when pretty printing is off (defaults to a single space).
    • getIndentation

      public String getIndentation()
      Returns the text written once per indent level at the start of each line (defaults to two spaces).
    • setIndentation

      public CodeWriter setIndentation(String indentation)
      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

      public CodeWriter setIndentCount(int indentCount)
      Sets the current indent level: how many times the indentation is written at the start of each line.
    • indent

      public CodeWriter indent()
      Increases the indent level by one for the lines that follow.
    • getLineCount

      public long getLineCount()
      Returns the number of lines ended by the println methods.
    • outdent

      public CodeWriter outdent()
      Decreases the indent level by one; throws IllegalStateException if it is already zero.
    • print

      public CodeWriter print(String s, Object... args)
      Writes s (run through String.format when arguments are given), indenting text that starts a line.
    • printif

      public CodeWriter printif(boolean condition, String s, Object... args)
    • printifNotEmpty

      public CodeWriter printifNotEmpty(String s, Object... args)
      Prints s like print(String, Object...) unless it is null or only whitespace.
    • write

      @Deprecated public CodeWriter write(String s)
      Deprecated.
    • print

      public CodeWriter print(String s)
      Writes s literally, without String.format processing; text starting a line is indented first.
    • printif

      public CodeWriter printif(boolean condition, String s)
    • print

      public CodeWriter print()
      Writes just the pending prefix of a newly started line (its indentation, or the separator when not pretty).
    • newLine

      public CodeWriter newLine(boolean force)
      Writes a raw line break if pretty printing is on or force is true; the line count and indentation state are unchanged.
    • println

      public CodeWriter println(String s, Object... args)
      Writes the text like print(String, Object...) and ends the line; the line break itself is only written when pretty printing.
    • printlnif

      public CodeWriter printlnif(boolean condition, String s, Object... args)
    • printlnifNotEmpty

      public CodeWriter printlnifNotEmpty(String s, Object... args)
      Prints s and ends the line like println(String, Object...) unless it is null or only whitespace.
    • println

      public CodeWriter println(String s)
      Writes s literally, without String.format processing, and ends the line.
    • printlnif

      public CodeWriter printlnif(boolean condition, String s)
    • printlnif

      public CodeWriter printlnif(boolean condition)
    • writeNewLine

      @Deprecated public CodeWriter writeNewLine()
      Deprecated.
    • println

      public CodeWriter println()
    • flush

      public CodeWriter flush()
    • close

      public CodeWriter close()
      Flushes and closes the underlying writer or stream.
    • getSourceString

      public String getSourceString()
      Returns the code written so far; only supported by writers created with the no-argument constructor.