Class MessageTee<M extends Message>

java.lang.Object
eu.bandm.tools.message.MessageTee<M>
Type Parameters:
M - the type of the messages
All Implemented Interfaces:
MessageReceiver<M>

public class MessageTee<M extends Message> extends Object implements MessageReceiver<M>
Propagates all received messages sequentially to a multitude of current drains. For this, a list of receivers is maintained. The order of this list is respected when sending the message, but should better not be relevant. All methods which add receivers follow the discipline described for add(MessageReceiver).

The identical message object is sent to all receivers, so in most use cases it should be treated read-only by subsequent processing in the drain pipelines.

See Also:
  • Field Details

    • drains

      protected final List<MessageReceiver<? super M extends Message>> drains
      List of all receivers. The sequential order is respected when sending the message, but should better not be relevant.
  • Constructor Details

    • MessageTee

      public MessageTee()
      Simplest constructor. The receivers must be added subsequently.
    • MessageTee

      @SafeVarargs public MessageTee(MessageReceiver<? super M>... drains)
      Constructor with the given receivers.

      (Attention when method add is redefined by a subclass to possible this-escapes.)

      Parameters:
      drains - the list of receivers
      See Also:
    • MessageTee

      public MessageTee(MessageReceiver<? super M> drain1)
      Constructor with one given receiver. (Non-vararg specialization avoids unchecked warnings.)

      (Attention when method add is redefined by a subclass to possible this-escapes.)

      Parameters:
      drain1 - the only initial receiver (more can be added)
      See Also:
    • MessageTee

      public MessageTee(MessageReceiver<? super M> drain1, MessageReceiver<? super M> drain2)
      Constructor with two given receivers. (Non-vararg specialization avoids unchecked warnings.)

      (Attention when method add is redefined by a subclass to possible this-escapes.)

      Parameters:
      drain1 - the first initial receiver
      drain2 - the second initial receiver (more can be added)
      See Also:
    • MessageTee

      public MessageTee(MessageReceiver<? super M> drain1, MessageReceiver<? super M> drain2, MessageReceiver<? super M> drain3)
      Constructor with three given receivers. (Non-vararg specialization avoids unchecked warnings.)

      (Attention when method add is redefined by a subclass to possible this-escapes.)

      Parameters:
      drain1 - the first initial receiver
      drain2 - the second initial receiver
      drain3 - the second initial receiver (more can be added)
      See Also:
  • Method Details

    • getDrains

      public List<MessageReceiver<? super M>> getDrains()
      Return an unmodifiable list of all receivers.
      Returns:
      an unmodifiable list of all receivers.
    • add

      public void add(MessageReceiver<? super M> drain)
      Adds the message receiver at the end of the list of all current drains. If it is already contained at some position in the list, it is first removed and then re-inserted at the end of the list.
      Parameters:
      drain - the receiver to add or to re-add
    • add

      public void add(MessageReceiver<? super M> drain1, MessageReceiver<? super M> drain2)
      Adds the message receivers at the end of the list of all current drains. (Non-vararg specialization avoids unchecked warnings.)
      Parameters:
      drain1 - the first receiver to add or re-add
      drain2 - the second receiver to add or re-add
      See Also:
    • add

      @SafeVarargs public final void add(MessageReceiver<? super M>... newdrains)
      Adds the message receivers at the end of the list of all current drains.
      Parameters:
      newdrains - the receivers to add or re-add
      See Also:
    • addAll

      public void addAll(Iterable<MessageReceiver<? super M>> newdrains)
      Adds the message receivers at the end of the list of all current drains.
      Parameters:
      newdrains - the receivers to add or re-add
      See Also:
    • clear

      public void clear()
      Clear the list of all receivers.
    • receive

      public void receive(M message)
      Propagates the message received sequentially to a multitude of current drains.

      In case that while propagating the message to the drains, the very same message is thrown as an exception, then it will be caught and re-thrown after the propagation to all drains is completed. Other exceptions, esp. the throwing of different messages, are not intercepted.

      Specified by:
      receive in interface MessageReceiver<M extends Message>
      Parameters:
      message - to propagate