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, returning the implied attribute map.

        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": "abc"}.

        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.

        If the template contains a named placeholder, then the output will contain a populated attribute map for all placeholders including those without names. The output will include a 'templateLocation' attribute identifying the location of the named placeholders.

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

        public static String recreateTemplate​(String message,
                                              String templateLocation)
        Recreates the template from the message and templateLocation code.

        This method returns the input message if the input is not valid.

        Parameters:
        message - the message, null tolerant
        templateLocation - the template location, null tolerant
        Returns:
        the message
      • mergeTemplateLocations

        public static String mergeTemplateLocations​(String location1,
                                                    String location2,
                                                    int message1Length)
        Merges two template locations.

        This takes two template locations and the length of the first message and returns the combined template.

        Parameters:
        location1 - the first location, null tolerant
        location2 - the second location, null tolerant
        message1Length - the length of the formatted message associated with location1
        Returns:
        the merged location, empty if unable to merge