Class TextConverter

java.lang.Object
io.github.zazalng.handler.TextConverter

public final class TextConverter extends Object
Internal router and dispatcher for converting numeric amounts to language-specific text.

This package-private utility class serves as the central conversion dispatcher. It is not intended for direct use by library consumers; instead, use the public ThaiBaht API. This class routes conversion requests to the appropriate language-specific handler based on the configured Language.

Conversion Process

The conversion process follows these steps:

  1. Receives a ThaiBaht instance containing an amount and configuration
  2. Validates that the amount and configuration are present
  3. Routes to the appropriate language handler:
    • THAIThaiConvertHandler.convert(ThaiBaht)
    • ENGLISHEnglishConvertHandler.convert(ThaiBaht)
  4. Returns the formatted text from the language handler

Amount Normalization

Each language-specific handler internally normalizes the amount to 2 decimal places using RoundingMode.DOWN (truncation). This ensures consistency in satang precision across all conversions.

Language-Specific Rules

Each language handler implements specialized conversion logic:

  • Thai: Applies Thai grammar rules for special cases like "เอ็ด", "ยี่", and repeating "ล้าน"
  • English: Applies English conventions including hyphens for compound numbers and standard position words (Hundred, Thousand, Million)

Configuration Handling

The converter respects all configuration options from ThaiBahtConfig, including:

  • Unit word inclusion/exclusion
  • Custom format templates
  • Negative amount prefixes
  • Formal wording rules

Thread Safety

This class is stateless and thread-safe. All instance state comes from the input ThaiBaht object, and the conversion algorithm maintains no mutable state.

Since:
1.0
Version:
1.3.0
Author:
Zazalng
See Also:
Implementation Note:
This class is package-private and should not be accessed from outside this package. Use the ThaiBaht public API instead.
  • Method Details

    • toBahtText

      public static String toBahtText(ThaiBaht baht)
      Converts a ThaiBaht instance to language-specific baht text.

      This method is the core internal conversion entry point for the library. It takes a ThaiBaht instance containing an amount and configuration, validates the inputs, and routes the conversion to the language handler in the configuration. The result respects all configuration options.

      Conversion Process

      The conversion process follows these steps:

      1. Receives a ThaiBaht instance containing an amount and configuration
      2. Validates that the amount and configuration are present
      3. Routes to the language handler stored in the configuration via ThaiBahtConfig.getLanguageHandler()
      4. Returns the formatted text from the language handler

      Amount Normalization

      The amount is normalized to 2 decimal places (satang precision) using RoundingMode.DOWN. This ensures that amounts like 100.567 are truncated to 100.56 (56 satang), not rounded.

      Negative Handling

      Negative amounts are handled by the language handler by extracting the absolute value and prepending the configured negative prefix (or handler's default if not specified).

      Example conversions:

      
       // Thai positive
       ThaiBaht baht1 = ThaiBaht.create(new BigDecimal("1234.56"));
       String thai = TextConverter.toBahtText(baht1);
       // Result: "หนึ่งพันสองร้อยสามสิบสี่บาทห้าสิบหกสตางค์"
      
       // English with units
       ThaiBahtConfig engConfig = ThaiBahtConfig.builder(new EnglishLanguageHandler())
           .useUnit(true).build();
       ThaiBaht baht2 = ThaiBaht.create(new BigDecimal("500.50"), engConfig);
       String english = TextConverter.toBahtText(baht2);
       // Result: "Five Hundred Baht Fifty Satang"
      
       // Negative amount
       ThaiBaht baht3 = ThaiBaht.create(new BigDecimal("-100"));
       String negative = TextConverter.toBahtText(baht3);
       // Result: "ลบหนึ่งร้อยบาทถ้วน"
       
      Parameters:
      baht - the ThaiBaht instance containing amount and configuration, must not be null
      Returns:
      the language-specific textual representation of the amount, never null
      Throws:
      IllegalArgumentException - if the amount is null
      Since:
      2.0.0
      See Also: