Package tools.msg

Class MessageService

java.lang.Object
tools.msg.MessageService

@Unmodifiable public class MessageService extends Object
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 Details

    • begTag

      protected final String begTag
      Two-character mark ("${") to introducing a template argument.
    • endTag

      protected final char endTag
      The mark ("}") to finishing a template argument.
    • defaultLocale

      @NotNull protected final @NotNull Locale defaultLocale
      Default locale
  • Constructor Details

    • MessageService

      public MessageService()
      Create new instance with the Locale.ENGLISH
    • MessageService

      public MessageService(@NotNull @NotNull String begTag, @NotNull @org.jetbrains.annotations.NotNull char endTag, @NotNull @NotNull Locale defaultLocale)
  • Method Details

    • map

      public <T> Map<String,Object> map(@NotNull @NotNull T... args)
      Create a map from man pairs key-value
      Parameters:
      args - Key-value pairs
    • template

      public <T> String template(@NotNull @NotNull T... args)
      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, the null locale will be replaced by the defaultLocale.
      key - The Key (see a MessageArg)
      value - The Value
      keyValuePairs - 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 of Supplier ares supported.
      locale - The target locale for an argument format, the null locale will be replaced by the defaultLocale.
      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 IOException
      Deprecated.
      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 of Supplier ares supported.
      locale - The target locale for an argument format, the null locale will be replaced by the defaultLocale.
      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 IOException
      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 of Supplier ares supported.
      locale - The target locale for an argument format, the null locale will be replaced by the defaultLocale.
      writer - A required writer.
      Throws:
      IOException
      See Also:
    • convertKey

      @Nullable protected @Nullable String convertKey(@NotNull @NotNull Object key)
      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