Class MessageLevelFilter<M extends Message>

Type Parameters:
M - the message subtype which can be received
All Implemented Interfaces:
MessageReceiver<M>

public class MessageLevelFilter<M extends Message> extends MessagePasser<M>
A filter which sends messages only to its drain if their level value is equal to or below a given threshold.

As with any MessagePasser, the receiver must be set to a non-null value before the first message is received. Otherwise the code may crash intentionally.

  • Field Details

    • level

      int level
      Level threshold. Only messages with a level equal or lower to this value are propagated.
    • unfiltered

      EnumSet<Message.Kind> unfiltered
      Set of Message.Kind which will not be suppressed by the threshold, i.e. which will always be forwarded. Initially, error and failure are contained here.
  • Constructor Details

    • MessageLevelFilter

      public MessageLevelFilter()
      Constructor with no initial drain. Before the filter can be used, SingleSender.setReceiver(MessageReceiver) must be called.
    • MessageLevelFilter

      public MessageLevelFilter(@Opt @Opt MessageReceiver<M> drain)
      Constructor with an initial drain.
      Parameters:
      drain - the receiver of all messages which pass this filter.
  • Method Details

    • setDrain

      public MessageLevelFilter<M> setDrain(@Opt @Opt MessageReceiver<? super M> r)
      Set the drain of the sent messages. As long as the value of that drain is null, invoking receive(M) may crash intentionally.
      Overrides:
      setDrain in class SingleSender<M extends Message>
      Parameters:
      r - the drain of the sent messages.
      Returns:
      this instance, for method chaining.
    • setLevel

      public MessageLevelFilter<M> setLevel(int level)
      Set the level threshold. Only messages with a level equal or lower to this value are propagated.
      Parameters:
      level - the threshold level.
      Returns:
      this for message chaining.
    • setUnfilteredKinds

      public MessageLevelFilter<M> setUnfilteredKinds(EnumSet<Message.Kind> unfiltered)
      Define the set of Message.Kind which will not be suppressed by the threshold, i.e. which will always be forwarded. The initial setting lets all error and failure messages pass, what is appropriate in most use cases.
      Parameters:
      unfiltered - the set of kinds which will not be suppressed by the level
      Returns:
      this for message chaining.
    • receive

      public void receive(M m)
      Receive a message and propagate it when it passes the threshold.
      Specified by:
      receive in interface MessageReceiver<M extends Message>
      Overrides:
      receive in class MessagePasser<M extends Message>
      Parameters:
      m - the message to receive.
      Throws:
      NullPointerException - if the drain has not been set, either by constructor or by invoking SingleSender.setReceiver(MessageReceiver).