Class CodeBlockImpl.BuilderImpl
- All Implemented Interfaces:
CodeBlock.Builder
- Enclosing class:
CodeBlockImpl
CodeBlock.Builder
as the builder for a new
CodeBlockImpl
instance.- Author:
- Square,Inc.
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: CodeBlockImpl.java 1105 2024-02-28 12:58:46Z tquadrat $
- Since:
- 0.0.5
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.javacomposer.internal.CodeBlockImpl.BuilderImpl"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Collection
<Object> The arguments.private final JavaComposer
The reference to the factory.The format Strings.private final Collection
<String> The static imports. -
Constructor Summary
ConstructorsConstructorDescriptionBuilderImpl
(JavaComposer composer) Creates a newBuilderImpl
instance.BuilderImpl
(JavaComposer composer, List<String> formatParts, List<Object> args) Creates a newBuilderImpl
instance. -
Method Summary
Modifier and TypeMethodDescriptionAdds code with positional or relative arguments.Adds aCodeBlock
instance.private final void
addArgument
(String format, char placeholder, Object arg) Adds the placeholder's argument.private final void
addDebug()
Adds debug output.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 givenenum
value.addStaticImport
(ClassName className, String... names) Adds a static import.addWithoutDebugInfo
(String format, Object... args) Adds code with positional or relative arguments, without prepending any debug output.addWithoutDebugInfo
(CodeBlock codeBlock) Adds aCodeBlock
instance without prepending any debug output.args()
Returns the arguments.private static final Object
Returns the given object literally.private static final Object
Translates the given object to a name.private static final Object
Translates the given object to a String.private static final TypeNameImpl
Translates the given object to a type.beginControlFlow
(String controlFlow, Object... args) Starts a control flow construct.final CodeBlockImpl
build()
Creates theCodeBlock
from the added components.Ends a control flow construct that was previously begun with a call toCodeBlock.Builder.beginControlFlow(String, Object...)
.endControlFlow
(String controlFlow, Object... args) Ends a control flow construct that was previously started with a call toCodeBlock.Builder.beginControlFlow(String,Object...)
orCodeBlock.Builder.beginControlFlow(String,Object...)
.Returns the format parts.indent()
Adds an indentation level to the code block.final boolean
isEmpty()
Checks whether the code block to build would be empty.private static final boolean
isNoArgPlaceholder
(char placeholder) Checks whether the given placeholder character would expect an argument.nextControlFlow
(String controlFlow, Object... args) Adds another control flow construct to an already existing one.unindent()
Removes an indentation level from the code block.
-
Field Details
-
Constructor Details
-
BuilderImpl
Creates a newBuilderImpl
instance.- Parameters:
composer
- The reference to the factory that created this builder instance.
-
BuilderImpl
Creates a newBuilderImpl
instance.- Parameters:
composer
- The reference to the factory that created this builder instance.formatParts
- The format parts.args
- The arguments.
-
-
Method Details
-
add
Adds aCodeBlock
instance.- Specified by:
add
in interfaceCodeBlock.Builder
- Parameters:
codeBlock
- The code block.- Returns:
- This
Builder
instance.
-
add
@API(status=INTERNAL, since="0.2.0") public final CodeBlockImpl.BuilderImpl add(String format, Object... args) 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.
- Specified by:
add
in interfaceCodeBlock.Builder
- Parameters:
format
- The format; may be empty.args
- The arguments.- Returns:
- This
Builder
instance.
-
addArgument
Adds the placeholder's argument.- Parameters:
format
- The format.placeholder
- The placeholder character.arg
- The argument.
-
addDebug
Adds debug output. -
addNamed
@API(status=INTERNAL, since="0.2.0") public final CodeBlockImpl.BuilderImpl addNamed(String format, Map<String, ?> args) 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
Integer
with the argument nameclazz
use a format string containing$clazz:T
and include the keyclazz
with valuejava.lang.Integer.class
in the argument map.- Specified by:
addNamed
in interfaceCodeBlock.Builder
- Parameters:
format
- The format.args
- The arguments.- Returns:
- This
Builder
instance.
-
addStatement
@API(status=STABLE, since="0.2.0") public final CodeBlockImpl.BuilderImpl addStatement(String format, Object... args) Adds a statement.
Do not use this method when the resulting code should be used as a field initializer. Use
CodeBlock.Builder.add(String, Object...)
instead.- Specified by:
addStatement
in interfaceCodeBlock.Builder
- Parameters:
format
- The format.args
- The arguments.- Returns:
- This
Builder
instance. - See Also:
-
addStaticImport
@API(status=STABLE, since="0.2.0") public final CodeBlockImpl.BuilderImpl addStaticImport(Class<?> clazz, String... names) Adds a static import.- Specified by:
addStaticImport
in interfaceCodeBlock.Builder
- Parameters:
clazz
- The class.names
- The names of the elements from the given class that are to be imported.- Returns:
- This
Builder
instance.
-
addStaticImport
@API(status=STABLE, since="0.2.0") public final CodeBlockImpl.BuilderImpl addStaticImport(ClassName className, String... names) Adds a static import.- Specified by:
addStaticImport
in interfaceCodeBlock.Builder
- Parameters:
className
- The class.names
- The names of the elements from the given class that are to be imported.- Returns:
- This
Builder
instance.
-
addStaticImport
@API(status=STABLE, since="0.2.0") public final CodeBlockImpl.BuilderImpl addStaticImport(Enum<?> constant) Adds a static import for the givenenum
value.- Specified by:
addStaticImport
in interfaceCodeBlock.Builder
- Parameters:
constant
- Theenum
value.- Returns:
- This
Builder
instance.
-
addWithoutDebugInfo
Adds aCodeBlock
instance without prepending any debug output.- Parameters:
codeBlock
- The code block.- Returns:
- This
Builder
instance.
-
addWithoutDebugInfo
@API(status=INTERNAL, since="0.2.0") public final CodeBlockImpl.BuilderImpl addWithoutDebugInfo(String format, Object... args) Adds code with positional or relative arguments, without prepending any debug output.
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
Builder
instance.
-
args
Returns the arguments.- Returns:
- The arguments.
-
argToLiteral
Returns the given object literally.- Parameters:
o
- The object.- Returns:
- The literal.
-
argToName
Translates the given object to a name.- Parameters:
o
- The object.- Returns:
- The name.
-
argToString
Translates the given object to a String.- Parameters:
o
- The object.- Returns:
- The resulting String, or
Util.NULL_REFERENCE
if the object isnull
.
-
argToType
Translates the given object to a type.- Parameters:
o
- The object.- Returns:
- The resulting type.
-
beginControlFlow
@API(status=INTERNAL, since="0.0.5") public final CodeBlockImpl.BuilderImpl beginControlFlow(String controlFlow, Object... args) Starts a control flow construct.- Specified by:
beginControlFlow
in interfaceCodeBlock.Builder
- 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
Builder
instance. - See Also:
-
build
Creates theCodeBlock
from the added components.- Specified by:
build
in interfaceCodeBlock.Builder
- Returns:
- The new
CodeBlock
instance.
-
endControlFlow
Ends a control flow construct that was previously begun with a call toCodeBlock.Builder.beginControlFlow(String, Object...)
.- Specified by:
endControlFlow
in interfaceCodeBlock.Builder
- Returns:
- This
Builder
instance.
-
endControlFlow
@API(status=INTERNAL, since="0.0.5") public final CodeBlockImpl.BuilderImpl endControlFlow(String controlFlow, Object... args) Ends a control flow construct that was previously started with a call to
CodeBlock.Builder.beginControlFlow(String,Object...)
orCodeBlock.Builder.beginControlFlow(String,Object...)
.This form is only used for
do/while
control flows.- Specified by:
endControlFlow
in interfaceCodeBlock.Builder
- Parameters:
controlFlow
- The optional control flow construct and its code, such aswhile(foo == 20)
.args
- The arguments.- Returns:
- This
Builder
instance.
-
formatParts
Returns the format parts.- Returns:
- The format parts.
-
indent
Adds an indentation level to the code block.- Specified by:
indent
in interfaceCodeBlock.Builder
- Returns:
- This
Builder
instance.
-
isEmpty
Checks whether the code block to build would be empty.- Specified by:
isEmpty
in interfaceCodeBlock.Builder
- Returns:
true
if the code block would be empty,false
otherwise.
-
isNoArgPlaceholder
Checks whether the given placeholder character would expect an argument.- Parameters:
placeholder
- The placeholder character.- Returns:
true
if there is no argument expected,false
otherwise.
-
nextControlFlow
@API(status=INTERNAL, since="0.0.5") public final CodeBlockImpl.BuilderImpl nextControlFlow(String controlFlow, Object... args) Adds another control flow construct to an already existing one.- Specified by:
nextControlFlow
in interfaceCodeBlock.Builder
- 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
Builder
instance.
-
unindent
Removes an indentation level from the code block.- Specified by:
unindent
in interfaceCodeBlock.Builder
- Returns:
- This
Builder
instance.
-