Class SimplePatternFormatter


  • public class SimplePatternFormatter
    extends Object
    Compiled version of a pattern such as "{1} was born in {0}".

    Using SimplePatternFormatter objects is both faster and safer than adhoc replacement such as pattern.replace("{0}", "Colorado").replace("{1} "Fred");. They are faster because they are precompiled; they are safer because they account for curly braces escaped by apostrophe ('). Placeholders are of the form \{[0-9]+\}. If a curly brace is preceded by a single quote, it becomes a curly brace instead of the start of a placeholder. Two single quotes resolve to one single quote.

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

    Example:

     SimplePatternFormatter fmt = SimplePatternFormatter.compile("{1} '{born} in {0}");
     
     // Output: "paul {born} in england"
     System.out.println(fmt.format("england", "paul"));
     
    • Method Detail

      • compile

        public static SimplePatternFormatter compile​(String pattern)
        Compiles a string.
        Parameters:
        pattern - The string.
        Returns:
        the new SimplePatternFormatter object.
      • getPlaceholderCount

        public int getPlaceholderCount()
        Returns the max placeholder ID + 1.
      • formatAndAppend

        public StringBuilder formatAndAppend​(StringBuilder appendTo,
                                             int[] offsets,
                                             CharSequence... values)
        Formats the given values.
        Parameters:
        appendTo - the result appended here.
        offsets - position of first value in appendTo stored in offsets[0]; second in offsets[1]; third in offsets[2] etc. An offset of -1 means that the corresponding value is not in appendTo. offsets.length and values.length may differ. If offsets.length < values.length then only the first offsets are written out; If offsets.length > values.length then the extra offsets get -1. If caller is not interested in offsets, caller may pass null here.
        values - the placeholder values. A placeholder value may not be the same object as appendTo.
        Returns:
        appendTo
      • formatAndReplace

        public StringBuilder formatAndReplace​(StringBuilder result,
                                              int[] offsets,
                                              CharSequence... values)
        Formats the given values.
        Parameters:
        result - The result is stored here overwriting any previously stored value.
        offsets - position of first value in result stored in offsets[0]; second in offsets[1]; third in offsets[2] etc. An offset of -1 means that the corresponding value is not in result. offsets.length and values.length may differ. If offsets.length < values.length then only the first offsets are written out; If offsets.length > values.length then the extra offsets get -1. If caller is not interested in offsets, caller may pass null here.
        values - the placeholder values. A placeholder value may be result itself in which case The previous value of result is used.
        Returns:
        result
      • toString

        public String toString()
        Formats this object using values {0}, {1} etc. Note that this is not the same as the original pattern string used to build this object.
        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.
      • getPatternWithNoPlaceholders

        public String getPatternWithNoPlaceholders()
        Returns this pattern with none of the placeholders.