Wolfizen

Custom MessageChannel Implementation [1/3]

Jan 11th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import javax.annotation.Nullable;
  2. import java.util.Objects;
  3. import java.util.Optional;
  4. import java.util.stream.Stream;
  5.  
  6. /**
  7.  * Custom implementation of {@link org.spongepowered.api.text.channel.MessageChannel MessageChannel}
  8.  *
  9.  * @param <R> The targeted type to receive messages
  10.  * @param <M> The type of message to be sent to recipients
  11.  */
  12. public interface MessageChannel<R,M> {
  13.  
  14.     /**
  15.      * Sends a message to all recipients.
  16.      *
  17.      * @param message The message to be sent
  18.      *
  19.      * @implSpec The default implementation is to chain {@code send(null, message)}
  20.      */
  21.     default void send(M message) {
  22.         send(null, message);
  23.     }
  24.  
  25.     /**
  26.      * Sends a message to all recipients.
  27.      *
  28.      * @param sender The object which is sending the message
  29.      * @param message The message to be sent
  30.      *
  31.      * @see MessageChannel#transformMessage(Object, R, M)
  32.      */
  33.     default void send(@Nullable Object sender, M message) {
  34.         Objects.requireNonNull(message, "message cannot be null");
  35.         getRecipients().forEach(recipient -> {
  36.             transformMessage(sender, recipient, message).ifPresent(transformedMessage -> sendTo(recipient, transformedMessage));
  37.         });
  38.     }
  39.  
  40.     /**
  41.      * Transforms a message with respect to a recipient.
  42.      * <p>
  43.      *     {@code Optional.empty()} will be returned if the transformation decides this recipient should not receive the message
  44.      * </p>
  45.      *
  46.      * @param sender The object which is sending the message
  47.      * @param recipient The individual recipient who will be receiving the message
  48.      * @param originalMessage The message to be transformed
  49.      *
  50.      * @return The transformed message
  51.      */
  52.     default Optional<M> transformMessage(@Nullable Object sender, R recipient, M originalMessage) {
  53.         return Optional.of(originalMessage);
  54.     }
  55.  
  56.     /**
  57.      * Obtains a {@code Stream} of the channel recipients.
  58.      * <p>
  59.      *     The recipients may change, so it is recommended to consume this Stream immediately.
  60.      * </p>
  61.      *
  62.      * @return The current list of recipients
  63.      */
  64.     Stream<R> getRecipients();
  65.  
  66.     /**
  67.      * Sends an arbitrary message to an arbitrary recipient.
  68.      * <p>
  69.      *     Mostly an implementation detail, as internally describes how to send a message to a recipient.
  70.      * </p>
  71.      *
  72.      * @param recipient The recipient to send the message to
  73.      * @param message The message to be sent
  74.      */
  75.     void sendTo(R recipient, M message);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment