You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/07/13 22:01:48 UTC
svn commit: r1610289 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/event/EventListenerList.java
test/java/org/apache/commons/configuration/event/TestEventListenerList.java
Author: oheger
Date: Sun Jul 13 20:01:47 2014
New Revision: 1610289
URL: http://svn.apache.org/r1610289
Log:
EventListenerList now provides a method for querying registrations of a super type.
With this method it is possible to find all event listeners derived from a
given type.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventListenerList.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java?rev=1610289&r1=1610288&r2=1610289&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java Sun Jul 13 20:01:47 2014
@@ -17,9 +17,12 @@
package org.apache.commons.configuration.event;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -206,6 +209,48 @@ public class EventListenerList
}
/**
+ * Returns a list with {@code EventListenerRegistrationData} objects for all
+ * event listener registrations of the specified event type or an event type
+ * having this type as super type (directly or indirectly). Note that this
+ * is the opposite direction than querying event types for firing events: in
+ * this case event listener registrations are searched which are super event
+ * types from a given type. This method in contrast returns event listener
+ * registrations for listeners that extend a given super type.
+ *
+ * @param eventType the event type object
+ * @param <T> the event type
+ * @return a list with the matching event listener registration objects
+ */
+ public <T extends Event> List<EventListenerRegistrationData<? extends T>> getRegistrationsForSuperType(
+ EventType<T> eventType)
+ {
+ Map<EventType<?>, Set<EventType<?>>> superTypes =
+ new HashMap<EventType<?>, Set<EventType<?>>>();
+ List<EventListenerRegistrationData<? extends T>> results =
+ new LinkedList<EventListenerRegistrationData<? extends T>>();
+
+ for (EventListenerRegistrationData<?> reg : listeners)
+ {
+ Set<EventType<?>> base = superTypes.get(reg.getEventType());
+ if (base == null)
+ {
+ base = fetchSuperEventTypes(reg.getEventType());
+ superTypes.put(reg.getEventType(), base);
+ }
+ if (base.contains(eventType))
+ {
+ @SuppressWarnings("unchecked")
+ // This is safe because we just did a check
+ EventListenerRegistrationData<? extends T> result =
+ (EventListenerRegistrationData<? extends T>) reg;
+ results.add(result);
+ }
+ }
+
+ return results;
+ }
+
+ /**
* Removes all event listeners registered at this object.
*/
public void clear()
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventListenerList.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventListenerList.java?rev=1610289&r1=1610288&r2=1610289&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventListenerList.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventListenerList.java Sun Jul 13 20:01:47 2014
@@ -28,6 +28,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
+import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -509,6 +510,30 @@ public class TestEventListenerList
}
/**
+ * Tests whether event listener registrations derived from a super type can
+ * be queried.
+ */
+ @Test
+ public void testGetEventListenerRegistrationsForSuperType()
+ {
+ ListenerTestImpl l1 = new ListenerTestImpl();
+ ListenerTestImpl l2 = new ListenerTestImpl();
+ @SuppressWarnings("unchecked")
+ EventListener<Event> l3 = EasyMock.createMock(EventListener.class);
+ list.addEventListener(typeSub1, l1);
+ list.addEventListener(Event.ANY, l3);
+ list.addEventListener(typeBase, l2);
+
+ List<EventListenerRegistrationData<? extends EventBase>> regs =
+ list.getRegistrationsForSuperType(typeBase);
+ Iterator<EventListenerRegistrationData<? extends EventBase>> iterator =
+ regs.iterator();
+ assertEquals("Wrong listener 1", l1, iterator.next().getListener());
+ assertEquals("Wrong listener 2", l2, iterator.next().getListener());
+ assertFalse("Too many elements", iterator.hasNext());
+ }
+
+ /**
* Test event class. For testing purposes, a small hierarchy of test event
* class is created. This way it can be checked whether event types are
* correctly evaluated and take the event hierarchy into account.