@Immutable public class MessageService extends Object
final MessageArg TYPE = MessageArg.of("TYPE"); final MessageArg NAME = MessageArg.of("NAME"); String expResult = "The ORM framework Ujorm."; String template = "The " + TYPE + " framework " + NAME + "."; String result = MessageService.formatMsg(template, TYPE, "ORM", NAME, "Ujorm"); assertEquals(expResult, result);or an similar usage:
final MessageArg NAME = MessageArg.of("NAME"); final MessageArg TYPE = MessageArg.of("TYPE"); String expResult = "The ORM framework Ujorm."; String expTemplate = "The ${TYPE} framework ${NAME}."; String template = service.template("The ", TYPE, " framework ", NAME, "."); Map<String, Object> args = new HashMap<>(); args.put(TYPE.name(), "ORM"); args.put(NAME.name(), "Ujorm"); String result = service.format(template, args); assertEquals(expTemplate, template); assertEquals(expResult, result);
MessageArg
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,
Locale locale,
CharSequence key,
Object value,
Object... keyValuePairs)
Format a template message using named variables.
|
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,
CharSequence key,
Object value,
Object... keyValuePairs)
Format a target message by a template with arguments type of Map
|
static String |
formatMsg(String template,
Map<String,Object> args)
Format a target message by a template with arguments
|
<T> Map<String,Object> |
map(T... args)
Create a map from man pairs key-value
|
<T> String |
template(T... args)
Create a message template from argument pairs key-value
|
protected void |
writeValue(Object value,
StringBuilder 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 <T> Map<String,Object> map(@Nonnull T... args)
args
- Key-value pairspublic <T> String template(@Nonnull T... 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 Locale locale, @Nonnull CharSequence key, @Nullable Object value, @Nonnull Object... keyValuePairs)
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.
locale
- The target locale for an argument format, the null
locale will be replaced by the defaultLocale
.key
- The Key (see a MessageArg
)value
- The ValuekeyValuePairs
- Key-value pairsFormatter
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 StringBuilder writer, @Nullable Locale locale)
public static final String formatMsg(@Nullable String template, @Nullable Map<String,Object> args)
Copyright 2019-2022, Pavel Ponec