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

top.jfunc.common.event.EventKit Maven / Gradle / Ivy

There is a newer version: 1.8.5
Show newest version
package top.jfunc.common.event;

import top.jfunc.common.event.core.ApplicationEvent;
import top.jfunc.common.event.core.EventType;
import top.jfunc.common.event.core.ListenerHelper;
import top.jfunc.common.utils.ArrayListMultimap;

import java.util.Collection;
import java.util.concurrent.ExecutorService;

/**
 * 事件工具类
 * 
 * @author L.cm email: [email protected] site:http://www.dreamlu.net date
 *         2015年4月26日下午9:58:53
 */
public class EventKit{
    private static ArrayListMultimap map;
    private static ExecutorService                              pool;

    static void init(ArrayListMultimap map, ExecutorService pool){
        EventKit.map = map;
        EventKit.pool = pool;
    }

    /**
     * 发布事件
     * 
     * @param event
     *            ApplicationEvent
     * @since 1.4.0
     */
    public static void post(final ApplicationEvent event){
        post(EventType.DEFAULT_TAG, event);
    }

    /**
     * 发布事件
     * 
     * @param tag
     *            标记
     * @param event
     *            事件
     * @since 1.4.0
     */
    public static void post(final String tag, final ApplicationEvent event){
        Class eventClazz = event.getClass();
        EventType eventType = new EventType(tag, eventClazz);
        post(eventType, event);
    }

    /**
     * 发布事件
     * 
     * @param eventType
     *            事件封装
     */
    @SuppressWarnings("unchecked")
    private static void post(final EventType eventType, final ApplicationEvent event){
        Collection listenerList = map.get(eventType);
        for(final ListenerHelper helper : listenerList){

            if(null != pool && helper.enableAsync){
                pool.execute(()->helper.listener.onApplicationEvent(event));
            } else{
                helper.listener.onApplicationEvent(event);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy