Interface CodeBlock.Builder
- All Known Implementing Classes:
CodeBlockImpl.BuilderImpl
- Enclosing interface:
CodeBlock
CodeBlock.- Author:
- Square,Inc.
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: CodeBlock.java 1085 2024-01-05 16:23:28Z tquadrat $
- Since:
- 0.0.5
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.javacomposer.CodeBlock.Builder"
-
Method Summary
Modifier and TypeMethodDescriptionAdds code with positional or relative arguments.Adds aCodeBlockinstance.Adds code using named arguments.addStatement(String format, Object... args) Adds a statement.addStaticImport(Class<?> clazz, String... names) Adds a static import.addStaticImport(Enum<?> constant) Adds a static import for the givenenumvalue.addStaticImport(ClassName className, String... names) Adds a static import.beginControlFlow(String controlFlow, Object... args) Starts a control flow construct.build()Creates theCodeBlockfrom the added components.Ends a control flow construct that was previously begun with a call tobeginControlFlow(String, Object...).endControlFlow(String controlFlow, Object... args) Ends a control flow construct that was previously started with a call tobeginControlFlow(String,Object...)orbeginControlFlow(String,Object...).indent()Adds an indentation level to the code block.booleanisEmpty()Checks whether the code block to build would be empty.nextControlFlow(String controlFlow, Object... args) Adds another control flow construct to an already existing one.unindent()Removes an indentation level from the code block.
-
Method Details
-
add
Adds aCodeBlockinstance.- Parameters:
codeBlock- The code block.- Returns:
- This
Builderinstance.
-
add
Adds code with positional or relative arguments.
Relative arguments map 1:1 with the placeholders in the format string.
Positional arguments use an index after the placeholder to identify which argument index to use. For example, for a literal to reference the 3rd argument, use
"$3L"(1 based index).Mixing relative and positional arguments in a call to add is illegal and will result in an error.
- Parameters:
format- The format; may be empty.args- The arguments.- Returns:
- This
Builderinstance.
-
addNamed
Adds code using named arguments.
Named arguments specify their name after the '$' followed by a colon
":"and the corresponding type character. Argument names consist of characters ina-z, A-Z, 0-9, and _and must start with a lowercase character.For example, to refer to the type
Integerwith the argument nameclazzuse a format string containing$clazz:Tand include the keyclazzwith valuejava.lang.Integer.classin the argument map.- Parameters:
format- The format.args- The arguments.- Returns:
- This
Builderinstance.
-
addStatement
Adds a statement.
Do not use this method when the resulting code should be used as a field initializer. Use
add(String, Object...)instead.- Parameters:
format- The format.args- The arguments.- Returns:
- This
Builderinstance. - See Also:
-
addStaticImport
@API(status=STABLE, since="0.2.0") CodeBlock.Builder addStaticImport(Class<?> clazz, String... names) Adds a static import.- Parameters:
clazz- The class.names- The names of the elements from the given class that are to be imported.- Returns:
- This
Builderinstance. - Since:
- 0.2.0
-
addStaticImport
@API(status=STABLE, since="0.2.0") CodeBlock.Builder addStaticImport(ClassName className, String... names) Adds a static import.- Parameters:
className- The class.names- The names of the elements from the given class that are to be imported.- Returns:
- This
Builderinstance. - Since:
- 0.2.0
-
addStaticImport
Adds a static import for the givenenumvalue.- Parameters:
constant- Theenumvalue.- Returns:
- This
Builderinstance. - Since:
- 0.2.0
-
beginControlFlow
Starts a control flow construct.- Parameters:
controlFlow-The control flow construct and its code, such as
if (foo == 5).Shouldn't contain braces or newline characters.
args- The arguments.- Returns:
- This
Builderinstance. - See Also:
-
build
Creates theCodeBlockfrom the added components.- Returns:
- The new
CodeBlockinstance.
-
endControlFlow
Ends a control flow construct that was previously begun with a call tobeginControlFlow(String, Object...).- Returns:
- This
Builderinstance.
-
endControlFlow
Ends a control flow construct that was previously started with a call to
beginControlFlow(String,Object...)orbeginControlFlow(String,Object...).This form is only used for
do/whilecontrol flows.- Parameters:
controlFlow- The optional control flow construct and its code, such aswhile(foo == 20).args- The arguments.- Returns:
- This
Builderinstance.
-
indent
Adds an indentation level to the code block.- Returns:
- This
Builderinstance.
-
isEmpty
boolean isEmpty()Checks whether the code block to build would be empty.- Returns:
trueif the code block would be empty,falseotherwise.
-
nextControlFlow
Adds another control flow construct to an already existing one.- Parameters:
controlFlow-The control flow construct and its code, such as
else if (foo == 10).Shouldn't contain braces or newline characters.
args- The arguments.- Returns:
- This
Builderinstance.
-
unindent
Removes an indentation level from the code block.- Returns:
- This
Builderinstance.
-
