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/22 22:06:45 UTC

svn commit: r1612680 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/ test/java/org/apache/commons/configuration/event/

Author: oheger
Date: Tue Jul 22 20:06:45 2014
New Revision: 1612680

URL: http://svn.apache.org/r1612680
Log:
Moved fetchSuperEventTypes() method to EventType.

It makes sense to have this useful utility method at a central place. The new
version has the advantage that it is null-safe.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventListenerList.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.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=1612680&r1=1612679&r2=1612680&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 Tue Jul 22 20:06:45 2014
@@ -18,7 +18,6 @@ package org.apache.commons.configuration
 
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -234,7 +233,7 @@ public class EventListenerList
             Set<EventType<?>> base = superTypes.get(reg.getEventType());
             if (base == null)
             {
-                base = fetchSuperEventTypes(reg.getEventType());
+                base = EventType.fetchSuperEventTypes(reg.getEventType());
                 superTypes.put(reg.getEventType(), base);
             }
             if (base.contains(eventType))
@@ -297,26 +296,6 @@ public class EventListenerList
     }
 
     /**
-     * Obtains a set of all super event types for the specified type (including
-     * the type itself). If an event listener was registered for one of these
-     * types, it can handle an event of the specified type.
-     *
-     * @param eventType the event type in question
-     * @return the set with all super event types
-     */
-    private static Set<EventType<?>> fetchSuperEventTypes(EventType<?> eventType)
-    {
-        Set<EventType<?>> types = new HashSet<EventType<?>>();
-        EventType<?> currentType = eventType;
-        while (currentType != null)
-        {
-            types.add(currentType);
-            currentType = currentType.getSuperType();
-        }
-        return types;
-    }
-
-    /**
      * A special {@code Iterator} implementation used by the
      * {@code getEventListenerIterator()} method. This iterator returns only
      * listeners compatible with a specified event type. It has a convenience
@@ -344,7 +323,7 @@ public class EventListenerList
         {
             underlyingIterator = it;
             baseEventType = base;
-            acceptedTypes = fetchSuperEventTypes(base);
+            acceptedTypes = EventType.fetchSuperEventTypes(base);
             initNextElement();
         }
 
@@ -419,8 +398,8 @@ public class EventListenerList
         private void validateEvent(Event event)
         {
             if (event == null
-                    || !fetchSuperEventTypes(event.getEventType()).contains(
-                            baseEventType))
+                    || !EventType.fetchSuperEventTypes(event.getEventType()).contains(
+                    baseEventType))
             {
                 throw new IllegalArgumentException(
                         "Event incompatible with listener iteration: " + event);

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java?rev=1612680&r1=1612679&r2=1612680&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java Tue Jul 22 20:06:45 2014
@@ -16,6 +16,9 @@
  */
 package org.apache.commons.configuration.event;
 
+import java.util.HashSet;
+import java.util.Set;
+
 /**
  * <p>
  * A class representing an event type.
@@ -101,4 +104,25 @@ public class EventType<T extends Event>
         return String.format(FMT_TO_STRING, getClass().getSimpleName(),
                 getName());
     }
+
+    /**
+     * Returns a set with all event types that are super types of the specified
+     * type. This set contains the direct and indirect super types and also
+     * includes the given type itself. The passed in type may be <b>null</b>,
+     * then an empty set is returned.
+     *
+     * @param eventType the event type in question
+     * @return a set with all super event types
+     */
+    public static Set<EventType<?>> fetchSuperEventTypes(EventType<?> eventType)
+    {
+        Set<EventType<?>> types = new HashSet<EventType<?>>();
+        EventType<?> currentType = eventType;
+        while (currentType != null)
+        {
+            types.add(currentType);
+            currentType = currentType.getSuperType();
+        }
+        return types;
+    }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java?rev=1612680&r1=1612679&r2=1612680&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java Tue Jul 22 20:06:45 2014
@@ -18,6 +18,11 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
 
 import org.junit.Test;
 
@@ -30,6 +35,50 @@ import org.junit.Test;
 public class TestConfigurationEventTypes
 {
     /**
+     * Tests whether the set of super event types for null input can be
+     * obtained.
+     */
+    @Test
+    public void testFetchSuperEventTypesNull()
+    {
+        Set<EventType<?>> superTypes = EventType.fetchSuperEventTypes(null);
+        assertTrue("Got super types", superTypes.isEmpty());
+    }
+
+    /**
+     * Tests whether the set of super event types for the base type can be
+     * obtained.
+     */
+    @Test
+    public void testFetchSuperEventTypesForBaseType()
+    {
+        Set<EventType<?>> superTypes =
+                EventType.fetchSuperEventTypes(Event.ANY);
+        assertEquals("Wrong number of super types", 1, superTypes.size());
+        assertTrue("Wrong super types", superTypes.contains(Event.ANY));
+    }
+
+    /**
+     * Tests whether the super event types of a specific type can be retrieved.
+     */
+    @Test
+    public void testFetchSuperEventTypesOfType()
+    {
+        Set<EventType<?>> superTypes =
+                EventType.fetchSuperEventTypes(ConfigurationEvent.ADD_NODES);
+        List<EventType<? extends Event>> expected =
+                new LinkedList<EventType<? extends Event>>();
+        expected.add(ConfigurationEvent.ADD_NODES);
+        expected.add(ConfigurationEvent.ANY_HIERARCHICAL);
+        expected.add(ConfigurationEvent.ANY);
+        expected.add(Event.ANY);
+        assertEquals("Wrong number of super types", expected.size(),
+                superTypes.size());
+        assertTrue("Wrong super types: " + superTypes,
+                superTypes.containsAll(expected));
+    }
+
+    /**
      * Tests the base event type for configuration events.
      */
     @Test