KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > event > EventManager


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.event;
11
12 import java.util.*;
13 import org.jgap.util.*;
14
15 /**
16  * Manages event notification in the system. Observers that desire to be
17  * notified of genetic events should subscribe to this class via the
18  * addEventListener() method. To unsubscribe, use the removeEventListener()
19  * method. To generate a genetic event, use the fireGeneticEvent() method,
20  * which will take care of notifying the appropriate subscribers.
21  *
22  * @author Neil Rotstan
23  * @author Klaus Meffert
24  * @since 1.0
25  */

26 public class EventManager
27     implements IEventManager, ICloneable {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
30
31   /**
32    * References a Map of subscribed event listeners. Each key is an event
33    * name, and each value is a List of listeners subscribed to that event.
34    */

35   private HashMap m_listeners = new HashMap();
36
37   /**
38    * Adds a new listener that will be notified when the event represented
39    * by the given name is fired.
40    *
41    * @param a_eventName the name of the event to which the given listener
42    * should be subscribed. Standard events are represented by constants in the
43    * GeneticEvent class
44    * @param a_eventListenerToAdd the genetic listener to subscribe to
45    * notifications of the given event
46    *
47    * @author Neil Rotstan
48    * @since 1.0
49    */

50   public synchronized void addEventListener(final String JavaDoc a_eventName,
51       final GeneticEventListener a_eventListenerToAdd) {
52     List eventListeners = (List) m_listeners.get(a_eventName);
53     if (eventListeners == null) {
54       eventListeners = new LinkedList();
55       m_listeners.put(a_eventName, eventListeners);
56     }
57     eventListeners.add(a_eventListenerToAdd);
58   }
59
60   /**
61    * Removes the given listener from subscription of the indicated event.
62    * The listener will no longer be notified when the given event occurs.
63    *
64    * @param a_eventName the name of the event to which the given listener
65    * should be removed. Standard events are represented by constants in the
66    * GeneticEvent class
67    * @param a_eventListenerToRemove the genetic listener to unsubscribe from
68    * notifications of the given event
69    *
70    * @author Neil Rotstan
71    * @since 1.0
72    */

73   public synchronized void removeEventListener(final String JavaDoc a_eventName,
74       final GeneticEventListener a_eventListenerToRemove) {
75     List eventListeners = (List) m_listeners.get(a_eventName);
76     if (eventListeners != null) {
77       eventListeners.remove(a_eventListenerToRemove);
78     }
79   }
80
81   /**
82    * Fires a genetic event. All subscribers of that particular event type
83    * (as determined by the name of the event) will be notified of it
84    * having been fired.
85    *
86    * @param a_eventToFire the representation of the GeneticEvent to fire
87    *
88    * @author Neil Rotstan
89    * @since 1.0
90    */

91   public synchronized void fireGeneticEvent(final GeneticEvent a_eventToFire) {
92     List eventListeners =
93         (List) m_listeners.get(a_eventToFire.getEventName());
94     if (eventListeners != null) {
95       // Iterate over the listeners and notify each one of the event.
96
// ------------------------------------------------------------
97
Iterator listenerIterator = eventListeners.iterator();
98       while (listenerIterator.hasNext()) {
99         ( (GeneticEventListener) listenerIterator.next()).
100             geneticEventFired(a_eventToFire);
101       }
102     }
103   }
104
105   /**
106    * @return hashcode
107    *
108    * @author Klaus Meffert
109    * @since 3.0
110    */

111   public int hashCode() {
112     int result = m_listeners.hashCode();
113     return result;
114   }
115
116   /**
117    * @return cloned instance
118    *
119    * @author Klaus Meffert
120    * @since 3.2
121    */

122   public Object JavaDoc clone() {
123     EventManager result = new EventManager();
124     if (!m_listeners .isEmpty()) {
125       result.m_listeners = (HashMap)m_listeners.clone();
126     }
127     return result;
128   }
129 }
130
Popular Tags