Class MessageFormats


  • public class MessageFormats
    extends java.lang.Object
    The message formatting relevant utilities.

    See also java.text.MessageFormat.

    Author:
    tomyeh
    • Constructor Summary

      Constructors 
      Constructor Description
      MessageFormats()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.StringBuffer format​(java.lang.StringBuffer result, java.lang.String pattern, java.lang.Object[] args)
      Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of StringBuffer and Locales.getCurrent().
      static java.lang.StringBuffer format​(java.lang.StringBuffer result, java.lang.String pattern, java.lang.Object[] args, java.util.Locale locale)
      Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of StringBuffer.
      static java.lang.String format​(java.lang.String pattern, java.lang.Object[] args)
      Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of Locales.getCurrent().
      static java.lang.String format​(java.lang.String pattern, java.lang.Object[] args, java.util.Locale locale)
      Creates a MessageFormat with the given pattern and uses it to format the given arguments.
      static java.lang.String formatByName​(java.lang.String pattern, java.util.Map<java.lang.String,​?> mapping)
      Formats a pattern by substituting names with values found in the giving map, by use of Locales.getCurrent().
      static java.lang.String formatByName​(java.lang.String pattern, java.util.Map<java.lang.String,​?> mapping, java.util.Locale locale)
      Formats a pattern by substituting names, enclosing with {}, with values found in the giving map.
      static MessageFormats.NameInfo parseByName​(java.lang.String pattern)
      Parses a pattern and converts it to the format of java.text.MessageFormat.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MessageFormats

        public MessageFormats()
    • Method Detail

      • format

        public static final java.lang.String format​(java.lang.String pattern,
                                                    java.lang.Object[] args,
                                                    java.util.Locale locale)
        Creates a MessageFormat with the given pattern and uses it to format the given arguments.

        An extension to java.text.MessageFormat.format by allowing to specify a Locale.

        Parameters:
        locale - the locale; null for Locales.getCurrent()
      • format

        public static final java.lang.String format​(java.lang.String pattern,
                                                    java.lang.Object[] args)
        Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of Locales.getCurrent().

        Note: java.text.MessageFormat.format uses Locale.getDefault, which might not be correct in a multi-user environment.

      • format

        public static final java.lang.StringBuffer format​(java.lang.StringBuffer result,
                                                          java.lang.String pattern,
                                                          java.lang.Object[] args,
                                                          java.util.Locale locale)
        Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of StringBuffer.

        If you want to concatenate a sequence of formated string, use this method.

      • format

        public static final java.lang.StringBuffer format​(java.lang.StringBuffer result,
                                                          java.lang.String pattern,
                                                          java.lang.Object[] args)
        Creates a MessageFormat with the given pattern and uses it to format the given arguments, by use of StringBuffer and Locales.getCurrent().
      • parseByName

        public static final MessageFormats.NameInfo parseByName​(java.lang.String pattern)
        Parses a pattern and converts it to the format of java.text.MessageFormat. Names are the first element of a substring enclosing with {} in the pattern.

        Example: "{a} is {b,number} of {a}" will return new NameInfo("{0} is {1,number} of {0}", {"a", "b"}).

        See Also:
        formatByName(String, Map, Locale)
      • formatByName

        public static final java.lang.String formatByName​(java.lang.String pattern,
                                                          java.util.Map<java.lang.String,​?> mapping,
                                                          java.util.Locale locale)
        Formats a pattern by substituting names, enclosing with {}, with values found in the giving map.

        This is an extension to java.text.MessageFormat. The only difference is that this method use arbitrary name instead of a number. For example, it use {name} instead of {0}. The quotation rule and formating pattern are the same as java.text.MessageFormat. Example, {var,number,$'#',###}.

        It actually uses parseByName(java.lang.String) to convert names to numbers, and then passes to java.text.MessageFormat.

        Parameters:
        locale - the locale; null for Locales.getCurrent()
        Throws:
        java.lang.IllegalArgumentException - if the pattern is invalid
        See Also:
        parseByName(java.lang.String)
      • formatByName

        public static final java.lang.String formatByName​(java.lang.String pattern,
                                                          java.util.Map<java.lang.String,​?> mapping)
        Formats a pattern by substituting names with values found in the giving map, by use of Locales.getCurrent().
        See Also:
        parseByName(java.lang.String)