public class SimplePatternFormatter extends Object
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"));
| Modifier and Type | Method and Description |
|---|---|
static SimplePatternFormatter |
compile(String pattern)
Compiles a string.
|
String |
format(CharSequence... values)
Formats the given values.
|
StringBuilder |
formatAndAppend(StringBuilder appendTo,
int[] offsets,
CharSequence... values)
Formats the given values.
|
StringBuilder |
formatAndReplace(StringBuilder result,
int[] offsets,
CharSequence... values)
Formats the given values.
|
String |
getPatternWithNoPlaceholders()
Returns this pattern with none of the placeholders.
|
int |
getPlaceholderCount()
Returns the max placeholder ID + 1.
|
String |
toString()
Formats this object using values {0}, {1} etc.
|
public static SimplePatternFormatter compile(String pattern)
pattern - The string.public int getPlaceholderCount()
public String format(CharSequence... values)
public StringBuilder formatAndAppend(StringBuilder appendTo, int[] offsets, CharSequence... values)
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.public StringBuilder formatAndReplace(StringBuilder result, int[] offsets, CharSequence... values)
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.public String toString()
public String getPatternWithNoPlaceholders()