Class StringFormat
This class provides a thread-safe, cacheable formatter for printf-style (Formatter) patterns
(StringUtils.mformat(String, Object...) / Shorts.mf(...)/Shorts.mfs(...)
(backed by MessageFormat).
Features:
- Printf grammar:
"%s" ,"%d" ,"%.2f" ,"%1$s" , etc. - Thread-Safe: Immutable class, safe for concurrent use
- Cacheable: Use
ofPrintf(String)for cached instances - Lenient null handling:
null arguments render as"null" for string conversions (rather than throwing) and a fast path avoidsFormatterfor simple"%s" tokens
Examples:
Caching:
Use ofPrintf(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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanFormats the pattern with the given arguments using the default locale.Formats the pattern with the given arguments using the specified locale.static StringformatPrintf(String pattern, Object... args) Formats a pattern string with the given arguments using printf-style semantics.static StringformatPrintf(String pattern, Locale locale, Object... args) Formats a pattern string with the given arguments using printf-style semantics and the specified locale.inthashCode()static StringFormatReturns a cached printf 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 printf StringFormat instance.{ and' are treated as literal text (genuineFormattersemantics); only printf-style placeholders are recognized.- Parameters:
pattern- The format pattern.- Throws:
IllegalArgumentException- If the pattern isnull .
-
-
Method Details
-
formatPrintf
Formats a pattern string with the given arguments using printf-style semantics.{ and' are literal text, matchingFormattersemantics. If no arguments are passed in, the pattern is 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.
-
formatPrintf
Formats a pattern string with the given arguments using printf-style semantics and the specified locale.- 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.
-
ofPrintf
Returns a cached printf 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.
{ and' are treated as literal text (genuineFormattersemantics); only printf-style placeholders are recognized.- 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 locale-specific printf 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 - 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
- S = StringFormat (printf) token
- Format character: 's' = simple, 'z' = complex printf
- Index: 0-based argument index
- Content: The format string content (for complex tokens)
Examples:
StringFormat
fmt = StringFormat.ofPrintf ("Hello %s, you have %d items" );fmt .toPattern ();// Returns: "[L:Hello ][S:s0:%s][L:, you have ][S:d1:%d][L: items]" - Returns:
- A debug string showing the parsed token structure.
- Literal tokens:
-
toString
-