Class SimpleFormatter

java.lang.Object
com.ibm.icu.text.SimpleFormatter

public final class SimpleFormatter extends 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:
  • Method Details

    • compile

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

      public static SimpleFormatter compileMinMaxArguments(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:
      IllegalArgumentException - for bad argument syntax and too few or too many arguments.
    • getArgumentLimit

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

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

      public StringBuilder formatAndAppend(StringBuilder appendTo, int[] offsets, 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 StringBuilder formatAndReplace(StringBuilder result, int[] offsets, 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 String toString()
      Returns a string similar to the original pattern, only for debugging.
      Overrides:
      toString in class Object
    • getTextWithNoArguments

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