View Javadoc
1   package org.djutils.event;
2   
3   import java.io.Serializable;
4   import java.rmi.RemoteException;
5   import java.util.List;
6   import java.util.Set;
7   
8   import org.djutils.event.reference.Reference;
9   import org.djutils.event.reference.ReferenceType;
10  
11  /**
12   * The LocalEventProducer defines the registration and fireEvent operations of an event producer. This behavior includes adding
13   * and removing listeners for a specific event type, and firing events for all kinds of different payloads. The EventListener
14   * and EventProducer together form a combination of the Publish-Subscribe design pattern and the Observer design pattern using
15   * the notify(event) method. See <a href="https://en.wikipedia.org/wiki/Publish-subscribe_pattern" target=
16   * "_blank">https://en.wikipedia.org/wiki/Publish-subscribe_pattern</a>,
17   * <a href="https://en.wikipedia.org/wiki/Observer_pattern" target="_blank">https://en.wikipedia.org/wiki/Observer_pattern</a>,
18   * and <a href="https://howtodoinjava.com/design-patterns/behavioral/observer-design-pattern/" target=
19   * "_blank">https://howtodoinjava.com/design-patterns/behavioral/observer-design-pattern/</a>.
20   * <p>
21   * The EventProducer forms the reference implementation of the publish side of the pub/sub design pattern. The storage of the
22   * listeners is done in a Map with the EventType as the key, and a List of References (weak or strong) to the Listeners. Note
23   * that the term 'Local' used in the class name is opposed to remote event producers such as the RmiEventProducer.
24   * </p>
25   * <p>
26   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
27   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
28   * distributed under a three-clause BSD-style license, which can be found at
29   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
30   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
31   * https://simulation.tudelft.nl/dsol/manual</a>.
32   * </p>
33   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
34   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   */
36  public class LocalEventProducer implements EventProducer, Serializable
37  {
38      /** The default serial version UID for serializable classes. */
39      private static final long serialVersionUID = 20200207;
40  
41      /** The collection of interested listeners. */
42      private EventListenerMap eventListenerMap = new EventListenerMap();
43  
44      /** {@inheritDoc} */
45      @Override
46      public EventListenerMap getEventListenerMap()
47      {
48          return this.eventListenerMap;
49      }
50  
51      /* **************************************************************************************************** */
52      /* *********************** (RE) IMPLEMENTATION OF METHODS WITHOUT REMOTEEXCEPTION ********************* */
53      /* **************************************************************************************************** */
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean addListener(final EventListener listener, final EventType eventType, final int position,
58              final ReferenceType referenceType)
59      {
60          try
61          {
62              return EventProducer.super.addListener(listener, eventType, position, referenceType);
63          }
64          catch (RemoteException exception)
65          {
66              throw new RuntimeException(exception);
67          }
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public boolean addListener(final EventListener listener, final EventType eventType)
73      {
74          try
75          {
76              return EventProducer.super.addListener(listener, eventType);
77          }
78          catch (RemoteException exception)
79          {
80              throw new RuntimeException(exception);
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public boolean addListener(final EventListener listener, final EventType eventType, final ReferenceType referenceType)
87      {
88          try
89          {
90              return EventProducer.super.addListener(listener, eventType, referenceType);
91          }
92          catch (RemoteException exception)
93          {
94              throw new RuntimeException(exception);
95  
96          }
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public boolean addListener(final EventListener listener, final EventType eventType, final int position)
102     {
103         try
104         {
105             return EventProducer.super.addListener(listener, eventType, position);
106         }
107         catch (RemoteException exception)
108         {
109             throw new RuntimeException(exception);
110 
111         }
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public int removeAllListeners()
117     {
118         try
119         {
120             return EventProducer.super.removeAllListeners();
121         }
122         catch (RemoteException exception)
123         {
124             throw new RuntimeException(exception);
125 
126         }
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public int removeAllListeners(final Class<?> ofClass)
132     {
133         try
134         {
135             return EventProducer.super.removeAllListeners(ofClass);
136         }
137         catch (RemoteException exception)
138         {
139             throw new RuntimeException(exception);
140 
141         }
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public boolean removeListener(final EventListener listener, final EventType eventType)
147     {
148         try
149         {
150             return EventProducer.super.removeListener(listener, eventType);
151         }
152         catch (RemoteException exception)
153         {
154             throw new RuntimeException(exception);
155 
156         }
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public boolean hasListeners()
162     {
163         try
164         {
165             return EventProducer.super.hasListeners();
166         }
167         catch (RemoteException exception)
168         {
169             throw new RuntimeException(exception);
170 
171         }
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public int numberOfListeners(final EventType eventType)
177     {
178         try
179         {
180             return EventProducer.super.numberOfListeners(eventType);
181         }
182         catch (RemoteException exception)
183         {
184             throw new RuntimeException(exception);
185 
186         }
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public List<Reference<EventListener>> getListenerReferences(final EventType eventType)
192     {
193         try
194         {
195             return EventProducer.super.getListenerReferences(eventType);
196         }
197         catch (RemoteException exception)
198         {
199             throw new RuntimeException(exception);
200 
201         }
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     public Set<EventType> getEventTypesWithListeners()
207     {
208         try
209         {
210             return EventProducer.super.getEventTypesWithListeners();
211         }
212         catch (RemoteException exception)
213         {
214             throw new RuntimeException(exception);
215 
216         }
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public void fireEvent(final Event event)
222     {
223         try
224         {
225             EventProducer.super.fireEvent(event);
226         }
227         catch (RemoteException exception)
228         {
229             throw new RuntimeException(exception);
230 
231         }
232     }
233 
234     /** {@inheritDoc} */
235     @Override
236     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final TimedEvent<C> event)
237     {
238         try
239         {
240             EventProducer.super.fireTimedEvent(event);
241         }
242         catch (RemoteException exception)
243         {
244             throw new RuntimeException(exception);
245 
246         }
247     }
248 
249     /** {@inheritDoc} */
250     @Override
251     public void fireEvent(final EventType eventType)
252     {
253         try
254         {
255             EventProducer.super.fireEvent(eventType);
256         }
257         catch (RemoteException exception)
258         {
259             throw new RuntimeException(exception);
260 
261         }
262     }
263 
264     /** {@inheritDoc} */
265     @Override
266     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final EventType eventType, final C time)
267     {
268         try
269         {
270             EventProducer.super.fireTimedEvent(eventType, time);
271         }
272         catch (RemoteException exception)
273         {
274             throw new RuntimeException(exception);
275 
276         }
277     }
278 
279     /** {@inheritDoc} */
280     @Override
281     public void fireEvent(final EventType eventType, final Serializable value)
282     {
283         try
284         {
285             EventProducer.super.fireEvent(eventType, value);
286         }
287         catch (RemoteException exception)
288         {
289             throw new RuntimeException(exception);
290 
291         }
292     }
293 
294     /** {@inheritDoc} */
295     @Override
296     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final EventType eventType, final Serializable value,
297             final C time)
298 
299     {
300         try
301         {
302             EventProducer.super.fireTimedEvent(eventType, value, time);
303         }
304         catch (RemoteException exception)
305         {
306             throw new RuntimeException(exception);
307 
308         }
309     }
310 
311     /** {@inheritDoc} */
312     @Override
313     public void fireUnverifiedEvent(final EventType eventType)
314     {
315         try
316         {
317             EventProducer.super.fireUnverifiedEvent(eventType);
318         }
319         catch (RemoteException exception)
320         {
321             throw new RuntimeException(exception);
322 
323         }
324     }
325 
326     /** {@inheritDoc} */
327     @Override
328     public <C extends Comparable<C> & Serializable> void fireUnverifiedTimedEvent(final EventType eventType, final C time)
329 
330     {
331         try
332         {
333             EventProducer.super.fireUnverifiedTimedEvent(eventType, time);
334         }
335         catch (RemoteException exception)
336         {
337             throw new RuntimeException(exception);
338 
339         }
340     }
341 
342     /** {@inheritDoc} */
343     @Override
344     public void fireUnverifiedEvent(final EventType eventType, final Serializable value)
345     {
346         try
347         {
348             EventProducer.super.fireUnverifiedEvent(eventType, value);
349         }
350         catch (RemoteException exception)
351         {
352             throw new RuntimeException(exception);
353 
354         }
355     }
356 
357     /** {@inheritDoc} */
358     @Override
359     public <C extends Comparable<C> & Serializable> void fireUnverifiedTimedEvent(final EventType eventType,
360             final Serializable value, final C time)
361     {
362         try
363         {
364             EventProducer.super.fireUnverifiedTimedEvent(eventType, value, time);
365         }
366         catch (RemoteException exception)
367         {
368             throw new RuntimeException(exception);
369 
370         }
371     }
372 
373 }