Class FormatApplier

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

public final class FormatApplier extends Object
Internal utility for applying custom format templates to baht/satang conversions.

This package-private class handles the substitution of named placeholders in custom format strings with actual numeric text values and language-specific unit words. It enables flexible output formatting for both positive and negative amounts while maintaining support for conditional placeholders. Not intended for direct use; accessed through the public ThaiBaht API via configuration.

Placeholder Types

Standard Placeholders (always available)

  • {INTEGER} - The baht (integer) part as text (e.g., "หนึ่งพัน", "One Thousand")
  • {FLOAT} - The satang (fractional) part as text (e.g., "ห้าสิบหก", "Fifty-Six")
  • {UNIT} - Currency unit word for this language ("บาท"/"Baht")
  • {EXACT} - The exact/only indicator when satang is zero ("ถ้วน"/"Only")
  • {SATANG} - Satang unit word for this language ("สตางค์"/"Satang")
  • {NEGPREFIX} - Negative prefix for this language/config ("ลบ"/"Minus")

Conditional Placeholders (smart inclusion/exclusion)

Conditional placeholders show content only when the satang value is non-zero:

  • {FLOAT?content} - Show content only if satang ≠ zero. Useful for omitting satang when it's zero.
  • {SATANG?content} - Show content only if satang ≠ zero. Useful for conditional unit word inclusion.

Important: Conditional logic works by checking if the value equals the language's "zero" representation. For Thai: "ศูนย์", for English: "Zero".

Format Template Examples

Thai Examples

Format Template Input: 100.50 Input: 100.00
{INTEGER}{UNIT}{FLOAT?{FLOAT}{SATANG}} หนึ่งร้อยบาทห้าสิบสตางค์ หนึ่งร้อยบาท
{INTEGER}{UNIT}{EXACT}{FLOAT?และ{FLOAT}{SATANG}} หนึ่งร้อยบาทและห้าสิบสตางค์ หนึ่งร้อยบาทถ้วน
[{INTEGER} {UNIT}]{FLOAT? ({FLOAT} {SATANG})} [หนึ่งร้อย บาท] (ห้าสิบ สตางค์) [หนึ่งร้อย บาท]

English Examples

Format Template Input: 100.50 Input: 100.00
{INTEGER} {UNIT}{FLOAT? {FLOAT} {SATANG}} One Hundred Baht Fifty Satang One Hundred Baht
{INTEGER} {UNIT} {EXACT}{FLOAT?and {FLOAT} {SATANG}} One Hundred Baht and Fifty Satang One Hundred Baht Only

Processing Algorithm

The formatter processes templates in the following order:

  1. Conditional placeholders first: {FLOAT?...} and {SATANG?...} are processed. These use brace-matching logic to find content boundaries, supporting nested braces.
  2. Exact value handling: {EXACT} is replaced with the exact/only word if satang = 0, otherwise replaced with empty string.
  3. Standard placeholders: {INTEGER}, {FLOAT}, {UNIT}, {SATANG}, {NEGPREFIX} are replaced with their corresponding values.

Nesting and Complexity

The formatter supports arbitrary complexity including:

  • Nested braces within conditional blocks
  • Multiple conditional blocks in one template
  • Arbitrary text between and around placeholders
  • Conditional blocks wrapping multiple placeholders (e.g., {FLOAT?and {FLOAT} {SATANG}})

Example - complex nested template:


 Format: "Amount: {INTEGER} {UNIT}{FLOAT?{FLOAT? (with {FLOAT})} {SATANG}}"
 Input: 100.50
 Output: "Amount: One Hundred Baht (with Fifty) Satang"
 

Thread Safety

This class is stateless and thread-safe. All state comes from method parameters.

Since:
1.4.0
Author:
Zazalng
See Also: