Class CodeBlock.Builder
java.lang.Object
be.imgn.gaia.processing.source.CodeBlock.Builder
- Enclosing class:
CodeBlock
-
Method Summary
Modifier and TypeMethodDescriptionAdds a code block.Adds code.addComment(String format, @Nullable Object... args) Adds a line comment, with placeholders as inadd(String, Object...): a type named with$Tis imported as needed, and a dollar sign is written$$.Adds code whose placeholders name their arguments, as in$type:Tor$count:L.addStatement(CodeBlock statement) Adds a statement: the code block, a semicolon and a new line.addStatement(String format, @Nullable Object... args) Adds a statement: the code, a semicolon and a new line.beginControlFlow(CodeBlock controlFlow) Opens a block, such asif (ready)orfor (String name : names), and indents what follows.beginControlFlow(String controlFlow, @Nullable Object... args) Opens a block, such asif (ready)orfor (String name : names), and indents what follows.build()Builds the code block.clear()Removes everything added so far.Closes a block.endControlFlow(CodeBlock controlFlow) Closes a block with code after its closing brace, such aswhile (more)for adoloop.endControlFlow(String controlFlow, @Nullable Object... args) Closes a block with code after its closing brace, such aswhile (more)for adoloop.indent()Indents the lines that follow.booleanisEmpty()Returns whether nothing was added yet.nextControlFlow(CodeBlock controlFlow) Closes a block and opens the next one, such aselse if (other)orcatch (IOException e).nextControlFlow(String controlFlow, @Nullable Object... args) Closes a block and opens the next one, such aselse if (other)orcatch (IOException e).unindent()Unindents the lines that follow.
-
Method Details
-
add
Adds code.- Parameters:
format- the code, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match
-
add
Adds a code block.- Parameters:
block- the code block- Returns:
- this builder
-
addNamed
Adds code whose placeholders name their arguments, as in$type:Tor$count:L. Names start with a lower-case letter, followed by letters, digits and underscores. Arguments the format does not use are ignored.builder.addNamed("$list:T<$item:T> $name:N = new $array:T<>()", Map.of( "list", List.class, "item", String.class, "name", "names", "array", ArrayList.class));- Parameters:
format- the code, with named placeholdersarguments- the arguments, by name- Returns:
- this builder
- Throws:
IllegalArgumentException- if a name is not valid, a placeholder has no argument, or the format has a placeholder without a name
-
addStatement
Adds a statement: the code, a semicolon and a new line.- Parameters:
format- the statement, without its semicolon, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match
-
addStatement
Adds a statement: the code block, a semicolon and a new line.- Parameters:
statement- the statement, without its semicolon- Returns:
- this builder
-
addComment
Adds a line comment, with placeholders as inadd(String, Object...): a type named with$Tis imported as needed, and a dollar sign is written$$.- Parameters:
format- the comment, without//, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match, or the comment has a line break
-
beginControlFlow
Opens a block, such asif (ready)orfor (String name : names), and indents what follows.- Parameters:
controlFlow- the code before the opening brace, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match
-
beginControlFlow
Opens a block, such asif (ready)orfor (String name : names), and indents what follows.- Parameters:
controlFlow- the code before the opening brace- Returns:
- this builder
-
nextControlFlow
Closes a block and opens the next one, such aselse if (other)orcatch (IOException e).- Parameters:
controlFlow- the code between the braces, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match
-
nextControlFlow
Closes a block and opens the next one, such aselse if (other)orcatch (IOException e).- Parameters:
controlFlow- the code between the braces- Returns:
- this builder
-
endControlFlow
-
endControlFlow
Closes a block with code after its closing brace, such aswhile (more)for adoloop.- Parameters:
controlFlow- the code after the closing brace, without its semicolon, with placeholdersargs- the arguments of the placeholders- Returns:
- this builder
- Throws:
IllegalArgumentException- if the format and the arguments do not match
-
endControlFlow
Closes a block with code after its closing brace, such aswhile (more)for adoloop.- Parameters:
controlFlow- the code after the closing brace, without its semicolon- Returns:
- this builder
-
indent
-
unindent
-
clear
-
isEmpty
public boolean isEmpty()Returns whether nothing was added yet.- Returns:
trueif the code block would be empty
-
build
-