SimpleCodeWriter
or a custom subclass of AbstractCodeWriter
.@Deprecated public class CodeWriter extends AbstractCodeWriter<CodeWriter>
Constructor and Description |
---|
CodeWriter()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
CodeWriter |
call(java.lang.Runnable task)
Deprecated.
Allows calling out to arbitrary code for things like looping or
conditional writes without breaking method chaining.
|
CodeWriter |
closeBlock(java.lang.String textAfterNewline,
java.lang.Object... args)
Deprecated.
Closes a block of syntax by writing a newline, dedenting, then writing text.
|
void |
copySettingsFrom(CodeWriter other)
Deprecated.
|
static CodeWriter |
createDefault()
Deprecated.
Creates a default instance of a CodeWriter that uses "\n" for newlines,
flattens multiple successive blank lines into a single blank line,
and adds a trailing new line if needed when converting the CodeWriter
to a string.
|
CodeWriter |
dedent()
Deprecated.
Removes one level of indentation from all lines.
|
CodeWriter |
dedent(int levels)
Deprecated.
Removes a specific number of indentations from all lines.
|
CodeWriter |
disableNewlines()
Deprecated.
Disables the automatic appending of newlines in the current state.
|
CodeWriter |
enableNewlines()
Deprecated.
Enables the automatic appending of newlines in the current state.
|
CodeWriter |
ensureNewline()
Deprecated.
Ensures that the last text written to the writer was a newline as defined in
the current state and inserts one if necessary.
|
CodeWriter |
indent()
Deprecated.
Indents all text one level.
|
CodeWriter |
indent(int levels)
Deprecated.
Indents all text a specific number of levels.
|
CodeWriter |
insertTrailingNewline()
Deprecated.
Configures the AbstractCodeWriter to always append a newline at the end of
the text if one is not already present.
|
CodeWriter |
insertTrailingNewline(boolean trailingNewline)
Deprecated.
Configures the AbstractCodeWriter to always append a newline at the end of
the text if one is not already present.
|
CodeWriter |
onSection(java.lang.String sectionName,
java.util.function.Consumer<java.lang.Object> interceptor)
Deprecated.
Registers a function that intercepts the contents of a section and
writes to the
AbstractCodeWriter with the updated contents. |
CodeWriter |
onSectionAppend(java.lang.String sectionName,
java.lang.Runnable writeAfter)
Deprecated.
|
CodeWriter |
onSectionPrepend(java.lang.String sectionName,
java.lang.Runnable writeBefore)
Deprecated.
|
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.Object... args)
Deprecated.
Opens a block of syntax by writing text, a newline, then indenting.
|
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object[] args,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3,
java.lang.Object arg4,
java.lang.Object arg5,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3,
java.lang.Object arg4,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Object arg1,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
openBlock(java.lang.String textBeforeNewline,
java.lang.String textAfterNewline,
java.lang.Runnable f)
Deprecated.
Opens a block of syntax by writing
textBeforeNewline , a newline, then
indenting, then executes the given Runnable , then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline . |
CodeWriter |
popState()
Deprecated.
Pops the current AbstractCodeWriter state from the state stack.
|
CodeWriter |
pushFilteredState(java.util.function.Function<java.lang.String,java.lang.String> filter)
Deprecated.
Pushes an anonymous named state that is always passed through the given
filter function before being written to the writer.
|
CodeWriter |
pushState()
Deprecated.
Copies and pushes the current state to the state stack.
|
CodeWriter |
pushState(java.lang.String sectionName)
Deprecated.
Copies and pushes the current state to the state stack using a named
state that can be intercepted by functions registered with
AbstractCodeWriter.onSection(CodeInterceptor) . |
CodeWriter |
putContext(java.util.Map<java.lang.String,java.lang.Object> mappings)
Deprecated.
Adds a map of named key-value pair to the context of the current state.
|
CodeWriter |
putContext(java.lang.String key,
java.lang.Object value)
Deprecated.
Adds a named key-value pair to the context of the current state.
|
CodeWriter |
putFormatter(char identifier,
java.util.function.BiFunction<java.lang.Object,java.lang.String,java.lang.String> formatFunction)
Deprecated.
Adds a custom formatter expression to the current state of the
AbstractCodeWriter . |
CodeWriter |
removeContext(java.lang.String key)
Deprecated.
Removes a named key-value pair from the context of the current state.
|
CodeWriter |
setExpressionStart(char expressionStart)
Deprecated.
Sets the character used to start expressions in the current state when calling
AbstractCodeWriter.write(java.lang.Object, java.lang.Object...) , AbstractCodeWriter.writeInline(java.lang.Object, java.lang.Object...) , AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...) , etc. |
CodeWriter |
setIndentText(java.lang.String indentText)
Deprecated.
Sets the text used for indentation (defaults to four spaces).
|
CodeWriter |
setNewline(char newline)
Deprecated.
Sets the character used to represent newlines in the current state
("\n" is the default).
|
CodeWriter |
setNewline(java.lang.String newline)
Deprecated.
Sets the character used to represent newlines in the current state
("\n" is the default).
|
CodeWriter |
setNewlinePrefix(java.lang.String newlinePrefix)
Deprecated.
Sets a prefix to prepend to every line after a new line is added
(except for an inserted trailing newline).
|
CodeWriter |
trimBlankLines()
Deprecated.
Ensures that no more than one blank line occurs in succession.
|
CodeWriter |
trimBlankLines(int trimBlankLines)
Deprecated.
Ensures that no more than the given number of newlines can occur
in succession, removing consecutive newlines that exceed the given
threshold.
|
CodeWriter |
trimTrailingSpaces()
Deprecated.
Enables the trimming of trailing spaces on a line.
|
CodeWriter |
trimTrailingSpaces(boolean trimTrailingSpaces)
Deprecated.
Configures if trailing spaces on a line are removed.
|
CodeWriter |
unwrite(java.lang.Object content,
java.lang.Object... args)
Deprecated.
Remove the most recent text written to the AbstractCodeWriter if and only
if the last written text is exactly equal to the given expanded
content string.
|
CodeWriter |
write(java.lang.Object content,
java.lang.Object... args)
Deprecated.
Writes text to the AbstractCodeWriter and appends a newline.
|
CodeWriter |
writeInline(java.lang.Object content,
java.lang.Object... args)
Deprecated.
Writes text to the AbstractCodeWriter without appending a newline.
|
CodeWriter |
writeOptional(java.lang.Object content)
Deprecated.
Optionally writes text to the AbstractCodeWriter and appends a newline
if a value is present.
|
CodeWriter |
writeWithNoFormatting(java.lang.Object content)
Deprecated.
Writes text to the AbstractCodeWriter and appends a newline.
|
consumer, copySettingsFrom, format, formatLiteral, getContext, getContext, getDebugInfo, getDebugInfo, getExpressionStart, getIndentLevel, getIndentText, getInsertTrailingNewline, getNewline, getNewlinePrefix, getTrimBlankLines, getTrimTrailingSpaces, injectSection, onSection, pushState, toString, writeInlineWithNoFormatting
public static CodeWriter createDefault()
@Deprecated public CodeWriter onSectionPrepend(java.lang.String sectionName, java.lang.Runnable writeBefore)
writer.onSectionPrepend("foo", () -> {
writer.write("This text is added before the rest of the section.");
});
sectionName
- The name of the section to intercept.writeBefore
- A runnable that prepends to a section by mutating the writer.as an alternative
that allows more explicit whitespace handling.
@Deprecated public CodeWriter onSectionAppend(java.lang.String sectionName, java.lang.Runnable writeAfter)
writer.onSectionAppend("foo", () -> {
writer.write("This text is added after the rest of the section.");
});
sectionName
- The name of the section to intercept.writeAfter
- A runnable that appends to a section by mutating the writer.as an alternative
that allows more explicit whitespace handling.
public void copySettingsFrom(CodeWriter other)
public CodeWriter putFormatter(char identifier, java.util.function.BiFunction<java.lang.Object,java.lang.String,java.lang.String> formatFunction)
AbstractCodeWriter
AbstractCodeWriter
.
The provided identifier
string must match the following ABNF:
%x21-23 ; ( '!' - '#' ) / %x25-2F ; ( '%' - '/' ) / %x3A-60 ; ( ':' - '`' ) / %x7B-7E ; ( '{' - '~' )
putFormatter
in class AbstractCodeWriter<CodeWriter>
identifier
- Formatter identifier to associate with this formatter.formatFunction
- Formatter function that formats the given object as a String.
The formatter is give the value to format as an object
(use .toString to access the string contents) and the
current indentation string of the AbstractCodeWriter.public CodeWriter setExpressionStart(char expressionStart)
AbstractCodeWriter
AbstractCodeWriter.write(java.lang.Object, java.lang.Object...)
, AbstractCodeWriter.writeInline(java.lang.Object, java.lang.Object...)
, AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...)
, etc.
By default, $
is used to start expressions (for example
$L
. However, some programming languages frequently give
syntactic meaning to $
, making this an inconvenient syntactic
character for the AbstractCodeWriter. In these cases, the character used to
start a AbstractCodeWriter expression can be changed. Just like $
, the
custom start character can be escaped using two subsequent start
characters (e.g., $$
).
setExpressionStart
in class AbstractCodeWriter<CodeWriter>
expressionStart
- Character to use to start expressions.public CodeWriter pushState()
AbstractCodeWriter
This method is used to prepare for a corresponding AbstractCodeWriter.popState()
operation later. It stores the current state of the AbstractCodeWriter into a
stack and keeps it active. After pushing, mutations can be made to the
state of the AbstractCodeWriter without affecting the previous state on the
stack. Changes to the state of the AbstractCodeWriter can be undone by using
AbstractCodeWriter.popState()
, which Returns self state to the state
it was in before calling pushState
.
pushState
in class AbstractCodeWriter<CodeWriter>
public CodeWriter pushState(java.lang.String sectionName)
AbstractCodeWriter
AbstractCodeWriter.onSection(CodeInterceptor)
.
The text written while in this state is buffered and passed to each
state interceptor. If no text is written by the section or an
interceptor, nothing is changed on the AbstractCodeWriter
. This
behavior allows for placeholder sections to be added into
AbstractCodeWriter
generators in order to provide extension points
that can be otherwise empty.
pushState
in class AbstractCodeWriter<CodeWriter>
sectionName
- Name of the section to set on the state.public CodeWriter pushFilteredState(java.util.function.Function<java.lang.String,java.lang.String> filter)
AbstractCodeWriter
pushFilteredState
in class AbstractCodeWriter<CodeWriter>
filter
- Function that maps over the entire section when popped.public CodeWriter popState()
AbstractCodeWriter
This method is used to reverse a previous AbstractCodeWriter.pushState()
operation. It configures the current AbstractCodeWriter state to what it was
before the last preceding pushState
call.
popState
in class AbstractCodeWriter<CodeWriter>
public CodeWriter onSection(java.lang.String sectionName, java.util.function.Consumer<java.lang.Object> interceptor)
AbstractCodeWriter
AbstractCodeWriter
with the updated contents.
The interceptor
function is expected to have a reference to
the AbstractCodeWriter
and to mutate it when they are invoked. Each
interceptor is invoked in their own isolated pushed/popped states.
The text provided to interceptor
does not contain a trailing
new line. A trailing new line is expected to be injected automatically
when the results of intercepting the contents are written to the
AbstractCodeWriter
. A result is only written if the interceptors write
a non-null, non-empty string, allowing for empty placeholders to be
added that don't affect the resulting layout of the code.
SimpleCodeWriter = new SimpleCodeWriter();
// Prepend text to a section named "foo".
writer.onSectionPrepend("foo", () -> writer.write("A"));
// Write text to a section, and ensure that the original
// text is written too.
writer.onSection("foo", text -> {
// Write before the original text.
writer.write("A");
// Write the original text of the section.
writer.writeWithNoFormatting(text);
// Write more text to the section.
writer.write("C");
});
// Create the section, write to it, then close the section.
writer.pushState("foo").write("B").popState();
assert(writer.toString().equals("A\nB\nC\n"));
This method is a wrapper around AbstractCodeWriter.onSection(CodeInterceptor)
that has several limitations:
interceptor
is expected to have a reference
to an AbstractCodeWriter
so that write calls can be made.AbstractCodeWriter.onSection(CodeInterceptor)
must be used directly and
careful use of AbstractCodeWriter.writeInlineWithNoFormatting(Object)
is
required when writing the previous contents to the interceptor.CodeInterceptor
s do.
The newline handling functionality provided by this method can be
reproduced using a CodeInterceptor
by removing trailing newlines
using AbstractCodeWriter.removeTrailingNewline(String)
.
SimpleCodeWriter = new SimpleCodeWriter();
CodeInterceptor<CodeSection, SimpleCodeWriter> interceptor = CodeInterceptor.forName(sectionName, (w, p) -> {
String trimmedContent = removeTrailingNewline(p);
interceptor.accept(trimmedContent);
})
writer.onSection(interceptor);
onSection
in class AbstractCodeWriter<CodeWriter>
sectionName
- The name of the section to intercept.interceptor
- The function to intercept with.public CodeWriter disableNewlines()
AbstractCodeWriter
Methods like AbstractCodeWriter.write(java.lang.Object, java.lang.Object...)
, AbstractCodeWriter.openBlock(java.lang.String, java.lang.Object...)
, and AbstractCodeWriter.closeBlock(java.lang.String, java.lang.Object...)
will not automatically append newlines when a state has this flag set.
disableNewlines
in class AbstractCodeWriter<CodeWriter>
public CodeWriter enableNewlines()
AbstractCodeWriter
enableNewlines
in class AbstractCodeWriter<CodeWriter>
public CodeWriter setNewline(java.lang.String newline)
AbstractCodeWriter
When the provided string is empty (""), then newlines are disabled
in the current state. This is exactly equivalent to calling
AbstractCodeWriter.disableNewlines()
, and does not actually change the newline
character of the current state.
Setting the newline character to a non-empty string implicitly enables newlines in the current state.
setNewline
in class AbstractCodeWriter<CodeWriter>
newline
- Newline character to use.public CodeWriter setNewline(char newline)
AbstractCodeWriter
This call also enables newlines in the current state by calling
AbstractCodeWriter.enableNewlines()
.
setNewline
in class AbstractCodeWriter<CodeWriter>
newline
- Newline character to use.public CodeWriter setIndentText(java.lang.String indentText)
AbstractCodeWriter
setIndentText
in class AbstractCodeWriter<CodeWriter>
indentText
- Indentation text.public CodeWriter trimTrailingSpaces()
AbstractCodeWriter
trimTrailingSpaces
in class AbstractCodeWriter<CodeWriter>
public CodeWriter trimTrailingSpaces(boolean trimTrailingSpaces)
AbstractCodeWriter
trimTrailingSpaces
in class AbstractCodeWriter<CodeWriter>
trimTrailingSpaces
- Set to true to trim trailing spaces.public CodeWriter trimBlankLines()
AbstractCodeWriter
trimBlankLines
in class AbstractCodeWriter<CodeWriter>
public CodeWriter trimBlankLines(int trimBlankLines)
AbstractCodeWriter
trimBlankLines
in class AbstractCodeWriter<CodeWriter>
trimBlankLines
- Number of allowed consecutive newlines. Set to
-1 to perform no trimming. Set to 0 to allow no blank lines. Set to
1 or more to allow for no more than N consecutive blank lines.public CodeWriter insertTrailingNewline()
AbstractCodeWriter
This setting is not captured as part of push/popState.
insertTrailingNewline
in class AbstractCodeWriter<CodeWriter>
public CodeWriter insertTrailingNewline(boolean trailingNewline)
AbstractCodeWriter
This setting is not captured as part of push/popState.
insertTrailingNewline
in class AbstractCodeWriter<CodeWriter>
trailingNewline
- True if a newline is added.public CodeWriter setNewlinePrefix(java.lang.String newlinePrefix)
AbstractCodeWriter
setNewlinePrefix
in class AbstractCodeWriter<CodeWriter>
newlinePrefix
- Newline prefix to use.public CodeWriter indent()
AbstractCodeWriter
indent
in class AbstractCodeWriter<CodeWriter>
public CodeWriter indent(int levels)
AbstractCodeWriter
indent
in class AbstractCodeWriter<CodeWriter>
levels
- Number of levels to indent.public CodeWriter dedent()
AbstractCodeWriter
dedent
in class AbstractCodeWriter<CodeWriter>
public CodeWriter dedent(int levels)
AbstractCodeWriter
Set to -1 to dedent back to 0 (root).
dedent
in class AbstractCodeWriter<CodeWriter>
levels
- Number of levels to remove.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.Object... args)
AbstractCodeWriter
String result = new SimpleCodeWriter()
.openBlock("public final class $L {", "Foo")
.openBlock("public void main(String[] args) {")
.write("System.out.println(args[0]);")
.closeBlock("}")
.closeBlock("}")
.toString();
openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.args
- String arguments to use for formatting.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.
SimpleCodeWriter = new SimpleCodeWriter();
writer.openBlock("public final class $L {", "}", "Foo", () -> {
writer.openBlock("public void main(String[] args) {", "}", () -> {
writer.write("System.out.println(args[0]);");
})
});
openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object arg1, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object arg1, java.lang.Object arg2, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute into textBeforeNewline
.arg2
- Second positional argument to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute into textBeforeNewline
.arg2
- Second positional argument to substitute into textBeforeNewline
.arg3
- Third positional argument to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute into textBeforeNewline
.arg2
- Second positional argument to substitute into textBeforeNewline
.arg3
- Third positional argument to substitute into textBeforeNewline
.arg4
- Fourth positional argument to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.arg1
- First positional argument to substitute into textBeforeNewline
.arg2
- Second positional argument to substitute into textBeforeNewline
.arg3
- Third positional argument to substitute into textBeforeNewline
.arg4
- Fourth positional argument to substitute into textBeforeNewline
.arg5
- Fifth positional argument to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter openBlock(java.lang.String textBeforeNewline, java.lang.String textAfterNewline, java.lang.Object[] args, java.lang.Runnable f)
AbstractCodeWriter
textBeforeNewline
, a newline, then
indenting, then executes the given Runnable
, then closes the block of
syntax by writing a newline, dedenting, then writing textAfterNewline
.openBlock
in class AbstractCodeWriter<CodeWriter>
textBeforeNewline
- Text to write before writing a newline and indenting.textAfterNewline
- Text to write after writing a newline and indenting.args
- Arguments to substitute into textBeforeNewline
.f
- Runnable function to execute inside of the block.public CodeWriter closeBlock(java.lang.String textAfterNewline, java.lang.Object... args)
AbstractCodeWriter
closeBlock
in class AbstractCodeWriter<CodeWriter>
textAfterNewline
- Text to write after writing a newline and dedenting.args
- String arguments to use for formatting.public CodeWriter writeWithNoFormatting(java.lang.Object content)
AbstractCodeWriter
The provided text does not use any kind of expression formatting.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
writeWithNoFormatting
in class AbstractCodeWriter<CodeWriter>
content
- Content to write.public CodeWriter call(java.lang.Runnable task)
AbstractCodeWriter
call
in class AbstractCodeWriter<CodeWriter>
task
- Method to invoke.public CodeWriter write(java.lang.Object content, java.lang.Object... args)
AbstractCodeWriter
The provided text is automatically formatted using variadic arguments.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
write
in class AbstractCodeWriter<CodeWriter>
content
- Content to write.args
- String arguments to use for formatting.public CodeWriter writeInline(java.lang.Object content, java.lang.Object... args)
AbstractCodeWriter
The provided text is automatically formatted using variadic arguments.
Indentation and the newline prefix is only prepended if the writer's cursor is at the beginning of a newline.
If newlines are present in the given string, each of those lines will receive proper indentation.
writeInline
in class AbstractCodeWriter<CodeWriter>
content
- Content to write.args
- String arguments to use for formatting.public CodeWriter ensureNewline()
AbstractCodeWriter
ensureNewline
in class AbstractCodeWriter<CodeWriter>
public CodeWriter writeOptional(java.lang.Object content)
AbstractCodeWriter
If the provided content
value is null
, nothing is
written. If the provided content
value is an empty
Optional
, nothing is written. If the result of calling
toString
on content
results in an empty string,
nothing is written. Finally, if the value is a non-empty string,
the content is written to the AbstractCodeWriter
at the current
level of indentation, and a newline is appended.
writeOptional
in class AbstractCodeWriter<CodeWriter>
content
- Content to write if present.public CodeWriter unwrite(java.lang.Object content, java.lang.Object... args)
AbstractCodeWriter
This can be useful, for example, for use cases like removing trailing commas from lists of values.
For example, the following will remove ", there." from the end of the AbstractCodeWriter:
SimpleCodeWriter = new SimpleCodeWriter();
writer.writeInline("Hello, there.");
writer.unwrite(", there.");
assert(writer.toString().equals("Hello\n"));
However, the following call to unwrite will do nothing because the last text written to the AbstractCodeWriter does not match:
SimpleCodeWriter = new SimpleCodeWriter();
writer.writeInline("Hello.");
writer.unwrite("there.");
assert(writer.toString().equals("Hello.\n"));
unwrite
in class AbstractCodeWriter<CodeWriter>
content
- Content to write.args
- String arguments to use for formatting.public CodeWriter putContext(java.lang.String key, java.lang.Object value)
AbstractCodeWriter
These context values can be referenced by named interpolated parameters.
putContext
in class AbstractCodeWriter<CodeWriter>
key
- Key to add to the context.value
- Value to associate with the key.public CodeWriter putContext(java.util.Map<java.lang.String,java.lang.Object> mappings)
AbstractCodeWriter
These context values can be referenced by named interpolated parameters.
putContext
in class AbstractCodeWriter<CodeWriter>
mappings
- Key value pairs to add.public CodeWriter removeContext(java.lang.String key)
AbstractCodeWriter
removeContext
in class AbstractCodeWriter<CodeWriter>
key
- Key to add to remove from the current context.