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

top.jiaojinxin.jln.event.DefaultEventHandlerRepository Maven / Gradle / Ivy

There is a newer version: 0.1.7
Show newest version
package top.jiaojinxin.jln.event;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 默认事件处理器存储库
 *
 * @author JiaoJinxin
 */
public class DefaultEventHandlerRepository implements EventHandlerRepository {

    private final Map, List>> repository = new HashMap<>();

    @Override
    public void register(EventHandler eventHandler) {
        this.repository.putIfAbsent(eventHandler.eventClass(), new ArrayList<>());
        List> eventHandlers = this.repository.get(eventHandler.eventClass());
        if (eventHandlers.contains(eventHandler)) {
            return;
        }
        eventHandlers.add(eventHandler);
    }

    @Override
    public void registerAll(Collection> eventHandlers) {
        if (CollectionUtils.isEmpty(eventHandlers)) {
            return;
        }
        this.repository.clear();
        this.repository.putAll(eventHandlers.stream().collect(Collectors.groupingBy(EventHandler::eventClass)));
    }

    @Override
    public void remove(EventHandler eventHandler) {
        if (this.repository.containsKey(eventHandler.eventClass())) {
            this.repository.get(eventHandler.eventClass()).remove(eventHandler);
        }
    }

    @Override
    public List> find(Class clazz) {
        return this.repository.getOrDefault(clazz, Collections.emptyList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy