public class Template extends MessageFormat
It is extended from MessageFormat to allow parameters
to be substituted by name as well as by position.
The following example, using MessageFormat, yields "Happy 64th birthday, Ringo!":
MessageFormat f = new MessageFormat("Happy {0,number}th birthday, {1}!");
Object[] args = {64, "Ringo"};
System.out.println(f.format(args);
Here is the same example using a Template and named parameters:
Template f = new Template("Happy {age,number}th birthday, {name}!");
Map<Object, Object> args = new HashMap<Object, Object>();
args.put("age", 64);
args.put("name", "Ringo");
System.out.println(f.format(args);
Using a Template you can also use positional parameters:
Template f = new Template("Happy {age,number}th birthday, {name}!");
Object[] args = {64, "Ringo"};
System.out.println(f.format(args);
Or a hybrid; here, one argument is specified by name, another by position:
Template f = new Template("Happy {age,number}th birthday, {name}!");
Map<Object, Object> args = new HashMap<Object, Object>();
args.put(0, 64);
args.put("name", "Ringo");
System.out.println(f.format(args);
MessageFormat.Field| Modifier and Type | Field and Description |
|---|---|
private List<String> |
parameterNames |
| Modifier | Constructor and Description |
|---|---|
private |
Template(String pattern,
List<String> parameterNames,
Locale locale) |
| Modifier and Type | Method and Description |
|---|---|
String |
format(Map<Object,Object> argMap)
Formats a set of arguments to produce a string.
|
static String |
formatByName(String pattern,
Map<Object,Object> argMap)
Creates a Template with the given pattern and uses it
to format the given arguments.
|
private Object |
getArg(Map<Object,Object> argMap,
int ordinal)
Returns the value of the
ordinalth argument. |
List<String> |
getParameterNames()
Returns the names of the parameters, in the order that they appeared in
the template string.
|
private static void |
makeFormat(StringBuilder[] segments,
List<String> parameterNames)
Called when a complete parameter has been seen.
|
static Template |
of(String pattern)
Creates a Template for the default locale and the
specified pattern.
|
static Template |
of(String pattern,
Locale locale)
Creates a Template for the specified locale and
pattern.
|
private static String |
process(String pattern,
List<String> parameterNames)
Parses the pattern, populates the parameter names, and returns the
pattern with parameter names converted to parameter ordinals.
|
applyPattern, clone, equals, format, format, format, formatToCharacterIterator, getFormats, getFormatsByArgumentIndex, getLocale, hashCode, parse, parse, parseObject, setFormat, setFormatByArgumentIndex, setFormats, setFormatsByArgumentIndex, setLocale, toPatternformat, parseObjectpublic static Template of(String pattern)
pattern - the pattern for this message formatIllegalArgumentException - if the pattern is invalidpublic static Template of(String pattern, Locale locale)
pattern - the pattern for this message formatlocale - the locale for this message formatIllegalArgumentException - if the pattern is invalidprivate static String process(String pattern, List<String> parameterNames)
To ensure that the same parsing rules apply, this code is copied from
MessageFormat.applyPattern(String) but with different
actions when a parameter is recognized.
pattern - PatternparameterNames - Names of parameters (output)private static void makeFormat(StringBuilder[] segments, List<String> parameterNames)
segments - Comma-separated segments of the parameter definitionparameterNames - List of parameter names seen so farpublic String format(Map<Object,Object> argMap)
Arguments may appear in the map using named keys (of type String), or positional keys (0-based ordinals represented either as type String or Integer).
argMap - A map containing the arguments as (key, value) pairsIllegalArgumentException - if the Format cannot format the given
objectprivate Object getArg(Map<Object,Object> argMap, int ordinal)
ordinalth argument.argMap - Map of argument valuesordinal - Ordinal of argumentpublic static String formatByName(String pattern, Map<Object,Object> argMap)
Template(pattern).format(java.util.Map<java.lang.Object, java.lang.Object>)(args)
IllegalArgumentException - if the pattern is invalid,
or if an argument in the
arguments array is not of the
type expected by the format element(s)
that use it.Copyright © 2012–2018 The Apache Software Foundation. All rights reserved.