Class Template

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class Template
    extends java.text.MessageFormat
    String template.

    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);
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.text.MessageFormat

        java.text.MessageFormat.Field
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.lang.String> parameterNames  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Template​(java.lang.String pattern, java.util.List<java.lang.String> parameterNames, java.util.Locale locale)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String format​(java.util.Map<java.lang.Object,​java.lang.Object> argMap)
      Formats a set of arguments to produce a string.
      static java.lang.String formatByName​(java.lang.String pattern, java.util.Map<java.lang.Object,​java.lang.Object> argMap)
      Creates a Template with the given pattern and uses it to format the given arguments.
      private java.lang.Object getArg​(java.util.Map<java.lang.Object,​java.lang.Object> argMap, int ordinal)
      Returns the value of the ordinalth argument.
      java.util.List<java.lang.String> getParameterNames()
      Returns the names of the parameters, in the order that they appeared in the template string.
      private static void makeFormat​(java.lang.StringBuilder[] segments, java.util.List<java.lang.String> parameterNames)
      Called when a complete parameter has been seen.
      static Template of​(java.lang.String pattern)
      Creates a Template for the default locale and the specified pattern.
      static Template of​(java.lang.String pattern, java.util.Locale locale)
      Creates a Template for the specified locale and pattern.
      private static java.lang.String process​(java.lang.String pattern, java.util.List<java.lang.String> parameterNames)
      Parses the pattern, populates the parameter names, and returns the pattern with parameter names converted to parameter ordinals.
      • Methods inherited from class java.text.MessageFormat

        applyPattern, clone, equals, format, format, format, formatToCharacterIterator, getFormats, getFormatsByArgumentIndex, getLocale, hashCode, parse, parse, parseObject, setFormat, setFormatByArgumentIndex, setFormats, setFormatsByArgumentIndex, setLocale, toPattern
      • Methods inherited from class java.text.Format

        format, parseObject
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • parameterNames

        private final java.util.List<java.lang.String> parameterNames
    • Constructor Detail

      • Template

        private Template​(java.lang.String pattern,
                         java.util.List<java.lang.String> parameterNames,
                         java.util.Locale locale)
    • Method Detail

      • of

        public static Template of​(java.lang.String pattern)
        Creates a Template for the default locale and the specified pattern.
        Parameters:
        pattern - the pattern for this message format
        Throws:
        java.lang.IllegalArgumentException - if the pattern is invalid
      • of

        public static Template of​(java.lang.String pattern,
                                  java.util.Locale locale)
        Creates a Template for the specified locale and pattern.
        Parameters:
        pattern - the pattern for this message format
        locale - the locale for this message format
        Throws:
        java.lang.IllegalArgumentException - if the pattern is invalid
      • process

        private static java.lang.String process​(java.lang.String pattern,
                                                java.util.List<java.lang.String> parameterNames)
        Parses the pattern, populates the parameter names, and returns the pattern with parameter names converted to parameter ordinals.

        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.

        Parameters:
        pattern - Pattern
        parameterNames - Names of parameters (output)
        Returns:
        Pattern with named parameters substituted with ordinals
      • makeFormat

        private static void makeFormat​(java.lang.StringBuilder[] segments,
                                       java.util.List<java.lang.String> parameterNames)
        Called when a complete parameter has been seen.
        Parameters:
        segments - Comma-separated segments of the parameter definition
        parameterNames - List of parameter names seen so far
      • format

        public java.lang.String format​(java.util.Map<java.lang.Object,​java.lang.Object> argMap)
        Formats a set of arguments to produce a string.

        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).

        Parameters:
        argMap - A map containing the arguments as (key, value) pairs
        Returns:
        Formatted string.
        Throws:
        java.lang.IllegalArgumentException - if the Format cannot format the given object
      • getArg

        private java.lang.Object getArg​(java.util.Map<java.lang.Object,​java.lang.Object> argMap,
                                        int ordinal)
        Returns the value of the ordinalth argument.
        Parameters:
        argMap - Map of argument values
        ordinal - Ordinal of argument
        Returns:
        Value of argument
      • formatByName

        public static java.lang.String formatByName​(java.lang.String pattern,
                                                    java.util.Map<java.lang.Object,​java.lang.Object> argMap)
        Creates a Template with the given pattern and uses it to format the given arguments. This is equivalent to
        Template(pattern).format(java.util.Map<java.lang.Object, java.lang.Object>)(args)
        Throws:
        java.lang.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.
      • getParameterNames

        public java.util.List<java.lang.String> getParameterNames()
        Returns the names of the parameters, in the order that they appeared in the template string.
        Returns:
        List of parameter names