/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* A support class for JSCData objects that manages adding,
* removing and notification of the different JSCDataListener.
*
* This class is not thread-save but clients must handle synchronization.
*
* @author Markus Kraetzig
*/
public final class JSCDataEventSupport {
private Map> typeMap = null;
/**
* JSCDataEventSupport constructor.
*/
public JSCDataEventSupport() {
super();
}
/**
* Adds evtListener to the list of listeners for events of
* type evtType.
*
* @param evtListener
* the JSCDataListener that gets notified whenever
* an event of evtType occurs
* @param evtType
* the event type
* @throws IllegalArgumentException
* if (evtListener == null || evtType == null)
*/
public void addListener(JSCDataListener evtListener,
JSCDataEventTypes evtType) {
if (evtListener == null || evtType == null)
throw new IllegalArgumentException("Argument was null.");
if (typeMap == null)
typeMap = new HashMap>();
List toPut = null;
if (!typeMap.containsKey(evtType)) { // First listener for that type.
toPut = new ArrayList();
typeMap.put(evtType, toPut);
} else
toPut = typeMap.get(evtType);
if (!toPut.contains(evtListener))
toPut.add(evtListener);
}
/**
* Dispatches evt by calling valueChanged of
* all registered listeners to events of type evt.getType.
*
* @param evt
* the event to be dispatched
* @throws IllegalArgumentException
* if (evt == null)
*/
public void dispatchEvent(JSCDataEvent evt) {
if (evt == null)
throw new IllegalArgumentException("Argument was null.");
// No listeners at all.
if (typeMap == null || typeMap.size() == 0)
return;
// No listeners for that type.
if (!typeMap.containsKey(evt.getType()))
return;
List currentList = (ArrayList) typeMap.get(evt.getType());
for (int i = 0; i < currentList.size(); i++)
((JSCDataListener) currentList.get(i)).valueChanged(evt);
}
/**
* Gets a list with all registered listeners for evtType.
*
* @param evtType
* the event type
* @return a copy of the respective list or an initialized list instance
* with size == 0 if no listeners are registered for
* evtType
*/
public List getListenersForType(JSCDataEventTypes evtType) {
if (!typeMap.containsKey(evtType))
return new ArrayList();
List list = typeMap.get(evtType);
return new ArrayList(list);
}
/**
* Removes evtListener from all listener lists. Nothing
* happens if evtListener is currently not a listener.
*
* @param evtListener
* the JSCDataListener to be removed
* @throws IllegalArgumentException
* if (evtListener == null)
*/
public void removeListener(JSCDataListener evtListener) {
if (evtListener == null)
throw new IllegalArgumentException("Argument was null.");
// No listeners at all.
if (typeMap == null || typeMap.size() == 0)
return;
Collection lists = typeMap.values();
for (Iterator it = lists.iterator(); it.hasNext();) {
List currentList = (ArrayList) it.next();
currentList.remove(evtListener);
}
}
/**
* Gets a string representation of this.
*
* @return informative string representation
*/
public String toString() {
StringBuffer str = new StringBuffer(super.toString() + "\n");
if (typeMap == null || typeMap.size() == 0)
str.append("no listeners registered\n");
else {
Collection lists = typeMap.values();
Collection keys = typeMap.keySet();
for (Iterator it = lists.iterator(), kit = keys.iterator(); it
.hasNext();)
str.append(kit.next() + " " + it.next() + "\n");
}
return str.toString();
}
}
Ads help maintain this website.