com.alibaba.cola.event.EventHub Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cola-core Show documentation
Show all versions of cola-core Show documentation
Cola framework core components
package com.alibaba.cola.event;
import com.alibaba.cola.exception.framework.ColaException;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 事件控制中枢
* @author shawnzhan.zxy
* @date 2017/11/20
*/
@SuppressWarnings("rawtypes")
@Component
public class EventHub {
private HashMap> eventRepository = new HashMap<>();
private Map responseRepository = new HashMap<>();
public HashMap> getEventRepository() {
return eventRepository;
}
public void setEventRepository(HashMap> eventRepository) {
this.eventRepository = eventRepository;
}
public Map getResponseRepository() {
return responseRepository;
}
public List getEventHandler(Class eventClass) {
List eventHandlerIList = findHandler(eventClass);
if (eventHandlerIList == null || eventHandlerIList.size() == 0) {
throw new ColaException(eventClass + "is not registered in eventHub, please register first");
}
return eventHandlerIList;
}
/**
* 注册事件
* @param eventClz
* @param executor
*/
public void register(Class extends EventI> eventClz, EventHandlerI executor){
List eventHandlers = eventRepository.get(eventClz);
if(eventHandlers == null){
eventHandlers = new ArrayList<>();
eventRepository.put(eventClz, eventHandlers);
}
eventHandlers.add(executor);
}
private List findHandler(Class extends EventI> eventClass){
List eventHandlerIList = null;
Class cls = eventClass;
eventHandlerIList = eventRepository.get(cls);
return eventHandlerIList;
}
}