@Immutable public class MessageService extends Object
final MessageService service = new MessageService(); final MessageArg<String> NAME = new MessageArg<>("NAME"); final MessageArg<String> TYPE = new MessageArg<>("TYPE"); String expResult = "The ORM framework Ujorm."; String expTemplate = "The ${TYPE} framework ${NAME}."; String template = service.template("The ", TYPE, " framework ", NAME, "."); Mapargs = service.map ( TYPE, "ORM" , NAME, "Ujorm"); String result = service.format(template, args); assertEquals(expTemplate, template); assertEquals(expResult, result);
Modifier and Type | Field and Description |
---|---|
protected Locale |
defaultLocale
Default locale
|
static String |
PARAM_BEG
Two-character mark ("${") to introducing a template argument.
|
static char |
PARAM_END
The mark ("}") to finishing a template argument.
|
Constructor and Description |
---|
MessageService()
Create new instance with the
Locale.ENGLISH |
MessageService(Locale defaultLocale) |
Modifier and Type | Method and Description |
---|---|
protected String |
convertKey(Object key)
Convert value.
|
String |
format(String msg,
Map<String,Object> args)
Format a template message using named variables.
|
String |
format(String msg,
Map<String,Object> args,
Locale locale)
Format a template message using named variables.
|
static String |
formatMsg(String template,
Map<String,Object> args)
Format a target message by a template with arguments
|
Map<String,Object> |
map(Object... args)
Create a map from man pairs key-value
|
String |
template(Object... args)
Create a message template from argument pairs key-value
|
protected void |
writeValue(Object value,
CharArrayWriter writer,
Locale locale)
Write a value to the output buffer.
|
public static final String PARAM_BEG
public static final char PARAM_END
public MessageService()
Locale.ENGLISH
public Map<String,Object> map(@Nonnull Object... args)
args
- Key-value pairspublic String template(@Nonnull Object... args)
args
- Sequence of the Objects and Argumentspublic final String format(@Nullable String msg, @Nullable Map<String,Object> args)
msg
- Template message, see the simple example:
"The input date ${KEY,%s} must be less than: ${DATE,%F}"
or
"The input date ${KEY,%s} must be less than: ${DATE,%tY-%tm-%td %tH:%tM:%tS}"
The format expression is separated by the character (,) a and it is not mandatory.args
- Key-value map argumentsFormatter
public final String format(@Nullable String msg, @Nullable Map<String,Object> args, @Nullable Locale locale)
msg
- Template message, see the simple example:
"The input date ${KEY,%s} must be less than: ${DATE,%F}"
or
"The input date ${KEY,%s} must be less than: ${DATE,%tY-%tm-%td %tH:%tM:%tS}"
The format expression is separated by the character (,) a and it is not mandatory.args
- Key-value map arguments where arguments type of Supplier
ares supported.locale
- The target locale for an argument format, the null
locale will be replaced by the defaultLocale
.Formatter
@Nullable protected String convertKey(@Nonnull Object key)
Key -> Key.getFullName()
.protected void writeValue(@Nonnull Object value, @Nonnull CharArrayWriter writer, @Nullable Locale locale)
Copyright 2019-2022, Pavel Ponec