Class SimpleFormatter


  • public final class SimpleFormatter
    extends java.lang.Object
    Formats simple patterns like "{1} was born in {0}". Minimal subset of MessageFormat; fast, simple, minimal dependencies. Supports only numbered arguments with no type nor style parameters, and formats only string values. Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.

    Factory methods throw exceptions for syntax errors and for too few or too many arguments/placeholders.

    SimpleFormatter objects are immutable and can be safely cached like strings.

    Example:

     SimpleFormatter fmt = SimpleFormatter.compile("{1} '{born}' in {0}");
    
     // Output: "paul {born} in england"
     System.out.println(fmt.format("england", "paul"));
     
    See Also:
    MessageFormat, MessagePattern.ApostropheMode
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String compiledPattern
      Binary representation of the compiled pattern.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SimpleFormatter​(java.lang.String compiledPattern)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static SimpleFormatter compile​(java.lang.CharSequence pattern)
      Creates a formatter from the pattern string.
      static SimpleFormatter compileMinMaxArguments​(java.lang.CharSequence pattern, int min, int max)
      Creates a formatter from the pattern string.
      java.lang.String format​(java.lang.CharSequence... values)
      Formats the given values.
      java.lang.StringBuilder formatAndAppend​(java.lang.StringBuilder appendTo, int[] offsets, java.lang.CharSequence... values)
      Formats the given values, appending to the appendTo builder.
      java.lang.StringBuilder formatAndReplace​(java.lang.StringBuilder result, int[] offsets, java.lang.CharSequence... values)
      Formats the given values, replacing the contents of the result builder.
      int getArgumentLimit()  
      java.lang.String getTextWithNoArguments()
      Returns the pattern text with none of the arguments.
      java.lang.String toString()
      Returns a string similar to the original pattern, only for debugging.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • compiledPattern

        private final java.lang.String compiledPattern
        Binary representation of the compiled pattern.
        See Also:
        SimpleFormatterImpl
    • Constructor Detail

      • SimpleFormatter

        private SimpleFormatter​(java.lang.String compiledPattern)
    • Method Detail

      • compile

        public static SimpleFormatter compile​(java.lang.CharSequence pattern)
        Creates a formatter from the pattern string.
        Parameters:
        pattern - The pattern string.
        Returns:
        The new SimpleFormatter object.
        Throws:
        java.lang.IllegalArgumentException - for bad argument syntax.
      • compileMinMaxArguments

        public static SimpleFormatter compileMinMaxArguments​(java.lang.CharSequence pattern,
                                                             int min,
                                                             int max)
        Creates a formatter from the pattern string. The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.
        Parameters:
        pattern - The pattern string.
        min - The pattern must have at least this many arguments.
        max - The pattern must have at most this many arguments.
        Returns:
        The new SimpleFormatter object.
        Throws:
        java.lang.IllegalArgumentException - for bad argument syntax and too few or too many arguments.
      • getArgumentLimit

        public int getArgumentLimit()
        Returns:
        The max argument number + 1.
      • format

        public java.lang.String format​(java.lang.CharSequence... values)
        Formats the given values.
      • formatAndAppend

        public java.lang.StringBuilder formatAndAppend​(java.lang.StringBuilder appendTo,
                                                       int[] offsets,
                                                       java.lang.CharSequence... values)
        Formats the given values, appending to the appendTo builder.
        Parameters:
        appendTo - Gets the formatted pattern and values appended.
        offsets - offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.
        values - The argument values. An argument value must not be the same object as appendTo. values.length must be at least getArgumentLimit(). Can be null if getArgumentLimit()==0.
        Returns:
        appendTo
      • formatAndReplace

        public java.lang.StringBuilder formatAndReplace​(java.lang.StringBuilder result,
                                                        int[] offsets,
                                                        java.lang.CharSequence... values)
        Formats the given values, replacing the contents of the result builder. May optimize by actually appending to the result if it is the same object as the value corresponding to the initial argument in the pattern.
        Parameters:
        result - Gets its contents replaced by the formatted pattern and values.
        offsets - offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.
        values - The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().
        Returns:
        result
      • toString

        public java.lang.String toString()
        Returns a string similar to the original pattern, only for debugging.
        Overrides:
        toString in class java.lang.Object
      • getTextWithNoArguments

        public java.lang.String getTextWithNoArguments()
        Returns the pattern text with none of the arguments. Like formatting with all-empty string values.