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

com.anwen.mongo.cache.global.AwareHandlerCache Maven / Gradle / Ivy

There is a newer version: 2.1.5
Show newest version
package com.anwen.mongo.cache.global;

import com.anwen.mongo.aware.Aware;
import com.anwen.mongo.toolkit.ClassTypeUtil;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 感知处理类缓存
 *
 * @author loser
 * @date 2024/6/29
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class AwareHandlerCache {

    private final Map, List> handlerMap = new ConcurrentHashMap<>();

    private static final AwareHandlerCache instant = new AwareHandlerCache();

    private AwareHandlerCache() {
    }

    public static AwareHandlerCache getInstance() {
        return instant;
    }

    /**
     * 获取感知类下的所有处理器
     *
     * @param clazz 感知类
     * @param    感知类
     * @return 感知处理器集合
     */
    public static  List listHandlers(Class clazz) {

        Object o = getInstance().handlerMap.get(clazz);
        if (Objects.isNull(o)) {
            return Collections.emptyList();
        }
        return (List) o;

    }

    /**
     * 将感知类添加到缓存
     *
     * @param aware 感知类
     * @param    感知类型
     */
    public static synchronized  void putAware(T aware) {

        Class[] interfaces = aware.getClass().getInterfaces();
        for (Class anInterface : interfaces) {
            if (ClassTypeUtil.isTargetClass(Aware.class,anInterface)) {
                List handlers;
                Object o = getInstance().handlerMap.get(anInterface);
                if (Objects.nonNull(o)) {
                    handlers = (List) o;
                } else {
                    handlers = new ArrayList<>();
                    getInstance().handlerMap.put(anInterface, handlers);
                }
                handlers.add(aware);
            }
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy