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:07:23 UTC

svn commit: r1612681 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/EventType.java test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java

Author: oheger
Date: Tue Jul 22 20:07:22 2014
New Revision: 1612681

URL: http://svn.apache.org/r1612681
Log:
Added an isInstanceOf() method to EventType.

This method can be used to check whether there is an instanceof relation
between two given event types.

Modified:
    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/EventType.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/EventType.java?rev=1612681&r1=1612680&r2=1612681&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:07:22 2014
@@ -125,4 +125,30 @@ public class EventType<T extends Event>
         }
         return types;
     }
+
+    /**
+     * Checks whether an event type is derived from another type. This
+     * implementation tests whether {@code baseType} is a direct or indirect
+     * super type of {@code derivedType}. If one of the types is <b>null</b>,
+     * result is <b>false</b>.
+     *
+     * @param derivedType the derived event type
+     * @param baseType the base event type
+     * @return <b>true</b> if the derived type is an instance of the base type,
+     *         <b>false</b> otherwise
+     */
+    public static boolean isInstanceOf(EventType<?> derivedType,
+            EventType<?> baseType)
+    {
+        EventType<?> currentType = derivedType;
+        while (currentType != null)
+        {
+            if (currentType == baseType)
+            {
+                return true;
+            }
+            currentType = currentType.getSuperType();
+        }
+        return false;
+    }
 }

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=1612681&r1=1612680&r2=1612681&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:07:22 2014
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration.event;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
@@ -79,6 +80,52 @@ public class TestConfigurationEventTypes
     }
 
     /**
+     * Tests isInstanceOf() if the derived type is null.
+     */
+    @Test
+    public void testIsInstanceOfDerivedNull()
+    {
+        assertFalse("Wrong result", EventType.isInstanceOf(null, Event.ANY));
+    }
+
+    /**
+     * Tests isInstanceOf() if the base type is null.
+     */
+    @Test
+    public void testIsInstanceOfBaseNull()
+    {
+        assertFalse("Wrong result",
+                EventType.isInstanceOf(ConfigurationEvent.ANY, null));
+    }
+
+    /**
+     * Tests isInstanceOf() if there is no instanceof relationship.
+     */
+    @Test
+    public void testIsInstanceOfFalse()
+    {
+        assertFalse("Wrong result", EventType.isInstanceOf(
+                ConfigurationErrorEvent.READ, ConfigurationEvent.ANY));
+    }
+
+    /**
+     * Tests isInstanceOf() if the expected result is true.
+     */
+    @Test
+    public void testIsInstanceOfTrue()
+    {
+        assertTrue("Wrong result (1)", EventType.isInstanceOf(
+                ConfigurationEvent.ADD_NODES,
+                ConfigurationEvent.ANY_HIERARCHICAL));
+        assertTrue("Wrong result (2)", EventType.isInstanceOf(
+                ConfigurationEvent.ADD_NODES, ConfigurationEvent.ANY));
+        assertTrue("Wrong result (3)",
+                EventType.isInstanceOf(ConfigurationEvent.ADD_NODES, Event.ANY));
+        assertTrue("Wrong result (4)", EventType.isInstanceOf(
+                ConfigurationEvent.ADD_NODES, ConfigurationEvent.ADD_NODES));
+    }
+
+    /**
      * Tests the base event type for configuration events.
      */
     @Test