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

fun.langel.cql.spi.Loader Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package fun.langel.cql.spi;

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

/**
 * @author [email protected](GuHan)
 * @date 2023/4/5 22:49
 **/
public class Loader {

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

    private Loader() {
    }

    public static  List load(Class klass) {
        if (cache.containsKey(klass)) {
            return (List) cache.get(klass);
        }
        synchronized (klass) {
            ServiceLoader loader = ServiceLoader.load(klass, classLoader());
            Iterator it = loader.iterator();
            while (it.hasNext()) {
                cache.computeIfAbsent(klass, (r) -> new LinkedList()).add(it.next());
            }
        }
        return cache.get(klass);
    }


    public static  T loadSingle(Class klass) {
        List v = load(klass);
        if (v == null || v.isEmpty()) {
            return null;
        }
        return v.get(0);
    }

    public static ClassLoader classLoader() {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        if (cl == null) {
            cl = Loader.class.getClassLoader();
        }
        if (cl == null) {
            cl = ClassLoader.getSystemClassLoader();
        }
        return cl;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy