Package org.ujorm.tools.msg
Class MessageService
java.lang.Object
org.ujorm.tools.msg.MessageService
Message Service. See the next example:
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);
- Since:
- 1.53
- Author:
- Pavel Ponec
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate new instance with theLocale.ENGLISHMessageService(@NotNull String begTag, @org.jetbrains.annotations.NotNull char endTag, @NotNull Locale defaultLocale) -
Method Summary
Modifier and TypeMethodDescriptionprotected @Nullable StringconvertKey(@NotNull Object key) Convert value.final Stringformat(@Nullable Appendable writer, @Nullable String msg, @Nullable Map<String, Object> args, @Nullable Locale locale) Deprecated.final Stringformat(@Nullable String msg, @Nullable Locale locale, @NotNull CharSequence key, @Nullable Object value, @NotNull Object... keyValuePairs) Format a template message using named variables.final StringFormat a template message using named variables.final StringFormat a template message using named variables.final voidformat(@Nullable String msg, @Nullable Map<String, Object> args, @Nullable Locale locale, @NotNull Appendable writer) Format a template message using named variables.static final StringformatMsg(@Nullable String template, @NotNull CharSequence key, @Nullable Object value, @NotNull Object... keyValuePairs) Format a target message by a template with arguments type of Mapstatic final StringFormat a target message by a template with argumentsstatic final voidFormat a target message by a template with argumentsmap(@NotNull T... args) Create a map from man pairs key-value<T> Stringtemplate(@NotNull T... args) Create a message template from argument pairs key-valueprotected voidwriteValue(@NotNull Object value, @NotNull Appendable writer, @Nullable Locale locale) Write a value to the output buffer.
-
Field Details
-
begTag
Two-character mark ("${") to introducing a template argument. -
endTag
protected final char endTagThe mark ("}") to finishing a template argument. -
defaultLocale
Default locale
-
-
Constructor Details
-
MessageService
public MessageService()Create new instance with theLocale.ENGLISH -
MessageService
-
-
Method Details
-
map
Create a map from man pairs key-value- Parameters:
args- Key-value pairs
-
template
Create a message template from argument pairs key-value- Parameters:
args- Sequence of the Objects and Arguments
-
format
public final String format(@Nullable @Nullable String msg, @Nullable @Nullable Map<String, Object> args) Format a template message using named variables. Each variable must be surrounded by two marks "${" and "}". The first mark is forbidden in a common text and can be replaced by the variable #{MARK}.- Parameters:
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- Returns:
- Target result
- See Also:
-
format
public final String format(@Nullable @Nullable String msg, @Nullable @Nullable Locale locale, @NotNull @NotNull CharSequence key, @Nullable @Nullable Object value, @NotNull @NotNull Object... keyValuePairs) Format a template message using named variables. Each variable must be surrounded by two marks "${" and "}". The first mark is forbidden in a common text and can be replaced by the variable #{MARK}.- Parameters:
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, thenulllocale will be replaced by thedefaultLocale.key- The Key (see aMessageArg)value- The ValuekeyValuePairs- Key-value pairs- See Also:
-
format
public final String format(@Nullable @Nullable String msg, @Nullable @Nullable Map<String, Object> args, @Nullable @Nullable Locale locale) Format a template message using named variables. Each variable must be surrounded by two marks "${" and "}". The first mark is forbidden in a common text and can be replaced by the variable #{MARK}.- Parameters:
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 ofSupplierares supported.locale- The target locale for an argument format, thenulllocale will be replaced by thedefaultLocale.- Returns:
- The result message or an empty String if the writter is available.
- See Also:
-
format
@Deprecated public final String format(@Nullable @Nullable Appendable writer, @Nullable @Nullable String msg, @Nullable @Nullable Map<String, Object> args, @Nullable @Nullable Locale locale) throws IOExceptionDeprecated.Use theformatMsg(String, Map, Appendable)rather.Format a template message using named variables. Each variable must be surrounded by two marks "${" and "}". The first mark is forbidden in a common text and can be replaced by the variable #{MARK}.- Parameters:
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 ofSupplierares supported.locale- The target locale for an argument format, thenulllocale will be replaced by thedefaultLocale.- Returns:
- The result message of an empty string of writter is available.
- Throws:
IOException- See Also:
-
format
public final void format(@Nullable @Nullable String msg, @Nullable @Nullable Map<String, Object> args, @Nullable @Nullable Locale locale, @NotNull @NotNull Appendable writer) throws IOExceptionFormat a template message using named variables. Each variable must be surrounded by two marks "${" and "}". The first mark is forbidden in a common text and can be replaced by the variable #{MARK}.- Parameters:
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 ofSupplierares supported.locale- The target locale for an argument format, thenulllocale will be replaced by thedefaultLocale.writer- A required writer.- Throws:
IOException- See Also:
-
convertKey
Convert value. The method can be overwrited for special data types, for example:Key -> Key.getFullName(). -
writeValue
protected void writeValue(@NotNull @NotNull Object value, @NotNull @NotNull Appendable writer, @Nullable @Nullable Locale locale) throws IOException Write a value to the output buffer. The method can be overwrited to escaping values. The method can be overwrited for special data types.- Throws:
IOException
-
formatMsg
public static final String formatMsg(@Nullable @Nullable String template, @Nullable @Nullable Map<String, Object> args) Format a target message by a template with arguments -
formatMsg
public static final String formatMsg(@Nullable @Nullable String template, @NotNull @NotNull CharSequence key, @Nullable @Nullable Object value, @NotNull @NotNull Object... keyValuePairs) Format a target message by a template with arguments type of Map -
formatMsg
public static final void formatMsg(@Nullable @Nullable String template, @Nullable @Nullable Map<String, Object> args, @NotNull @NotNull Appendable writer) Format a target message by a template with arguments
-
formatMsg(String, Map, Appendable)rather.