Class Messages


  • public final class Messages
    extends java.lang.Object
    Contains utility methods for managing messages.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String format​(java.lang.String messageTemplate, java.lang.Object arg)
      Formats a templated message inserting a single argument.
      static java.lang.String format​(java.lang.String messageTemplate, java.lang.Object... args)
      Formats a templated message inserting arguments.
      static Pair<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatWithAttributes​(java.lang.String messageTemplate, java.lang.Object... args)
      Formats a templated message inserting named arguments.
      • Methods inherited from class java.lang.Object

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

      • format

        public static java.lang.String format​(java.lang.String messageTemplate,
                                              java.lang.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 java.lang.String format​(java.lang.String messageTemplate,
                                              java.lang.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<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatWithAttributes​(java.lang.String messageTemplate,
                                                                                                                               java.lang.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