001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.event;
011:
012: import java.util.*;
013: import org.jgap.util.*;
014:
015: /**
016: * Manages event notification in the system. Observers that desire to be
017: * notified of genetic events should subscribe to this class via the
018: * addEventListener() method. To unsubscribe, use the removeEventListener()
019: * method. To generate a genetic event, use the fireGeneticEvent() method,
020: * which will take care of notifying the appropriate subscribers.
021: *
022: * @author Neil Rotstan
023: * @author Klaus Meffert
024: * @since 1.0
025: */
026: public class EventManager implements IEventManager, ICloneable {
027: /** String containing the CVS revision. Read out via reflection!*/
028: private final static String CVS_REVISION = "$Revision: 1.8 $";
029:
030: /**
031: * References a Map of subscribed event listeners. Each key is an event
032: * name, and each value is a List of listeners subscribed to that event.
033: */
034: private HashMap m_listeners = new HashMap();
035:
036: /**
037: * Adds a new listener that will be notified when the event represented
038: * by the given name is fired.
039: *
040: * @param a_eventName the name of the event to which the given listener
041: * should be subscribed. Standard events are represented by constants in the
042: * GeneticEvent class
043: * @param a_eventListenerToAdd the genetic listener to subscribe to
044: * notifications of the given event
045: *
046: * @author Neil Rotstan
047: * @since 1.0
048: */
049: public synchronized void addEventListener(final String a_eventName,
050: final GeneticEventListener a_eventListenerToAdd) {
051: List eventListeners = (List) m_listeners.get(a_eventName);
052: if (eventListeners == null) {
053: eventListeners = new LinkedList();
054: m_listeners.put(a_eventName, eventListeners);
055: }
056: eventListeners.add(a_eventListenerToAdd);
057: }
058:
059: /**
060: * Removes the given listener from subscription of the indicated event.
061: * The listener will no longer be notified when the given event occurs.
062: *
063: * @param a_eventName the name of the event to which the given listener
064: * should be removed. Standard events are represented by constants in the
065: * GeneticEvent class
066: * @param a_eventListenerToRemove the genetic listener to unsubscribe from
067: * notifications of the given event
068: *
069: * @author Neil Rotstan
070: * @since 1.0
071: */
072: public synchronized void removeEventListener(
073: final String a_eventName,
074: final GeneticEventListener a_eventListenerToRemove) {
075: List eventListeners = (List) m_listeners.get(a_eventName);
076: if (eventListeners != null) {
077: eventListeners.remove(a_eventListenerToRemove);
078: }
079: }
080:
081: /**
082: * Fires a genetic event. All subscribers of that particular event type
083: * (as determined by the name of the event) will be notified of it
084: * having been fired.
085: *
086: * @param a_eventToFire the representation of the GeneticEvent to fire
087: *
088: * @author Neil Rotstan
089: * @since 1.0
090: */
091: public synchronized void fireGeneticEvent(
092: final GeneticEvent a_eventToFire) {
093: List eventListeners = (List) m_listeners.get(a_eventToFire
094: .getEventName());
095: if (eventListeners != null) {
096: // Iterate over the listeners and notify each one of the event.
097: // ------------------------------------------------------------
098: Iterator listenerIterator = eventListeners.iterator();
099: while (listenerIterator.hasNext()) {
100: ((GeneticEventListener) listenerIterator.next())
101: .geneticEventFired(a_eventToFire);
102: }
103: }
104: }
105:
106: /**
107: * @return hashcode
108: *
109: * @author Klaus Meffert
110: * @since 3.0
111: */
112: public int hashCode() {
113: int result = m_listeners.hashCode();
114: return result;
115: }
116:
117: /**
118: * @return cloned instance
119: *
120: * @author Klaus Meffert
121: * @since 3.2
122: */
123: public Object clone() {
124: EventManager result = new EventManager();
125: if (!m_listeners.isEmpty()) {
126: result.m_listeners = (HashMap) m_listeners.clone();
127: }
128: return result;
129: }
130: }
|