Interface BaseBuilder<T extends BaseBuilder<T>>

Type Parameters:
T - the concrete builder type (enables fluent interface with proper return types)
All Known Subinterfaces:
CheckboxBuilder, ChoiceBuilder, ConfirmBuilder, EditorBuilder, InputBuilder, KeyPressBuilder, ListBuilder, NumberBuilder, PasswordBuilder, SearchBuilder<T>, TextBuilder, ToggleBuilder
All Known Implementing Classes:
DefaultCheckboxBuilder, DefaultChoiceBuilder, DefaultConfirmBuilder, DefaultEditorBuilder, DefaultInputBuilder, DefaultKeyPressBuilder, DefaultListBuilder, DefaultNumberBuilder, DefaultPasswordBuilder, DefaultSearchBuilder, DefaultTextBuilder, DefaultToggleBuilder

public interface BaseBuilder<T extends BaseBuilder<T>>
Base interface for all prompt builders providing common configuration methods.

BaseBuilder defines the fundamental methods that all prompt builders share, including name and message configuration. It uses a parameterized self-type pattern to enable fluent method chaining while maintaining type safety across different builder types.

Fluent Interface Pattern

The interface uses the "curiously recurring template pattern" where each builder extends BaseBuilder<ConcreteBuilderType>. This ensures that method chaining returns the correct concrete type, enabling IDE auto-completion and type safety.

Common Usage Pattern


 // All builders follow this pattern
 builder.createListPrompt()
     .name("choice")           // From BaseBuilder
     .message("Select option:") // From BaseBuilder
     .newItem("opt1")          // Specific to ListBuilder
     .text("Option 1")         // Specific to ListBuilder
     .add()                    // Specific to ListBuilder
     .addPrompt();             // From BaseBuilder
 

Implementation Requirements

Concrete builders must:

  • Extend this interface with their own type as the parameter
  • Return this from all builder methods for fluent chaining
  • Validate that required fields (name, message) are set before building
Since:
3.30.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Complete the configuration of this prompt and add it to the parent builder.
    default T
    Set a filter function that modifies the actual returned value.
    message(String message)
    Set the message displayed to the user for this prompt.
    name(String name)
    Set the unique identifier for this prompt.
    default T
    Set a transformer function that modifies how the answer is displayed after submission.
  • Method Details

    • transformer

      default T transformer(Function<String,String> transformer)
      Set a transformer function that modifies how the answer is displayed after submission. This does not change the actual returned value. For example, a password prompt might transform the answer to "***".
      Parameters:
      transformer - the transformer function
      Returns:
      this builder instance for method chaining
    • filter

      default T filter(Function<String,String> filter)
      Set a filter function that modifies the actual returned value. This changes the value stored in the result map. For example, trimming whitespace or converting to lowercase.
      Parameters:
      filter - the filter function
      Returns:
      this builder instance for method chaining
    • name

      T name(String name)
      Set the unique identifier for this prompt.

      The name serves as the key in the result map returned by Prompter.prompt(java.util.List<org.jline.utils.AttributedString>, java.util.List<? extends org.jline.prompt.Prompt>). It must be unique within a single prompt session to avoid conflicts.

      Naming Guidelines:

      • Use descriptive, lowercase names with underscores: "user_name", "file_path"
      • Avoid spaces and special characters
      • Keep names concise but meaningful
      • Use consistent naming conventions across your application

      Example:

      
       builder.createInputPrompt()
           .name("email_address")  // Used as key in results map
           .message("Enter your email:")
           .addPrompt();
      
       // Later access the result
       InputResult emailResult = (InputResult) results.get("email_address");
       
      Parameters:
      name - the unique identifier for this prompt (required, non-null, non-empty)
      Returns:
      this builder instance for method chaining
      Throws:
      IllegalArgumentException - if name is null or empty
    • message

      T message(String message)
      Set the message displayed to the user for this prompt.

      The message is the primary text that explains what the user should do. It should be clear, concise, and provide sufficient context for the user to understand what input is expected.

      Message Guidelines:

      • Use clear, actionable language: "Select your preferred option"
      • End with appropriate punctuation (colon for selections, question mark for questions)
      • Keep messages concise but informative
      • Consider the terminal width for longer messages

      Examples:

      • List prompt: "Choose your preferred IDE:"
      • Input prompt: "Enter your full name:"
      • Confirm prompt: "Do you want to continue?"
      • Checkbox prompt: "Select features to enable:"
      Parameters:
      message - the message to display to the user (required, non-null, non-empty)
      Returns:
      this builder instance for method chaining
      Throws:
      IllegalArgumentException - if message is null or empty
    • addPrompt

      PromptBuilder addPrompt()
      Complete the configuration of this prompt and add it to the parent builder.

      This method finalizes the current prompt configuration, validates that all required fields are set, creates the prompt instance, and adds it to the parent PromptBuilder. After calling this method, you can continue adding more prompts or call PromptBuilder.build() to create the final list.

      Validation:

      This method typically validates that:

      • The prompt name is set and unique
      • The message is set and non-empty
      • Any prompt-specific requirements are met (e.g., list items are added)

      Example Usage:

      
       PromptBuilder builder = prompter.newBuilder();
      
       builder.createListPrompt()
           .name("color")
           .message("Choose a color:")
           .newItem("red").text("Red").add()
           .newItem("blue").text("Blue").add()
           .addPrompt()  // Completes this prompt and returns to builder
           .createConfirmPrompt()
           .name("confirm")
           .message("Are you sure?")
           .addPrompt(); // Completes second prompt
      
       List<Prompt> prompts = builder.build();
       
      Returns:
      the parent PromptBuilder for continued prompt configuration
      Throws:
      IllegalStateException - if required fields are not set or validation fails
      See Also: