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-2024 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      @Override
45      public EventListenerMap getEventListenerMap()
46      {
47          return this.eventListenerMap;
48      }
49  
50      /* **************************************************************************************************** */
51      /* *********************** (RE) IMPLEMENTATION OF METHODS WITHOUT REMOTEEXCEPTION ********************* */
52      /* **************************************************************************************************** */
53  
54      @Override
55      public boolean addListener(final EventListener listener, final EventType eventType, final int position,
56              final ReferenceType referenceType)
57      {
58          try
59          {
60              return EventProducer.super.addListener(listener, eventType, position, referenceType);
61          }
62          catch (RemoteException exception)
63          {
64              throw new RuntimeException(exception);
65          }
66      }
67  
68      @Override
69      public boolean addListener(final EventListener listener, final EventType eventType)
70      {
71          try
72          {
73              return EventProducer.super.addListener(listener, eventType);
74          }
75          catch (RemoteException exception)
76          {
77              throw new RuntimeException(exception);
78          }
79      }
80  
81      @Override
82      public boolean addListener(final EventListener listener, final EventType eventType, final ReferenceType referenceType)
83      {
84          try
85          {
86              return EventProducer.super.addListener(listener, eventType, referenceType);
87          }
88          catch (RemoteException exception)
89          {
90              throw new RuntimeException(exception);
91  
92          }
93      }
94  
95      @Override
96      public boolean addListener(final EventListener listener, final EventType eventType, final int position)
97      {
98          try
99          {
100             return EventProducer.super.addListener(listener, eventType, position);
101         }
102         catch (RemoteException exception)
103         {
104             throw new RuntimeException(exception);
105 
106         }
107     }
108 
109     @Override
110     public int removeAllListeners()
111     {
112         try
113         {
114             return EventProducer.super.removeAllListeners();
115         }
116         catch (RemoteException exception)
117         {
118             throw new RuntimeException(exception);
119 
120         }
121     }
122 
123     @Override
124     public int removeAllListeners(final Class<?> ofClass)
125     {
126         try
127         {
128             return EventProducer.super.removeAllListeners(ofClass);
129         }
130         catch (RemoteException exception)
131         {
132             throw new RuntimeException(exception);
133 
134         }
135     }
136 
137     @Override
138     public boolean removeListener(final EventListener listener, final EventType eventType)
139     {
140         try
141         {
142             return EventProducer.super.removeListener(listener, eventType);
143         }
144         catch (RemoteException exception)
145         {
146             throw new RuntimeException(exception);
147 
148         }
149     }
150 
151     @Override
152     public boolean hasListeners()
153     {
154         try
155         {
156             return EventProducer.super.hasListeners();
157         }
158         catch (RemoteException exception)
159         {
160             throw new RuntimeException(exception);
161 
162         }
163     }
164 
165     @Override
166     public int numberOfListeners(final EventType eventType)
167     {
168         try
169         {
170             return EventProducer.super.numberOfListeners(eventType);
171         }
172         catch (RemoteException exception)
173         {
174             throw new RuntimeException(exception);
175 
176         }
177     }
178 
179     @Override
180     public List<Reference<EventListener>> getListenerReferences(final EventType eventType)
181     {
182         try
183         {
184             return EventProducer.super.getListenerReferences(eventType);
185         }
186         catch (RemoteException exception)
187         {
188             throw new RuntimeException(exception);
189 
190         }
191     }
192 
193     @Override
194     public Set<EventType> getEventTypesWithListeners()
195     {
196         try
197         {
198             return EventProducer.super.getEventTypesWithListeners();
199         }
200         catch (RemoteException exception)
201         {
202             throw new RuntimeException(exception);
203 
204         }
205     }
206 
207     @Override
208     public void fireEvent(final Event event)
209     {
210         try
211         {
212             EventProducer.super.fireEvent(event);
213         }
214         catch (RemoteException exception)
215         {
216             throw new RuntimeException(exception);
217 
218         }
219     }
220 
221     @Override
222     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final TimedEvent<C> event)
223     {
224         try
225         {
226             EventProducer.super.fireTimedEvent(event);
227         }
228         catch (RemoteException exception)
229         {
230             throw new RuntimeException(exception);
231 
232         }
233     }
234 
235     @Override
236     public void fireEvent(final EventType eventType)
237     {
238         try
239         {
240             EventProducer.super.fireEvent(eventType);
241         }
242         catch (RemoteException exception)
243         {
244             throw new RuntimeException(exception);
245 
246         }
247     }
248 
249     @Override
250     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final EventType eventType, final C time)
251     {
252         try
253         {
254             EventProducer.super.fireTimedEvent(eventType, time);
255         }
256         catch (RemoteException exception)
257         {
258             throw new RuntimeException(exception);
259 
260         }
261     }
262 
263     @Override
264     public void fireEvent(final EventType eventType, final Serializable value)
265     {
266         try
267         {
268             EventProducer.super.fireEvent(eventType, value);
269         }
270         catch (RemoteException exception)
271         {
272             throw new RuntimeException(exception);
273 
274         }
275     }
276 
277     @Override
278     public <C extends Comparable<C> & Serializable> void fireTimedEvent(final EventType eventType, final Serializable value,
279             final C time)
280 
281     {
282         try
283         {
284             EventProducer.super.fireTimedEvent(eventType, value, time);
285         }
286         catch (RemoteException exception)
287         {
288             throw new RuntimeException(exception);
289 
290         }
291     }
292 
293     @Override
294     public void fireUnverifiedEvent(final EventType eventType)
295     {
296         try
297         {
298             EventProducer.super.fireUnverifiedEvent(eventType);
299         }
300         catch (RemoteException exception)
301         {
302             throw new RuntimeException(exception);
303 
304         }
305     }
306 
307     @Override
308     public <C extends Comparable<C> & Serializable> void fireUnverifiedTimedEvent(final EventType eventType, final C time)
309 
310     {
311         try
312         {
313             EventProducer.super.fireUnverifiedTimedEvent(eventType, time);
314         }
315         catch (RemoteException exception)
316         {
317             throw new RuntimeException(exception);
318 
319         }
320     }
321 
322     @Override
323     public void fireUnverifiedEvent(final EventType eventType, final Serializable value)
324     {
325         try
326         {
327             EventProducer.super.fireUnverifiedEvent(eventType, value);
328         }
329         catch (RemoteException exception)
330         {
331             throw new RuntimeException(exception);
332 
333         }
334     }
335 
336     @Override
337     public <C extends Comparable<C> & Serializable> void fireUnverifiedTimedEvent(final EventType eventType,
338             final Serializable value, final C time)
339     {
340         try
341         {
342             EventProducer.super.fireUnverifiedTimedEvent(eventType, value, time);
343         }
344         catch (RemoteException exception)
345         {
346             throw new RuntimeException(exception);
347 
348         }
349     }
350 
351 }