Class MessageFormatter

java.lang.Object
dk.cloudcreate.essentials.shared.MessageFormatter

public final class MessageFormatter extends Object
Slf4J compatible message formatter.

The MessageFormatter supports formatting a message using Slf4J style message anchors:
 msg("This {} a {} message {}", "is", "simple", "example");
 
will return "This is a simple message example"

The MessageFormatter also support named argument binding:
 bind("Hello {:firstName} {:lastName}.",
      arg("firstName", "Peter"),
      arg("lastName", "Hansen"))
 
will return the string "Hello Peter Hansen."
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Named argument binding instance
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
    static String
    Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
    static String
    bind(String message, Map<String,Object> bindings)
    Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
    static String
    msg(String message, Object... messageAnchorPlaceHolderValues)
    Format a message using Slf4J like message anchors.
    Example: msg("This {} a {} message {}", "is", "simple", "example"); will result in this String:
    "This is a simple message example"

    Methods inherited from class java.lang.Object

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

    • MessageFormatter

      public MessageFormatter()
  • Method Details

    • msg

      public static String msg(String message, Object... messageAnchorPlaceHolderValues)
      Format a message using Slf4J like message anchors.
      Example: msg("This {} a {} message {}", "is", "simple", "example"); will result in this String:
      "This is a simple message example"
      Parameters:
      message - The message that can contain zero or more message anchors {} that will be replaced positionally by matching values in messageAnchorPlaceHolderValues
      messageAnchorPlaceHolderValues - the positional placeholder values that will be inserted into the message if it contains matching anchors
      Returns:
      the message with any anchors replaced with messageAnchorPlaceHolderValues
    • bind

      public static String bind(String message, Map<String,Object> bindings)
      Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
           bind("Hello {:firstName} {:lastName}.",
                    Map.of("firstName", "Peter",
                           "lastName", "Hansen"
                    )
               )
       
      Parameters:
      message - the message string
      bindings - the map where the key will become the MessageFormatter.NamedArgumentBinding.name and the value will become the MessageFormatter.NamedArgumentBinding.value
      Values are converted to String's using the toString()
      Returns:
      the message merged with the bindings
    • bind

      public static String bind(String message, MessageFormatter.NamedArgumentBinding... bindings)
      Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
           bind("Hello {:firstName} {:lastName}.",
                arg("firstName", "Peter"),
                arg("lastName", "Hansen"))
       
      Parameters:
      message - the message string
      bindings - the vararg list of MessageFormatter.NamedArgumentBinding's (prefer using the static method @MessageFormatter.NamedArgumentBinding.arg(String, Object)
      Values are converted to String's using the toString()
      Returns:
      the message merged with the bindings
    • bind

      public static String bind(String message, List<MessageFormatter.NamedArgumentBinding> bindings)
      Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
           bind("Hello {:firstName} {:lastName}.",
                    List.of(
                        arg("firstName", "Peter"),
                        arg("lastName", "Hansen")
                    )
               )
       
      Parameters:
      message - the message string
      bindings - list of MessageFormatter.NamedArgumentBinding's (use the static method @MessageFormatter.NamedArgumentBinding.arg(String, Object)
      Values are converted to String's using the toString()
      Returns:
      the message merged with the bindings