com.jamonapi.CompositeListener Maven / Gradle / Ivy
package com.jamonapi;
import com.jamonapi.utils.DetailData;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** A class that can contain other listeners that can listen to jamon events of interest.
* These classes will all implement the JAMonListener interface too. This is an example of the
* Gang of 4 Composite design pattern.
*
* @author steve souza
*
*/
public class CompositeListener implements JAMonListener, DetailData {
private static final long serialVersionUID = 278L;
// A variable that will hold the list of Listeners
private List listenerList=new ArrayList(4);
// The name of the composite listener
private String name;
/** Uses the CompositeListener name */
public CompositeListener() {
this("CompositeJAMonListener");
}
/** Pass in a Listener name that allows you to differentiate this listener from others */
public CompositeListener(String name) {
this.name=name;
}
/** Add a listener to the composite and return this object */
public CompositeListener addListener(JAMonListener listener) {
// added a check to not have the same listener name like FIFOBuffer. This will allow for one time initialization of
// a listener such as a FIFOBuffer on an exception monitor. Repeat adding of a listener with this name
// will have no effect. This is useful if different parts of the program initialize an exception listener for example.
if (listener instanceof CompositeListener || !hasListener(listener.getName())) {
listenerList.add(listener);
}
return this;
}
/** Return the listener associated with the passed in name */
public JAMonListener getListener(String listenerName) {
int rows=getNumListeners();
for (int i=0;i