All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.cola.event.EventHub Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
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 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 eventClass){
        List eventHandlerIList = null;
        Class cls = eventClass;
        eventHandlerIList = eventRepository.get(cls);
        return eventHandlerIList;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy