com.rop.event.AbstractRopEventMulticaster Maven / Gradle / Ivy
The newest version!
/**
* 版权声明:中图一购网络科技有限公司 版权所有 违者必究 2012
* 日 期:12-6-2
*/
package com.rop.event;
import java.util.*;
/**
*
* 功能说明:
*
*
* @author 陈雄华
* @version 1.0
*/
public abstract class AbstractRopEventMulticaster implements RopEventMulticaster {
private Set ropEventListeners = new HashSet();
private static final Map, ListenerRegistry> cachedRopEventListeners =
new HashMap, ListenerRegistry>();
@Override
public void removeAllRopListeners() {
ropEventListeners.clear();
}
@Override
public void addRopListener(RopEventListener listener) {
ropEventListeners.add(listener);
}
@Override
public void removeRopListener(RopEventListener listener) {
ropEventListeners.remove(listener);
}
protected List getRopEventListeners(RopEvent event) {
Class extends RopEvent> eventType = event.getClass();
if (!cachedRopEventListeners.containsKey(eventType)) {
LinkedList allListeners = new LinkedList();
if (ropEventListeners != null && ropEventListeners.size() > 0) {
for (RopEventListener ropEventListener : ropEventListeners) {
if (supportsEvent(ropEventListener, eventType)) {
allListeners.add(ropEventListener);
}
}
sortRopEventListener(allListeners);
}
ListenerRegistry listenerRegistry = new ListenerRegistry(allListeners);
cachedRopEventListeners.put(eventType, listenerRegistry);
}
return cachedRopEventListeners.get(eventType).getRopEventListeners();
}
protected boolean supportsEvent(
RopEventListener listener, Class extends RopEvent> eventType) {
SmartRopEventListener smartListener = (listener instanceof SmartRopEventListener ?
(SmartRopEventListener) listener : new GenericRopEventAdapter(listener));
return (smartListener.supportsEventType(eventType));
}
protected void sortRopEventListener(List ropEventListeners) {
Collections.sort(ropEventListeners, new Comparator() {
public int compare(RopEventListener o1, RopEventListener o2) {
if (o1.getOrder() > o2.getOrder()) {
return 1;
} else if (o1.getOrder() < o2.getOrder()) {
return -1;
} else {
return 0;
}
}
});
}
private class ListenerRegistry {
public List ropEventListeners;
private ListenerRegistry(List ropEventListeners) {
this.ropEventListeners = ropEventListeners;
}
public List getRopEventListeners() {
return ropEventListeners;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy