Class Messages


  • public final class Messages
    extends Object
    Contains utility methods for managing messages.
    • Method Detail

      • format

        public static String format​(String messageTemplate,
                                    Object arg)
        Formats a templated message inserting a single argument.

        This method combines a template message with a single argument. It can be useful to delay string concatenation, which is sometimes a performance issue. The approach is similar to SLF4J MessageFormat, Guava Preconditions and String format().

        The message template contains zero to many "{}" placeholders. The first placeholder is replaced by the string form of the argument. Subsequent placeholders are not replaced. If there is no placeholder, then the argument is appended to the end of the message. No attempt is made to format the argument.

        This method is null tolerant to ensure that use in exception construction will not throw another exception, which might hide the intended exception.

        Parameters:
        messageTemplate - the message template with "{}" placeholders, null returns empty string
        arg - the message argument, null treated as string "null"
        Returns:
        the formatted message
      • format

        public static String format​(String messageTemplate,
                                    Object... args)
        Formats a templated message inserting arguments.

        This method combines a template message with a list of specific arguments. It can be useful to delay string concatenation, which is sometimes a performance issue. The approach is similar to SLF4J MessageFormat, Guava Preconditions and String format().

        The message template contains zero to many "{}" placeholders. Each placeholder is replaced by the next available argument. If there are too few arguments, then the message will be left with placeholders. If there are too many arguments, then the excess arguments are appended to the end of the message. No attempt is made to format the arguments.

        This method is null tolerant to ensure that use in exception construction will not throw another exception, which might hide the intended exception.

        Parameters:
        messageTemplate - the message template with "{}" placeholders, null returns empty string
        args - the message arguments, null treated as empty array
        Returns:
        the formatted message
      • formatWithAttributes

        public static Pair<String,​Map<String,​String>> formatWithAttributes​(String messageTemplate,
                                                                                       Object... args)
        Formats a templated message inserting named arguments.

        A typical template would look like:

         Messages.formatWithAttributes("Foo={foo}, Bar={}", "abc", 123)
         
        This will return a Pair with a String and a Map. The String will be the formatted message: "Foo=abc, Bar=123". The Map will look like: {"foo": "123"}.

        This method combines a template message with a list of specific arguments. It can be useful to delay string concatenation, which is sometimes a performance issue. The approach is similar to SLF4J MessageFormat, Guava Preconditions and String format().

        The message template contains zero to many "{name}" placeholders. Each placeholder is replaced by the next available argument. If there are too few arguments, then the message will be left with placeholders. If there are too many arguments, then the excess arguments are appended to the message. No attempt is made to format the arguments.

        This method is null tolerant to ensure that use in exception construction will not throw another exception, which might hide the intended exception.

        Parameters:
        messageTemplate - the message template with "{}" placeholders, null returns empty string
        args - the message arguments, null treated as empty array
        Returns:
        the formatted message