Package dk.cloudcreate.essentials.shared
Class MessageFormatter
- java.lang.Object
-
- dk.cloudcreate.essentials.shared.MessageFormatter
-
public final class MessageFormatter extends Object
Slf4J compatible message formatter.
TheMessageFormattersupports formatting a message using Slf4J style message anchors:
msg("This {} a {} message {}", "is", "simple", "example");will return"This is a simple message example"
TheMessageFormatteralso 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 classMessageFormatter.NamedArgumentBindingNamed argument binding instance
-
Constructor Summary
Constructors Constructor Description MessageFormatter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringbind(String message, MessageFormatter.NamedArgumentBinding... bindings)Replaces placeholders of style"Hello {:firstName} {:lastName}."with bind offirstNameto "Peter" andlastNameto "Hansen" this will return the string"Hello Peter Hansen.":static Stringbind(String message, List<MessageFormatter.NamedArgumentBinding> bindings)Replaces placeholders of style"Hello {:firstName} {:lastName}."with bind offirstNameto "Peter" andlastNameto "Hansen" this will return the string "Hello Peter Hansen.":static Stringbind(String message, Map<String,Object> bindings)Replaces placeholders of style"Hello {:firstName} {:lastName}."with bind offirstNameto "Peter" andlastNameto "Hansen" this will return the string"Hello Peter Hansen.":static Stringmsg(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"
-
-
-
Method Detail
-
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 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
public static String bind(String message, Map<String,Object> bindings)
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
public static String bind(String message, MessageFormatter.NamedArgumentBinding... bindings)
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}.", arg("firstName", "Peter"), arg("lastName", "Hansen"))- Parameters:
message- the message stringbindings- the vararg list ofMessageFormatter.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 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
-
-