Some of the modifications Stephen wants.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137122 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2002-11-12 22:36:59 +00:00
parent dfac194424
commit 4c236996fa
1 changed files with 26 additions and 18 deletions

View File

@ -54,8 +54,10 @@
package org.apache.commons.lang; package org.apache.commons.lang;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.EventObject; import java.util.EventObject;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -76,10 +78,11 @@ public class Notifier {
private String methodName; private String methodName;
private Class clss; private Class clss;
public Notifier() {
}
public Notifier(Class listener) { public Notifier(Class listener) {
if(listener == null) {
throw new IllegalArgumentException("Illegal to have a null listener Class. ");
}
this.clss = clss; this.clss = clss;
// now we check methods, if only one of them, then // now we check methods, if only one of them, then
// let's set it // let's set it
@ -90,18 +93,24 @@ public class Notifier {
} }
/** /**
* Set the name of the method to call upon the listeners. * Construct with the class and the name of the method to
* call upon the listeners.
*/ */
public void setListenerMethod(String name) { public Notifier(Class clss, String name) {
if(clss == null) {
throw new IllegalArgumentException("Illegal to have a null Listener Class. ");
}
if(name == null) {
throw new IllegalArgumentException("Illegal to have a null method name. ");
}
this.clss = clss;
this.methodName = name; this.methodName = name;
if(this.clss != null) { try {
try { // then we get the Method object
// then we get the Method object this.listenerMethod = this.clss.getDeclaredMethod(name, new Class[] { EventObject.class} );
this.listenerMethod = this.clss.getDeclaredMethod(name, new Class[] { EventObject.class} ); } catch(NoSuchMethodException nsme) {
} catch(NoSuchMethodException nsme) { // nsme.printStackTrace();
// nsme.printStackTrace(); throw new IllegalArgumentException("Method not on Class. ");
throw new IllegalArgumentException("Method not on Class. ");
}
} }
} }
@ -113,10 +122,9 @@ public class Notifier {
this.listeners.remove(not); this.listeners.remove(not);
} }
public ArrayList getListeners() { public List getListeners() {
ArrayList cloned = new ArrayList(); ArrayList cloned = new ArrayList(listeners);
cloned.addAll(listeners); return Collections.unmodifiableList(cloned);
return cloned;
} }
/** /**
@ -138,7 +146,7 @@ public class Notifier {
* This is usable when a Listener has more than one method and * This is usable when a Listener has more than one method and
* a single Notifier wants to be shared. * a single Notifier wants to be shared.
*/ */
public void notify(Method listenerMethod, EventObject event) throws NotifierException { private void notify(Method listenerMethod, EventObject event) throws NotifierException {
Iterator itr = getListeners().iterator(); Iterator itr = getListeners().iterator();
while(itr.hasNext()) { while(itr.hasNext()) {
try { try {