commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1609783 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/reloading/ReloadingController.java test/java/org/apache/commons/configuration/reloading/TestReloadingController.java
Date Fri, 11 Jul 2014 20:11:08 GMT
Author: oheger
Date: Fri Jul 11 20:11:07 2014
New Revision: 1609783

URL: http://svn.apache.org/r1609783
Log:
ReloadingController now supports the new event style.

The old methods for adding and removing event listeners have been deprecated.
They will be removed when all references have been replaced.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ReloadingController.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestReloadingController.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ReloadingController.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ReloadingController.java?rev=1609783&r1=1609782&r2=1609783&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ReloadingController.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ReloadingController.java
Fri Jul 11 20:11:07 2014
@@ -16,6 +16,9 @@
  */
 package org.apache.commons.configuration.reloading;
 
+import org.apache.commons.configuration.event.EventListener;
+import org.apache.commons.configuration.event.EventListenerList;
+import org.apache.commons.configuration.event.EventType;
 import org.apache.commons.lang3.event.EventListenerSupport;
 
 /**
@@ -67,7 +70,10 @@ public class ReloadingController
     private final ReloadingDetector detector;
 
     /** The helper object which manages the registered event listeners. */
-    private final EventListenerSupport<ReloadingListener> listeners;
+    private final EventListenerSupport<ReloadingListener> deprecatedListeners;
+
+    /** The helper object which manages the registered event listeners. */
+    private final EventListenerList listeners;
 
     /** A flag whether this controller is in reloading state. */
     private boolean reloadingState;
@@ -88,7 +94,8 @@ public class ReloadingController
         }
 
         detector = detect;
-        listeners = EventListenerSupport.create(ReloadingListener.class);
+        listeners = new EventListenerList();
+        deprecatedListeners = EventListenerSupport.create(ReloadingListener.class);
     }
 
     /**
@@ -106,20 +113,49 @@ public class ReloadingController
      * receive notifications whenever a reload operation is necessary.
      *
      * @param l the listener to be added (must not be <b>null</b>)
+     *          @deprecated Use {@code addEventListener()}
      */
     public void addReloadingListener(ReloadingListener l)
     {
-        listeners.addListener(l);
+        deprecatedListeners.addListener(l);
+    }
+
+    /**
+     * Adds an event listener of the specified type to this controller. It will
+     * receive notifications whenever a reload operation is necessary.
+     *
+     * @param eventType the event type (must not be <b>null</b>)
+     * @param listener the event listener (must not be <b>null</b>)
+     * @param <T> the event type
+     */
+    public <T extends ReloadingEvent> void addEventListener(
+            EventType<T> eventType, EventListener<? super T> listener)
+    {
+        listeners.addEventListener(eventType, listener);
     }
 
     /**
      * Removes the specified {@code ReloadingListener} from this controller.
      *
      * @param l the listener to be removed
+     *          @deprecated use {@code removeEventListener()}
      */
     public void removeReloadingListener(ReloadingListener l)
     {
-        listeners.removeListener(l);
+        deprecatedListeners.removeListener(l);
+    }
+
+    /**
+     * Removes the specified event listener from this controller.
+     *
+     * @param eventType the event type
+     * @param listener the listener to be removed
+     * @param <T> the event type
+     */
+    public <T extends ReloadingEvent> void removeEventListener(
+            EventType<T> eventType, EventListener<? super T> listener)
+    {
+        listeners.removeEventListener(eventType, listener);
     }
 
     /**
@@ -170,7 +206,8 @@ public class ReloadingController
 
         if (sendEvent)
         {
-            listeners.fire().reloadingRequired(new ReloadingEvent(this, data));
+            deprecatedListeners.fire().reloadingRequired(new ReloadingEvent(this, data));
+            listeners.fire(new ReloadingEvent(this, data));
             return true;
         }
         return false;

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestReloadingController.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestReloadingController.java?rev=1609783&r1=1609782&r2=1609783&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestReloadingController.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestReloadingController.java
Fri Jul 11 20:11:07 2014
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertSam
 import static org.junit.Assert.assertTrue;
 
 import org.apache.commons.configuration.event.Event;
+import org.apache.commons.configuration.event.EventListener;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.easymock.EasyMock;
 import org.easymock.IAnswer;
@@ -74,16 +75,29 @@ public class TestReloadingController
     }
 
     /**
+     * Creates a mock event listener.
+     *
+     * @return the mock listener
+     */
+    private static EventListener<ReloadingEvent> createListenerMock()
+    {
+        @SuppressWarnings("unchecked")
+        EventListener<ReloadingEvent> listener =
+                EasyMock.createMock(EventListener.class);
+        return listener;
+    }
+
+    /**
      * Prepares the given event listener mock to expect an event notification.
      * The event received is stored in the given mutable object.
      *
      * @param l the listener mock
      * @param evRef the reference where to store the event
      */
-    private void expectEvent(ReloadingListener l,
+    private void expectEvent(EventListener<ReloadingEvent> l,
             final MutableObject<ReloadingEvent> evRef)
     {
-        l.reloadingRequired(EasyMock.anyObject(ReloadingEvent.class));
+        l.onEvent(EasyMock.anyObject(ReloadingEvent.class));
         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
         {
             @Override
@@ -101,18 +115,16 @@ public class TestReloadingController
     @Test
     public void testCheckForReloadingTrue()
     {
-        ReloadingListener l = EasyMock.createMock(ReloadingListener.class);
-        ReloadingListener lRemoved =
-                EasyMock.createMock(ReloadingListener.class);
-        MutableObject<ReloadingEvent> evRef =
-                new MutableObject<ReloadingEvent>();
+        EventListener<ReloadingEvent> l = createListenerMock();
+        EventListener<ReloadingEvent> lRemoved = createListenerMock();
+        MutableObject<ReloadingEvent> evRef = new MutableObject<ReloadingEvent>();
         expectEvent(l, evRef);
         EasyMock.expect(detector.isReloadingRequired()).andReturn(Boolean.TRUE);
         EasyMock.replay(detector, l, lRemoved);
         ReloadingController ctrl = createController();
-        ctrl.addReloadingListener(lRemoved);
-        ctrl.addReloadingListener(l);
-        ctrl.removeReloadingListener(lRemoved);
+        ctrl.addEventListener(ReloadingEvent.ANY, lRemoved);
+        ctrl.addEventListener(ReloadingEvent.ANY, l);
+        ctrl.removeEventListener(ReloadingEvent.ANY, lRemoved);
         Object testData = "Some test data";
         assertTrue("Wrong result", ctrl.checkForReloading(testData));
         assertTrue("Not in reloading state", ctrl.isInReloadingState());
@@ -128,12 +140,12 @@ public class TestReloadingController
     @Test
     public void testCheckForReloadingFalse()
     {
-        ReloadingListener l = EasyMock.createMock(ReloadingListener.class);
+        EventListener<ReloadingEvent> l = createListenerMock();
         EasyMock.expect(detector.isReloadingRequired())
                 .andReturn(Boolean.FALSE);
         EasyMock.replay(detector, l);
         ReloadingController ctrl = createController();
-        ctrl.addReloadingListener(l);
+        ctrl.addEventListener(ReloadingEvent.ANY, l);
         assertFalse("Wrong result", ctrl.checkForReloading(null));
         assertFalse("In reloading state", ctrl.isInReloadingState());
         EasyMock.verify(detector, l);
@@ -146,12 +158,12 @@ public class TestReloadingController
     @Test
     public void testCheckForReloadingInReloadingState()
     {
-        ReloadingListener l = EasyMock.createMock(ReloadingListener.class);
+        EventListener<ReloadingEvent> l = createListenerMock();
         EasyMock.expect(detector.isReloadingRequired()).andReturn(Boolean.TRUE);
         expectEvent(l, new MutableObject<ReloadingEvent>());
         EasyMock.replay(detector, l);
         ReloadingController ctrl = createController();
-        ctrl.addReloadingListener(l);
+        ctrl.addEventListener(ReloadingEvent.ANY, l);
         assertTrue("Wrong result (1)", ctrl.checkForReloading(1));
         assertTrue("Wrong result (2)", ctrl.checkForReloading(2));
         EasyMock.verify(detector, l);



Mime
View raw message