Package com.ibm.icu.impl
Class SimplePatternFormatter
- java.lang.Object
-
- com.ibm.icu.impl.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SimplePatternFormattercompile(String pattern)Compiles a string.Stringformat(CharSequence... values)Formats the given values.StringBuilderformatAndAppend(StringBuilder appendTo, int[] offsets, CharSequence... values)Formats the given values.StringBuilderformatAndReplace(StringBuilder result, int[] offsets, CharSequence... values)Formats the given values.StringgetPatternWithNoPlaceholders()Returns this pattern with none of the placeholders.intgetPlaceholderCount()Returns the max placeholder ID + 1.StringtoString()Formats this object using values {0}, {1} etc.
-
-
-
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.
-
format
public String format(CharSequence... values)
Formats the given values.
-
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.
-
getPatternWithNoPlaceholders
public String getPatternWithNoPlaceholders()
Returns this pattern with none of the placeholders.
-
-