Package eu.bandm.tools.message
Class MessageMapper<M extends Message,N extends Message>
java.lang.Object
eu.bandm.tools.message.SingleSender<N>
eu.bandm.tools.message.MessageMapper<M,N>
- Type Parameters:
M
- the type of the ingoing messagesN
- the type of the outgoing messages
- All Implemented Interfaces:
MessageReceiver<M>
public class MessageMapper<M extends Message,N extends Message>
extends SingleSender<N>
implements MessageReceiver<M>
A message receiver which receives a message of one type,
and propagates the result of applying a function to this message, which
may be of different type.
If receiver is set to null and a message is received, the code
intentionally crashes.
(Since the types of in and out messages can be different, this
general class cannot be a suclass of MessagePasser
.)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
Whether a null value coming from the mapping function means not to send anything.The function to apply to all incoming messages.Fields inherited from class eu.bandm.tools.message.SingleSender
drain
-
Constructor Summary
ConstructorsConstructorDescriptionMessageMapper
(Function<? super M, ? extends N> fun) Will apply the specified total function to each received message and propagate it to a receiver.MessageMapper
(Function<? super M, ? extends N> fun, boolean p) Will apply the specified function to each received message and propagate it to a receiver.MessageMapper
(Function<? super M, ? extends N> fun, boolean p, @Opt MessageReceiver<N> drain) Will apply the specified function to each received message and propagate it to a receiver. -
Method Summary
Modifier and TypeMethodDescriptionstatic <M extends Message,
N extends Message>
Function<MessageReceiver<N>, MessageReceiver<M>> Delivers a function which applied to a message receiver for type "N" will deliver a message receiver for type "M".void
Apply the function and send its result to the current drain.protected MessageMapper
<M, N> setAllowPartialFunctions
(boolean b) Change whether the function may be partial and null results are ignored and not treated as error.protected MessageMapper
<M, N> setFunction
(Function<? super M, ? extends N> fun) Change the function to apply to the incoming messages.Methods inherited from class eu.bandm.tools.message.SingleSender
getDrain, getReceiver, send, setDrain, setReceiver
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface eu.bandm.tools.message.MessageReceiver
ifSubcriticalThen, ifSubcriticalThen, ifSubcriticalThenElse
-
Field Details
-
fun
The function to apply to all incoming messages. -
allowPartialFunctions
protected boolean allowPartialFunctionsWhether a null value coming from the mapping function means not to send anything. Otherwise, it is an error.
-
-
Constructor Details
-
MessageMapper
public MessageMapper(Function<? super M, ? extends N> fun, boolean p, @Opt @Opt MessageReceiver<N> drain) Will apply the specified function to each received message and propagate it to a receiver.- Parameters:
fun
- the function to apply to all received messages.p
- whether partial functions returning null are allowed.drain
- the receiver to which the function results are propagated.
-
MessageMapper
Will apply the specified function to each received message and propagate it to a receiver. The receiver must be set before the first message is received; otherwise the code will intentionally crash.- Parameters:
fun
- the function to apply to all received messages.p
- whether partial functions returning null are allowed.- See Also:
-
MessageMapper
Will apply the specified total function to each received message and propagate it to a receiver. Partial functions returning null are not allowed. The receiver must be set before the first message is received; otherwise the code will intentionally crash.- Parameters:
fun
- the function to apply to all received messages.
-
-
Method Details
-
setFunction
Change the function to apply to the incoming messages.- Parameters:
fun
- the new value of the Function.- Returns:
- this instance, for method chaining.
-
setAllowPartialFunctions
Change whether the function may be partial and null results are ignored and not treated as error.- Parameters:
b
- the new value of that flag.- Returns:
- this instance, for method chaining.
-
receive
Apply the function and send its result to the current drain. If the value ofSingleSender.drain
is null, then the code crashes intentionally.- Specified by:
receive
in interfaceMessageReceiver<M extends Message>
- Parameters:
message
- the message to process and propagate.
-
lift
public static <M extends Message,N extends Message> Function<MessageReceiver<N>,MessageReceiver<M>> lift(Function<M, ? extends N> f) Delivers a function which applied to a message receiver for type "N" will deliver a message receiver for type "M". A function must be given which maps the messages the other way round.- Type Parameters:
M
- the message type for the resulting receiverN
- the message type for the receiver as the function argument.- Parameters:
f
- function which maps messages compatible with the resulting receiver to those compatible with the argument receiver.- Returns:
- the function to produce modified message receivers.
-