Class StringFormat
This class provides a thread-safe, cacheable formatter that can handle mixed format styles in a single pattern.
It supports both MessageFormat syntax (
Features:
- Dual Format Support: Mix MessageFormat and printf-style placeholders in the same pattern
- Thread-Safe: Immutable class, safe for concurrent use
- Cacheable: Use
of(String)for cached instances - Argument Sharing: Both format styles share the same argument array
Format Style Detection:
The formatter automatically detects which style to use for each placeholder:
- MessageFormat style:
"{0}" ,"{1,number}" ,"{2,date}" , etc. - Printf style:
"%s" ,"%d" ,"%.2f" ,"%1$s" , etc.
Argument Mapping:
Arguments are processed in order of appearance:
- MessageFormat placeholders: Use explicit indices (e.g.,
"{0}" usesargs[0] ) - Printf placeholders: Use sequential indices starting after the highest MessageFormat index
Examples:
Caching:
Use of(String) to get cached instances. The cache is thread-safe and limited to 1000 entries.
For uncached instances, use the constructor directly.
Thread Safety:
This class is immutable and thread-safe. Multiple threads can safely use the same instance concurrently.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanFormats the pattern with the given arguments using the default locale.static StringFormats a pattern string with the given arguments using the default locale.static StringFormats a pattern string with the given arguments using the specified locale.Formats the pattern with the given arguments using the specified locale.inthashCode()static StringFormatReturns a cached StringFormat instance for the given pattern.Returns a debug representation of the parsed pattern showing the token structure.toString()
-
Constructor Details
-
StringFormat
Creates a new StringFormat instance.- Parameters:
pattern- The format pattern. Can contain both MessageFormat and printf-style placeholders.- Throws:
IllegalArgumentException- If the pattern isnull .
-
-
Method Details
-
format
Formats a pattern string with the given arguments using the specified locale.This is a convenience method that creates a StringFormat instance and formats it. If no arguments are passed in, the pattern is returned as-is.
- Parameters:
pattern- The format pattern.locale- The locale to use for formatting. Ifnull , uses the default locale.args- The arguments to format.- Returns:
- The formatted string.
- Throws:
IllegalArgumentException- If the pattern isnull or format specifiers are invalid.
-
format
Formats a pattern string with the given arguments using the default locale.This is a convenience method that creates a StringFormat instance and formats it. If no arguments are passed in, the pattern is simply returned as-is.
- Parameters:
pattern- The format pattern.args- The arguments to format.- Returns:
- The formatted string.
- Throws:
IllegalArgumentException- If the pattern isnull or format specifiers are invalid.
-
of
Returns a cached StringFormat instance for the given pattern.This method uses a thread-safe cache to avoid recreating StringFormat instances for the same pattern. The cache is limited to 1000 entries.
- Parameters:
pattern- The format pattern.- Returns:
- A cached or new StringFormat instance.
- Throws:
IllegalArgumentException- If the pattern isnull .
-
equals
-
format
Formats the pattern with the given arguments using the specified locale.The locale affects both MessageFormat and printf-style formatting:
- MessageFormat: Locale-specific number, date, and time formatting
- Printf: Locale-specific number formatting (decimal separators, etc.)
- Parameters:
locale- The locale to use for formatting. Ifnull , uses the default locale.args- The arguments to format.- Returns:
- The formatted string.
- Throws:
IllegalArgumentException- If format specifiers are invalid or arguments don't match.
-
format
Formats the pattern with the given arguments using the default locale.- Parameters:
args- The arguments to format.- Returns:
- The formatted string.
- Throws:
IllegalArgumentException- If format specifiers are invalid or arguments don't match.
-
hashCode
-
toPattern
Returns a debug representation of the parsed pattern showing the token structure.This method is useful for debugging and understanding how a pattern was parsed. It returns a string showing each token in the format:
- Literal tokens:
"[L:text]" - Literal text - MessageFormat tokens (simple):
"[M:s0]" - Simple MessageFormat placeholder (format='s', index=0) - MessageFormat tokens (complex):
"[M:o0:{0,number,currency}]" - Complex MessageFormat placeholder (format='o', index=0, content) - StringFormat tokens (simple):
"[S:s0:%s]" - Simple printf placeholder (format='s', index=0, content) - StringFormat tokens (complex):
"[S:z0:%.2f]" - Complex printf placeholder (format='z', index=0, content)
Token Format:
- L = Literal token
- M = MessageFormat token
- S = StringFormat (printf) token
- Format character: 's' = simple, 'o' = other/complex, 'z' = complex printf
- Index: 0-based argument index
- Content: The format string content (for complex tokens)
Examples:
StringFormat
fmt = StringFormat.of ("Hello {0}, you have %d items" );fmt .toPattern ();// Returns: "[L:Hello ][M:s0][L:, you have ][S:d1:%d][L: items]" - Returns:
- A debug string showing the parsed token structure.
- Literal tokens:
-
toString
-