@Unmodifiable 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 String |
begTag
Two-character mark ("${") to introducing a template argument.
|
protected @NotNull Locale |
defaultLocale
Default locale
|
protected char |
endTag
The mark ("}") to finishing a template argument.
|
| Constructor and Description |
|---|
MessageService()
Create new instance with the
Locale.ENGLISH |
MessageService(@NotNull String begTag,
@NotNull char endTag,
@NotNull Locale defaultLocale) |
| Modifier and Type | Method and Description |
|---|---|
protected @Nullable String |
convertKey(@NotNull Object key)
Convert value.
|
String |
format(@Nullable Appendable writer,
@Nullable String msg,
@Nullable Map<String,Object> args,
@Nullable Locale locale)
Format a template message using named variables.
|
String |
format(@Nullable String msg,
@Nullable Locale locale,
@NotNull CharSequence key,
@Nullable Object value,
Object... keyValuePairs)
Format a template message using named variables.
|
String |
format(@Nullable String msg,
@Nullable Map<String,Object> args)
Format a template message using named variables.
|
String |
format(@Nullable String msg,
@Nullable Map<String,Object> args,
@Nullable Locale locale)
Format a template message using named variables.
|
static String |
formatMsg(@Nullable String template,
@NotNull CharSequence key,
@Nullable Object value,
Object... keyValuePairs)
Format a target message by a template with arguments type of Map
|
static String |
formatMsg(@Nullable String template,
@Nullable 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(@NotNull Object value,
@NotNull Appendable writer,
@Nullable Locale locale)
Write a value to the output buffer.
|
protected final String begTag
protected final char endTag
@NotNull protected final @NotNull Locale defaultLocale
public MessageService()
Locale.ENGLISHpublic <T> Map<String,Object> map(@NotNull T... args)
args - Key-value pairspublic <T> String template(@NotNull T... args)
args - Sequence of the Objects and Argumentspublic final String format(@Nullable @Nullable String msg, @Nullable @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 @Nullable String msg, @Nullable @Nullable Locale locale, @NotNull @NotNull CharSequence key, @Nullable @Nullable Object value, @NotNull 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 pairsFormatterpublic final String format(@Nullable @Nullable String msg, @Nullable @Nullable Map<String,Object> args, @Nullable @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.Formatterpublic final String format(@Nullable @Nullable Appendable writer, @Nullable @Nullable String msg, @Nullable @Nullable Map<String,Object> args, @Nullable @Nullable Locale locale) throws IOException
writer - An optional writer.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.IOExceptionFormatter@Nullable protected @Nullable String convertKey(@NotNull @NotNull Object key)
Key -> Key.getFullName() .protected void writeValue(@NotNull
@NotNull Object value,
@NotNull
@NotNull Appendable writer,
@Nullable
@Nullable Locale locale)
throws IOException
IOExceptionpublic static final String formatMsg(@Nullable @Nullable String template, @Nullable @Nullable Map<String,Object> args)
public static final String formatMsg(@Nullable @Nullable String template, @NotNull @NotNull CharSequence key, @Nullable @Nullable Object value, @NotNull Object... keyValuePairs)
Copyright 2015, Pavel Ponec