Class MessageFormatter<D>

Type Parameters:
D - the type of the document id
All Implemented Interfaces:
MessageReceiver<SimpleMessage<D>>

public class MessageFormatter<D> extends MessagePasser<SimpleMessage<D>>
Inserts the arguments into the message text, similar to the Java standard string formatter. In principle, the insertion happens by Formatter.format(String, Object...). But before, all arguments of type PreMessage are replaced by the string result of formatting them, recursively. For each received message a new message is constructed with all fields the same, but with the formatted text, and sent to the receiver. The flag stripArgs decides whether to keep the top-level arguments of the message, even though their textual representation is now present in the generated text.
  • Field Details

    • stripArgs

      protected boolean stripArgs
      Whether the args are only entered to formatting, but not propagated. Initially true.
  • Constructor Details

    • MessageFormatter

      public MessageFormatter()
      Constructor with no receiver. The receiver must be set before sending messages to it.
    • MessageFormatter

      public MessageFormatter(MessageReceiver<? super SimpleMessage<D>> rec)
      Constructor with the given receiver.
      Parameters:
      rec - the receiver to which the formatted messages will be sent.
  • Method Details

    • setStripArgs

      public MessageFormatter<D> setStripArgs(boolean b)
      Set whether the arguments shall not be included in the formatted method. They are represented in the text of the formatted method anyhow.
      Parameters:
      b - whether to strip the arguments.
      Returns:
      this instance, for method chaining.
    • receive

      public void receive(SimpleMessage<D> m)
      Applies the message text as a format to the arguments and propagates the new message to the current drain.
      Specified by:
      receive in interface MessageReceiver<D>
      Overrides:
      receive in class MessagePasser<SimpleMessage<D>>
      Parameters:
      m - the message to receive.
      See Also:
    • render

      @Opt public static @Opt String render(SimpleMessage<?> msg)
      Service access point to apply the text as a format to the arguments.

      String.format(String,Object...) is called in any case, also with empty arg list, for format codes like "%n". Any PreMessage contained in the arguments is formatted recursively.

      Any formatting which fails returns for diagnosis the concatenation of the text, an error text and the Object.toString() results of the arguments.

      Parameters:
      msg - the message to be rendered
      Returns:
      a string representation of the message text and the rendered arguments
    • process

      @Opt public static @Opt String process(PreMessage embed)
      Service access point to apply the text as a format to the arguments.
      Parameters:
      embed - text and arguments to be rendered
      Returns:
      a string representation of the message text and the rendered arguments
      See Also:
    • process

      @Opt public static @Opt String process(@Opt @Opt String text, Object[] args)
      Service access point to apply the text as a format to the arguments.

      String.format(String,Object...) is called in any case, also with empty arg list, for format codes like "%n". Any PreMessage contained in the arguments is formatted recursively.

      Any formatting which fails returns for diagnosis the concatenation of the text, an error text and the Object.toString() results of the arguments.

      Parameters:
      text - the format text to be rendered
      args - the arguments to be rendered
      Returns:
      a string representation of the message text and the rendered arguments, or null iff the text input is null.