public class MessageService extends Object
final MessageService service = new MessageService();
final MessageArg<String> NAME = new MessageArg<>("NAME");
String expResult = "The ORM framework Ujorm.";
String expTemplate = "The ORM framework ${NAME}.";
String template = service.template("The ORM framework ", NAME, ".");
Map args = service.map(NAME, "Ujorm");
String result = service.format(template, args);
assertEquals(expTemplate, template);
assertEquals(expResult, result);
| Modifier and Type | Field and Description |
|---|---|
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() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
appendValue(String value,
StringBuffer result)
Append a value to the output buffer.
|
protected Object |
convertValue(Object value)
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.
|
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
|
public static final String PARAM_BEG
public static final char PARAM_END
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 argumentsFormatterpublic 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 argumentslocale - The target locale for an argument format, the null locale will be replaced by the ENGLISH.Formatter@Nullable protected Object convertValue(@Nullable Object value)
Key -> Key.getFullName() .protected void appendValue(@Nullable String value, @Nonnull StringBuffer result)
Copyright 2015, Pavel Ponec