diff --git a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java index ce5b2f359..69da3d6bc 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java +++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java @@ -17,56 +17,88 @@ package org.apache.commons.lang3.event; -import org.apache.commons.lang3.Validate; - import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.apache.commons.lang3.Validate; + /** - * An EventListenerSupport object can be used to manage a list of event listeners of a particular type. + * An EventListenerSupport object can be used to manage a list of event + * listeners of a particular type. The class provides + * {@link #addListener(Object)} and {@link #removeListener(Object)} methods + * for registering listeners, as well as a {@link #fire()} method for firing + * events to the listeners. + * *
* To use this class, suppose you want to support ActionEvents. You would do: - *
+ *
* public class MyActionEventSource
* {
- * private EventListenerSupport actionListeners = EventListenerSupport.create(ActionListener.class);
- *
+ * private EventListenerSupport actionListeners =
+ * EventListenerSupport.create(ActionListener.class);
+ *
* public void someMethodThatFiresAction()
* {
* ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "somethingCool");
* actionListeners.fire().actionPerformed(e);
* }
* }
- *
+ *
*
- * @param listenerInterface
is
+ * null
.
+ * @throws IllegalArgumentException if listenerInterface
is
+ * not an interface.
*/
- public static listenerInterface
is
+ * null
.
+ * @throws IllegalArgumentException if listenerInterface
is
+ * not an interface.
*/
public EventListenerSupport(ClasslistenerInterface
or
+ * classLoader
is null
.
+ * @throws IllegalArgumentException if listenerInterface
is
+ * not an interface.
*/
public EventListenerSupport(Classnull
).
+ *
+ * @throws NullPointerException if listener
is
+ * null
.
*/
public void addListener(L listener)
{
@@ -117,9 +164,9 @@ public void addListener(L listener)
/**
* Returns the number of registered listeners.
*
- * @return the number of registered listeners
+ * @return the number of registered listeners.
*/
- public int getListenerCount()
+ int getListenerCount()
{
return listeners.size();
}
@@ -127,7 +174,10 @@ public int getListenerCount()
/**
* Unregisters an event listener.
*
- * @param listener the event listener
+ * @param listener the event listener (may not be null
).
+ *
+ * @throws NullPointerException if listener
is
+ * null
.
*/
public void removeListener(L listener)
{
@@ -140,7 +190,18 @@ public void removeListener(L listener)
*/
private class ProxyInvocationHandler implements InvocationHandler
{
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ /**
+ * Propagates the method call to all registered listeners in place of
+ * the proxy listener object.
+ *
+ * @param proxy the proxy object representing a listener on which the
+ * invocation was called.
+ * @param method the listener method that will be called on all of the
+ * listeners.
+ * @param args event arguments to propogate to the listeners.
+ */
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable
{
for (L listener : listeners)
{