Class MessagePrinter<M extends Message>

All Implemented Interfaces:
Direct Known Subclasses:

public class MessagePrinter<M extends Message> extends Object implements MessageReceiver<M>
Prints textual representations of all received messages to an output stream.
After each printed message this stream is flushed explicitly.
  • Field Details

    • out

      protected final PrintWriter out
      The drain of the print activities.
    • toStr

      protected Function<? super M extends Message,? extends String> toStr
      The function which converts the message to a printable string. Defaults to Object::toString.
  • Constructor Details

    • MessagePrinter

      public MessagePrinter()
      Prints to System.err, using default encoding.
    • MessagePrinter

      public MessagePrinter(OutputStream os)
      Creates and uses as its drain a fresh PrintWriter to the OutputStream argument.
      Therefore printing must use the default character encoding.
    • MessagePrinter

      public MessagePrinter(PrintWriter out)
      Prints to thre PrintWriter provided.

      So the character encoding can be set explicitly.
  • Method Details

    • setMessageToString

      public void setMessageToString(Function<? super M,? extends String> toStr)
      Sets the function which converts the message to a printable string.
    • receive

      public void receive(M message)
      Print the message to the output stream.

      Rendering is done by applying the function in toStr.
      After each printed message the output stream is flushed explicitly.
      Specified by:
      receive in interface MessageReceiver<M extends Message>