Class ThaiBahtConfig.Builder

java.lang.Object
io.github.zazalng.ThaiBahtConfig.Builder
Enclosing class:
ThaiBahtConfig

public static final class ThaiBahtConfig.Builder extends Object
Builder for constructing immutable ThaiBahtConfig instances.

This builder class uses the builder pattern to configure all aspects of currency text conversion. Once configured, call build() to create an immutable ThaiBahtConfig. The built configuration is thread-safe and can be safely shared across application threads.

Key Features

  • Fluent Interface: All setter methods return this for method chaining
  • Not Thread-Safe: Builder instances should not be shared across threads
  • Immutable Results: Each build() call creates a new immutable configuration
  • Smart Prefix Handling: Negative prefix automatically updates when language changes, unless explicitly set by user

Negative Prefix Behavior

The builder handles negative prefixes intelligently:

  • When first created, prefix starts empty and defaults to the language's standard prefix
  • If you call language(Language), the prefix automatically updates to the new language's default
  • Once you call setPrefix(String), the prefix is marked as "user-set" and no longer updates with language changes
  • To reset to language defaults, create a new builder or explicitly set prefix to null

Usage Examples

Example 1 - Basic builder pattern:


 ThaiBahtConfig config = ThaiBahtConfig.builder()
     .language(Language.THAI)
     .useUnit(true)
     .formal(true)
     .build();
 

Example 2 - Chaining multiple settings:


 ThaiBahtConfig config = ThaiBahtConfig.builder(Language.ENGLISH)
     .useUnit(true)
     .setPrefix("Payment Amount")
     .formal(true)
     .build();
 

Example 3 - Custom format templates:


 ThaiBahtConfig config = ThaiBahtConfig.builder()
     .setFormatTemplate("{INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}")
     .setNegativeFormatTemplate("({NEGPREFIX}){INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}")
     .build();
 
Since:
1.0
See Also:
  • Constructor Details

    • Builder

      public Builder(LanguageHandler handler)
      Constructs a builder with a specific initial language handler.

      This constructor sets up the builder with the given language handler and initializes other settings to their defaults. The negative prefix starts as empty string and will default to the handler's standard prefix unless explicitly set.

      Parameters:
      handler - the language handler, must not be null
      Since:
      2.0.0
    • Builder

      @Deprecated public Builder(Language language)
      Deprecated.
      Use Builder(LanguageHandler) for new code
      Constructs a builder with a specific initial language (backward compatibility).

      This constructor creates the appropriate handler for the language and initializes other settings to their defaults.

      Parameters:
      language - the language enum, must not be null
  • Method Details

    • languageHandler

      public ThaiBahtConfig.Builder languageHandler(LanguageHandler handler)
      Sets the language handler for text conversion.

      This method controls which language handler is used for conversion. The handler defines both the conversion logic and language-specific unit words.

      Example:

      
       ThaiBahtConfig config = ThaiBahtConfig.builder()
           .languageHandler(new EnglishLanguageHandler())
           .build();
       
      Parameters:
      handler - the language handler, must not be null
      Returns:
      this builder for method chaining
      Since:
      2.0.0
    • language

      @Deprecated public ThaiBahtConfig.Builder language(Language language)
      Deprecated.
      Sets the output language for text conversion (backward compatibility).

      This method controls which language handler is used by selecting the appropriate built-in handler for the language. This is provided for backward compatibility with v1.x code.

      Example:

      
       ThaiBahtConfig config = ThaiBahtConfig.builder()
           .language(Language.ENGLISH)
           .build();
       
      Parameters:
      language - the desired output language (THAI or ENGLISH), must not be null
      Returns:
      this builder for method chaining
      Since:
      1.3
    • useUnit

      public ThaiBahtConfig.Builder useUnit(boolean v)
      Sets whether unit words should be included in the output.

      When true (default), the text will include currency unit words ("บาท"/"Baht" and "สตางค์"/"Satang"). When false, only numeric words are output.

      Example:

      
       // With units: "One Hundred Baht"
       // Without units: "One Hundred"
       ThaiBahtConfig config = ThaiBahtConfig.builder()
           .language(Language.ENGLISH)
           .useUnit(true)  // Include "Baht"
           .build();
       
      Parameters:
      v - true to include unit words (default), false to omit them
      Returns:
      this builder for method chaining
    • formal

      public ThaiBahtConfig.Builder formal(boolean v)
      Sets whether formal wording rules should be applied.

      This setting is currently reserved for future use and may enable different wording conventions (formal vs. casual) in future versions. Currently, formal mode is always treated as enabled but serves as a placeholder for language variation support.

      Parameters:
      v - true to use formal rules (default), false for casual
      Returns:
      this builder for method chaining
    • setPrefix

      public ThaiBahtConfig.Builder setPrefix(String negativePrefix)
      Sets the prefix wording for negative amounts.

      This string will be prepended to negative numeric representations. Once explicitly set, this prefix will be preserved even when the language is changed via language(Language). If you want to reset to language defaults after setting a custom prefix, you must create a new builder or set the prefix back to null or empty string.

      Language defaults:

      • Thai: "ลบ" (Thai minus)
      • English: "Minus"

      Examples:

      
       // Custom Thai prefix
       ThaiBahtConfig config1 = ThaiBahtConfig.builder()
           .setPrefix("ติดลบ")
           .build();
       // Output for -100: "ติดลบหนึ่งร้อยบาทถ้วน"
      
       // Custom English prefix
       ThaiBahtConfig config2 = ThaiBahtConfig.builder(Language.ENGLISH)
           .setPrefix("Negative:")
           .build();
       // Output for -100: "Negative: One Hundred Baht Only"
      
       // Resetting prefix (set to empty or null)
       ThaiBahtConfig config3 = ThaiBahtConfig.builder(Language.THAI)
           .setPrefix("")
           .build();
       // Prefix reverts to "" (no prefix for this config)
       
      Parameters:
      negativePrefix - the new prefix to use for negative amounts (null treated as empty string)
      Returns:
      this builder for method chaining
      Since:
      1.0
    • setFormatTemplate

      public ThaiBahtConfig.Builder setFormatTemplate(String formatString)
      Sets a custom format template for positive amounts.

      Custom format templates provide precise control over output layout through named placeholders. The format string supports the following placeholders (wrapped in curly braces):

      • {INTEGER} - The baht (integer) part as text
      • {UNIT} - Currency unit word (บาท/Baht)
      • {EXACT} - The exact indicator when satang is zero (ถ้วน/Only)
      • {FLOAT} - The satang (fractional) part as text
      • {SATANG} - Satang unit word (สตางค์/Satang)
      • {FLOAT?content} - Conditional: show content only if satang is non-zero
      • {SATANG?content} - Conditional: show content only if satang is non-zero

      Format Requirements:

      • Must contain both {INTEGER} and {FLOAT} placeholders
      • Other placeholders are optional
      • Placeholders can be used multiple times
      • Regular text can be placed between placeholders freely

      Examples:

      
       // Standard format
       builder.setFormatTemplate("{INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}");
       // Output: "หนึ่งร้อยบาทและห้าสิบสตางค์" for 100.50
      
       // With spaces and parentheses
       builder.setFormatTemplate("{INTEGER} {UNIT}{FLOAT? ({FLOAT} {SATANG})}");
       // Output: "หนึ่งร้อย บาท (ห้าสิบ สตางค์)" for 100.50
      
       // English example
       builder.setFormatTemplate("{INTEGER} {UNIT}{FLOAT?  and  {FLOAT} {SATANG}}");
       // Output: "Five Hundred Baht  and  Fifty Satang" for 500.50
       
      Parameters:
      formatString - the custom format string with named placeholders, null or empty string will clear the format template
      Returns:
      this builder for method chaining
      Throws:
      IllegalArgumentException - if format string is invalid (missing required placeholders)
      Since:
      1.4.0
    • setFormatTemplate

      public ThaiBahtConfig.Builder setFormatTemplate(FormatTemplate template)
      Sets a custom format template for positive amounts using a FormatTemplate instance.

      This method accepts a pre-constructed FormatTemplate object. Use this when you want to reuse the same format across multiple configurations or when you need to validate the format before applying it.

      Parameters:
      template - the format template instance, null clears the template
      Returns:
      this builder for method chaining
      Since:
      1.4.0
      See Also:
    • setNegativeFormatTemplate

      public ThaiBahtConfig.Builder setNegativeFormatTemplate(String formatString)
      Sets a custom format template for negative amounts.

      Custom format templates for negative amounts provide precise control over how negative values are displayed. Use this when you want different formatting for negative vs. positive amounts. If no negative template is configured, the positive template (if any) will be used instead.

      Supported placeholders (same as positive templates):

      • {INTEGER} - The baht (integer) part as text
      • {UNIT} - Currency unit word (บาท/Baht)
      • {EXACT} - The exact indicator when satang is zero (ถ้วน/Only)
      • {FLOAT} - The satang (fractional) part as text
      • {SATANG} - Satang unit word (สตางค์/Satang)
      • {NEGPREFIX} - Negative prefix (ลบ/Minus) - useful in negative format
      • {FLOAT?content} - Conditional: show content only if satang is non-zero
      • {SATANG?content} - Conditional: show content only if satang is non-zero

      Examples:

      
       // Negative in parentheses
       builder.setNegativeFormatTemplate("({NEGPREFIX} {INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}})");
       // Output for -100.50: "(ลบ หนึ่งร้อยบาทและห้าสิบสตางค์)"
      
       // Negative with different prefix
       builder.setNegativeFormatTemplate("{NEGPREFIX}: {INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}");
       // Output for -100.50: "Minus: One Hundred Baht and Fifty Satang"
       
      Parameters:
      formatString - the custom format string with named placeholders for negative amounts, null or empty string will clear the template
      Returns:
      this builder for method chaining
      Throws:
      IllegalArgumentException - if format string is invalid (missing required placeholders)
      Since:
      1.4.0
    • setNegativeFormatTemplate

      public ThaiBahtConfig.Builder setNegativeFormatTemplate(FormatTemplate template)
      Sets a custom format template for negative amounts using a FormatTemplate instance.

      This method accepts a pre-constructed FormatTemplate object. Use this when you want to validate or reuse format templates across multiple configurations.

      Parameters:
      template - the format template instance for negative amounts, null clears the template
      Returns:
      this builder for method chaining
      Since:
      1.4.0
      See Also:
    • setFormat

      public ThaiBahtConfig.Builder setFormat(String formatString, boolean applyToNegative)
      Sets a custom format for both positive and negative amounts using the same template.

      This convenience method applies the same format template to both positive and negative amounts. It's useful when you want consistent formatting regardless of sign. For different formats per sign, use setFormatTemplate(String) and setNegativeFormatTemplate(String) separately.

      Supported placeholders:

      • {INTEGER} - The baht (integer) part as text
      • {UNIT} - Currency unit word (บาท/Baht)
      • {EXACT} - The exact indicator when satang is zero (ถ้วน/Only)
      • {FLOAT} - The satang (fractional) part as text
      • {SATANG} - Satang unit word (สตางค์/Satang)
      • {NEGPREFIX} - Negative prefix (ลบ/Minus)
      • {FLOAT?content} - Conditional: show content only if satang is non-zero

      Example:

      
       // Apply same format to both positive and negative
       builder.setFormat("{INTEGER} {UNIT}{FLOAT? ({FLOAT} {SATANG})}", true);
      
       // Apply format only to positive (negative uses standard formatting)
       builder.setFormat("{INTEGER}{UNIT}{FLOAT?{FLOAT}{SATANG}}", false);
       
      Parameters:
      formatString - the custom format string with named placeholders
      applyToNegative - if true, applies the same format to both positive and negative amounts; if false, applies only to positive amounts
      Returns:
      this builder for method chaining
      Throws:
      IllegalArgumentException - if format string is invalid (missing required placeholders)
      Since:
      1.4.0
    • build

      public ThaiBahtConfig build()
      Builds an immutable ThaiBahtConfig instance with the configured values.

      Once built, the configuration is immutable and can be safely shared and reused across threads and application instances. Each call to this method creates a new independent configuration object, allowing builder reuse for multiple configurations.

      Example - reusing builder for multiple configurations:

      
       ThaiBahtConfig.Builder builder = ThaiBahtConfig.builder(new ThaiLanguageHandler());
      
       ThaiBahtConfig config1 = builder.useUnit(true).build();
       ThaiBahtConfig config2 = builder.useUnit(false).build();
       ThaiBahtConfig config3 = builder.languageHandler(new EnglishLanguageHandler()).build();
       
      Returns:
      a new immutable ThaiBahtConfig with the builder's settings