View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * TheseFoolishThings: Miscellaneous utilities
5    * http://tidalwave.it/projects/thesefoolishthings
6    *
7    * Copyright (C) 2009 - 2026 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22   * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.messagebus;
27  
28  import jakarta.annotation.Nonnull;
29  
30  /***************************************************************************************************************************************************************
31   * A simple message bus for a local publish/subscribe facility. The topic of a message is a class: a {@link Listener} subscribed to a topic receives the
32   * messages of that class and of its subclasses (or implementations, if the topic is an interface).
33   * @author  Fabrizio Giudici
34   **************************************************************************************************************************************************************/
35  public interface MessageBus
36    {
37      /***********************************************************************************************************************************************************
38       * A listener to receive notifications from a {@link MessageBus}.
39       * @param  <T>  the static type of the topic
40       * @stereotype  Listener
41       **********************************************************************************************************************************************************/
42      public static interface Listener<T>
43        {
44          /*******************************************************************************************************************************************************
45           * Notifies the reception of the given message.
46           * @param  message  the event
47           ******************************************************************************************************************************************************/
48          public void notify (@Nonnull T message);
49        }
50  
51      /***********************************************************************************************************************************************************
52       * Publishes the given event. The topic is the class of the event.
53       * @param  <T>      the static type of the topic
54       * @param  message  the event
55       **********************************************************************************************************************************************************/
56      public <T> void publish (@Nonnull T message);
57  
58      /***********************************************************************************************************************************************************
59       * Publishes the given message and topic. Passing an explicit topic can be useful when dealing with a hierarchy of events (so, perhaps a subclass is passed
60       * but the topic is the root of the hierarchy).
61       * @param  <T>      the static type of the topic
62       * @param  topic    the topic
63       * @param  message  the message
64       **********************************************************************************************************************************************************/
65      public <T> void publish (@Nonnull Class<T> topic, @Nonnull T message);
66  
67      /***********************************************************************************************************************************************************
68       * Subscribes a {@link Listener} to a topic.
69       * @param  <T>       the static type of the topic
70       * @param  topic     the topic
71       * @param  listener  the listener
72       **********************************************************************************************************************************************************/
73      public <T> void subscribe (@Nonnull Class<T> topic, @Nonnull Listener<T> listener);
74  
75      /***********************************************************************************************************************************************************
76       * Unsubscribes a {@link Listener} from all the topics it was subscribed to.
77       * @param  listener  the listener
78       **********************************************************************************************************************************************************/
79      public void unsubscribe (@Nonnull Listener<?> listener);
80    }