Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 114   Methods: 4
NCLOC: 38   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EventManager.java 100% 100% 100% 100%
coverage
 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   
 14    /**
 15    * Manages event notification in the system. Observers that desire to be
 16    * notified of genetic events should subscribe to this class via the
 17    * addEventListener() method. To unsubscribe, use the removeEventListener()
 18    * method. To generate a genetic event, use the fireGeneticEvent() method,
 19    * which will take care of notifying the appropriate subscribers.
 20    *
 21    * @author Neil Rotstan
 22    * @author Klaus Meffert
 23    * @since 1.0
 24    */
 25    public class EventManager
 26    implements IEventManager {
 27    /** String containing the CVS revision. Read out via reflection!*/
 28    private final static String CVS_REVISION = "$Revision: 1.6 $";
 29   
 30    /**
 31    * References a Map of subscribed event listeners. Each key is an event
 32    * name, and each value is a List of listeners subscribed to that event.
 33    */
 34    private Map m_listeners = new HashMap();
 35   
 36    /**
 37    * Adds a new listener that will be notified when the event represented
 38    * by the given name is fired.
 39    *
 40    * @param a_eventName the name of the event to which the given listener
 41    * should be subscribed. Standard events are represented by constants in the
 42    * GeneticEvent class
 43    * @param a_eventListenerToAdd the genetic listener to subscribe to
 44    * notifications of the given event
 45    *
 46    * @author Neil Rotstan
 47    * @since 1.0
 48    */
 49  6 public synchronized void addEventListener(final String a_eventName,
 50    final GeneticEventListener a_eventListenerToAdd) {
 51  6 List eventListeners = (List) m_listeners.get(a_eventName);
 52  6 if (eventListeners == null) {
 53  5 eventListeners = new LinkedList();
 54  5 m_listeners.put(a_eventName, eventListeners);
 55    }
 56  6 eventListeners.add(a_eventListenerToAdd);
 57    }
 58   
 59    /**
 60    * Removes the given listener from subscription of the indicated event.
 61    * The listener will no longer be notified when the given event occurs.
 62    *
 63    * @param a_eventName the name of the event to which the given listener
 64    * should be removed. Standard events are represented by constants in the
 65    * GeneticEvent class
 66    * @param a_eventListenerToRemove the genetic listener to unsubscribe from
 67    * notifications of the given event
 68    *
 69    * @author Neil Rotstan
 70    * @since 1.0
 71    */
 72  3 public synchronized void removeEventListener(final String a_eventName,
 73    final GeneticEventListener a_eventListenerToRemove) {
 74  3 List eventListeners = (List) m_listeners.get(a_eventName);
 75  3 if (eventListeners != null) {
 76  2 eventListeners.remove(a_eventListenerToRemove);
 77    }
 78    }
 79   
 80    /**
 81    * Fires a genetic event. All subscribers of that particular event type
 82    * (as determined by the name of the event) will be notified of it
 83    * having been fired.
 84    *
 85    * @param a_eventToFire the representation of the GeneticEvent to fire
 86    *
 87    * @author Neil Rotstan
 88    * @since 1.0
 89    */
 90  32 public synchronized void fireGeneticEvent(final GeneticEvent a_eventToFire) {
 91  32 List eventListeners =
 92    (List) m_listeners.get(a_eventToFire.getEventName());
 93  32 if (eventListeners != null) {
 94    // Iterate over the listeners and notify each one of the event.
 95    // ------------------------------------------------------------
 96  1 Iterator listenerIterator = eventListeners.iterator();
 97  1 while (listenerIterator.hasNext()) {
 98  1 ( (GeneticEventListener) listenerIterator.next()).
 99    geneticEventFired(a_eventToFire);
 100    }
 101    }
 102    }
 103   
 104    /**
 105    * @return hashcode
 106    *
 107    * @author Klaus Meffert
 108    * @since 3.0
 109    */
 110  3622 public int hashCode() {
 111  3622 int result = m_listeners.hashCode();
 112  3622 return result;
 113    }
 114    }