Class ThaiBahtConfig
This class encapsulates formatting preferences for converting numeric amounts into textual
representations. All instances are immutable and thread-safe, making them safe for concurrent
use across application threads. Configuration options are set via the ThaiBahtConfig.Builder class
and cannot be modified after construction.
Configuration Options
- Language: The output language (THAI or ENGLISH). Determines which language handler is used and sets default negative prefixes.
- Unit Words: Whether to include currency unit words in the output. When enabled (default), includes "บาท"/"Baht" and "สตางค์"/"Satang". When disabled, only numeric words appear.
- Formal Mode: Reserved for future language variations (formal vs. casual wording). Currently always treated as true but may enable different conventions in future versions.
- Negative Prefix: The text prefix prepended to negative amounts. Defaults to "ลบ" (Thai) or "Minus" (English) if not explicitly set.
- Format Templates: Custom format strings with named placeholders for precise control over output layout. Separate templates can be used for positive and negative amounts.
Language and Prefix Behavior
Each language has a default negative prefix that is automatically applied when the language is selected:
- Thai: Default prefix "ลบ" (Thai minus symbol)
- English: Default prefix "Minus"
ThaiBahtConfig.Builder.setPrefix(String). Once you set
a custom prefix, it is preserved across language changes.
Format Templates (1.4.0+)
Custom format strings support the following named 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{SATANG?content}- Conditional: show content only if satang is non-zero
Usage Examples
Example 1 - Default configuration:
ThaiBahtConfig config = ThaiBahtConfig.defaultConfig();
// Language: Thai, Units: true, Formal: true, Prefix: "ลบ"
Example 2 - Builder pattern:
ThaiBahtConfig config = ThaiBahtConfig.builder(Language.ENGLISH)
.useUnit(true)
.formal(true)
.setPrefix("Negative:")
.build();
Example 3 - Custom format template:
ThaiBahtConfig config = ThaiBahtConfig.builder()
.setFormatTemplate("{INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}")
.build();
Example 4 - Convert existing to builder:
ThaiBahtConfig newConfig = existingConfig.toBuilder()
.language(Language.ENGLISH)
.useUnit(false)
.build();
Immutability and Thread Safety
This class is fully immutable - all fields are private, final, and initialized at construction time. Instances are safe to share across multiple threads without synchronization. The builder is not thread-safe and should not be shared across threads.
- Since:
- 1.0
- Version:
- 1.4.0
- Author:
- Zazalng
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for constructing immutableThaiBahtConfiginstances. -
Method Summary
Modifier and TypeMethodDescriptionstatic ThaiBahtConfig.Builderbuilder()Creates a newThaiBahtConfig.Builderwith default initialization for building custom configurations.static ThaiBahtConfig.BuilderDeprecated.static ThaiBahtConfig.Builderbuilder(LanguageHandler handler) Creates a newThaiBahtConfig.Builderwith a specific initial language handler.static ThaiBahtConfigObtains the default configuration for currency text conversion.Returns the custom format template for positive amounts.Returns the output language for text conversion.Returns the language handler implementation for this configuration.Returns the custom format template for negative amounts.Returns the negative prefix wording for this configuration.booleanisFormal()Returns whether formal wording rules should be applied.booleanReturns whether unit words are included in the output.Converts this configuration into aThaiBahtConfig.Builderfor modification.
-
Method Details
-
getLanguage
Returns the output language for text conversion.This determines which language handler is used and affects unit words and default prefixes.
- Returns:
- the configured language (THAI or ENGLISH), never
null
-
getLanguageHandler
Returns the language handler implementation for this configuration.This handler is responsible for the actual conversion of amounts to text. It contains the language-specific conversion logic and unit words.
- Returns:
- the LanguageHandler implementation, never
null - Since:
- 2.0.0
-
isUseUnit
public boolean isUseUnit()Returns whether unit words are included in the output.When true, the output includes currency unit words ("บาท"/"Baht" and "สตางค์"/"Satang"). When false, only numeric text is returned.
- Returns:
trueif unit words are included in the textual representation,falseotherwise
-
isFormal
public boolean isFormal()Returns whether formal wording rules should be applied.Currently reserved for future behavior. This flag may enable different wording conventions (formal vs. casual) in future versions. Always true in current implementation.
- Returns:
trueif formal rules are requested
-
getNegativePrefix
Returns the negative prefix wording for this configuration.This string is prepended to negative amounts. It may be explicitly set or default to the language's standard prefix ("ลบ" for Thai, "Minus" for English).
- Returns:
- the current negative prefix, possibly empty string, never
null
-
getFormatTemplate
Returns the custom format template for positive amounts.If a custom format was configured via
ThaiBahtConfig.Builder.setFormatTemplate(String), it is returned here. Otherwise, standard formatting rules are used.- Returns:
- the format template for positive values, or
nullif not configured - Since:
- 1.4.0
-
getNegativeFormatTemplate
Returns the custom format template for negative amounts.If a custom format was configured via
ThaiBahtConfig.Builder.setNegativeFormatTemplate(String), it is returned here. If not configured, the positive format template is used (if any).- Returns:
- the format template for negative values, or
nullif not configured - Since:
- 1.4.0
-
defaultConfig
Obtains the default configuration for currency text conversion.The default configuration is pre-configured with standard settings suitable for most applications:
- Language: THAI
- Unit Words: Enabled (includes "บาท" and "สตางค์")
- Formal Mode: Enabled
- Negative Prefix: Empty string (uses language default "ลบ")
- Format Templates: None (uses standard formatting)
This is a convenient factory method for creating a ready-to-use configuration without building one manually. Use
builder()if you need custom settings.- Returns:
- a default immutable
ThaiBahtConfigsuitable for general use - See Also:
-
toBuilder
Converts this configuration into aThaiBahtConfig.Builderfor modification.This method is useful for creating a modified version of an existing configuration without starting from defaults. The returned builder is initialized with all values from this configuration, including any custom format templates.
Important Prefix Behavior: When you convert to a builder, the current prefix value is marked as "explicitly set". This means that if you subsequently change the language via
ThaiBahtConfig.Builder.language(Language), the prefix will NOT automatically update to the new language's default. It will be preserved as-is. To reset to language defaults after setting a custom prefix, you must:- Create a new builder with
builder(Language), OR - Explicitly set the prefix to null or empty string in the builder
Example - preserving prefix across language change:
ThaiBahtConfig thai = ThaiBahtConfig.defaultConfig(); ThaiBahtConfig modified = thai.toBuilder() .language(Language.ENGLISH) .build(); // Prefix remains "ลบ" (Thai default) even though language is now EnglishExample - modifying multiple settings:
ThaiBahtConfig newConfig = existingConfig.toBuilder() .language(Language.ENGLISH) .useUnit(false) .setPrefix("Negative:") .build();- Returns:
- a new builder initialized with this configuration's values
- Since:
- 1.0
- Create a new builder with
-
builder
Creates a newThaiBahtConfig.Builderwith default initialization for building custom configurations.The returned builder starts with Thai language defaults:
- Language Handler: ThaiLanguageHandler
- Unit Words:
true - Formal Mode:
true - Negative Prefix: Empty string (uses Thai default "ลบ")
- Format Templates: None
Example - English with no units:
ThaiBahtConfig config = ThaiBahtConfig.builder() .languageHandler(new EnglishLanguageHandler()) .useUnit(false) .build();- Returns:
- a new builder instance with Thai language handler as default
- Since:
- 2.0.0
- See Also:
-
builder
Creates a newThaiBahtConfig.Builderwith a specific initial language handler.This factory method is useful when you want to build a configuration with a specific language handler.
Example - create custom language configuration:
ThaiBahtConfig config = ThaiBahtConfig.builder(new EnglishLanguageHandler()) .useUnit(true) .formal(true) .build();- Parameters:
handler- the language handler for the configuration, must not benull- Returns:
- a new builder instance initialized with the specified handler
- Since:
- 2.0.0
- See Also:
-
builder
Deprecated.Usebuilder(LanguageHandler)for new codeCreates a newThaiBahtConfig.Builderwith a specific initial language (backward compatibility).This factory method provides backward compatibility with v1.x code by accepting a Language enum. Internally, it creates the appropriate handler for the language.
Example - create English configuration:
ThaiBahtConfig config = ThaiBahtConfig.builder(Language.ENGLISH) .useUnit(true) .formal(true) .build();- Parameters:
language- the initial language for the configuration, must not benull- Returns:
- a new builder instance initialized with the appropriate handler for the language
- Since:
- 2.0.0
- See Also:
-
builder(LanguageHandler)for new code