Class CodeBlock.Builder

java.lang.Object
be.imgn.gaia.processing.source.CodeBlock.Builder
Enclosing class:
CodeBlock

public static final class CodeBlock.Builder extends Object
A builder of CodeBlocks.
  • Method Details

    • add

      public CodeBlock.Builder add(String format, @Nullable Object... args)
      Adds code.
      Parameters:
      format - the code, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • add

      public CodeBlock.Builder add(CodeBlock block)
      Adds a code block.
      Parameters:
      block - the code block
      Returns:
      this builder
    • addNamed

      public CodeBlock.Builder addNamed(String format, Map<String,?> arguments)
      Adds code whose placeholders name their arguments, as in $type:T or $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 placeholders
      arguments - 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

      public CodeBlock.Builder addStatement(String format, @Nullable Object... args)
      Adds a statement: the code, a semicolon and a new line.
      Parameters:
      format - the statement, without its semicolon, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • addStatement

      public CodeBlock.Builder addStatement(CodeBlock statement)
      Adds a statement: the code block, a semicolon and a new line.
      Parameters:
      statement - the statement, without its semicolon
      Returns:
      this builder
    • addComment

      public CodeBlock.Builder addComment(String format, @Nullable Object... args)
      Adds a line comment, with placeholders as in add(String, Object...): a type named with $T is imported as needed, and a dollar sign is written $$.
      Parameters:
      format - the comment, without //, with placeholders
      args - 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

      public CodeBlock.Builder beginControlFlow(String controlFlow, @Nullable Object... args)
      Opens a block, such as if (ready) or for (String name : names), and indents what follows.
      Parameters:
      controlFlow - the code before the opening brace, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • beginControlFlow

      public CodeBlock.Builder beginControlFlow(CodeBlock controlFlow)
      Opens a block, such as if (ready) or for (String name : names), and indents what follows.
      Parameters:
      controlFlow - the code before the opening brace
      Returns:
      this builder
    • nextControlFlow

      public CodeBlock.Builder nextControlFlow(String controlFlow, @Nullable Object... args)
      Closes a block and opens the next one, such as else if (other) or catch (IOException e).
      Parameters:
      controlFlow - the code between the braces, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • nextControlFlow

      public CodeBlock.Builder nextControlFlow(CodeBlock controlFlow)
      Closes a block and opens the next one, such as else if (other) or catch (IOException e).
      Parameters:
      controlFlow - the code between the braces
      Returns:
      this builder
    • endControlFlow

      public CodeBlock.Builder endControlFlow()
      Closes a block.
      Returns:
      this builder
    • endControlFlow

      public CodeBlock.Builder endControlFlow(String controlFlow, @Nullable Object... args)
      Closes a block with code after its closing brace, such as while (more) for a do loop.
      Parameters:
      controlFlow - the code after the closing brace, without its semicolon, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • endControlFlow

      public CodeBlock.Builder endControlFlow(CodeBlock controlFlow)
      Closes a block with code after its closing brace, such as while (more) for a do loop.
      Parameters:
      controlFlow - the code after the closing brace, without its semicolon
      Returns:
      this builder
    • indent

      public CodeBlock.Builder indent()
      Indents the lines that follow.
      Returns:
      this builder
    • unindent

      public CodeBlock.Builder unindent()
      Unindents the lines that follow.
      Returns:
      this builder
    • clear

      public CodeBlock.Builder clear()
      Removes everything added so far.
      Returns:
      this builder, empty
    • isEmpty

      public boolean isEmpty()
      Returns whether nothing was added yet.
      Returns:
      true if the code block would be empty
    • build

      public CodeBlock build()
      Builds the code block.
      Returns:
      the code block