Package dk.cloudcreate.essentials.shared
Class MessageFormatter
java.lang.Object
dk.cloudcreate.essentials.shared.MessageFormatter
Slf4J compatible message formatter.
The
The
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 ClassesModifier and TypeClassDescriptionstatic classNamed argument binding instance -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringbind(String message, MessageFormatter.NamedArgumentBinding... bindings) Replaces placeholders of style"Hello {:firstName} {:lastName}."static Stringbind(String message, List<MessageFormatter.NamedArgumentBinding> bindings) Replaces placeholders of style"Hello {:firstName} {:lastName}."static StringReplaces placeholders of style"Hello {:firstName} {:lastName}."static StringFormat 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"
-
Constructor Details
-
MessageFormatter
public MessageFormatter()
-
-
Method Details
-
msg
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 inmessageAnchorPlaceHolderValuesmessageAnchorPlaceHolderValues- the positional placeholder values that will be inserted into themessageif it contains matching anchors- Returns:
- the message with any anchors replaced with
messageAnchorPlaceHolderValues
-
bind
Replaces placeholders of style"Hello {:firstName} {:lastName}."with bind offirstNameto "Peter" andlastNameto "Hansen" this will return the string"Hello Peter Hansen.":bind("Hello {:firstName} {:lastName}.", Map.of("firstName", "Peter", "lastName", "Hansen" ) )- Parameters:
message- the message stringbindings- the map where the key will become theMessageFormatter.NamedArgumentBinding.nameand the value will become theMessageFormatter.NamedArgumentBinding.value
Values are converted to String's using the toString()- Returns:
- the message merged with the bindings
-
bind
Replaces placeholders of style"Hello {:firstName} {:lastName}."with bind offirstNameto "Peter" andlastNameto "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 stringbindings- list ofMessageFormatter.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
-