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

io.github.quickmsg.common.spi.DynamicLoader Maven / Gradle / Ivy

The newest version!
package io.github.quickmsg.common.spi;

import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @author luxurong
 */
public final class DynamicLoader {


    private DynamicLoader() {
    }

    public static  Optional findFirst(Class clazz) {
        ServiceLoader load = ServiceLoader.load(clazz);
        return StreamSupport.stream(load.spliterator(), false).findFirst();
    }

    public static  Optional findFirst(Class clazz, Predicate predicate) {
        ServiceLoader load = ServiceLoader.load(clazz);
        Stream stream = StreamSupport.stream(load.spliterator(), false);
        return stream.filter(predicate).findFirst();
    }

    public static  Stream findAll(Class clazz) {
        ServiceLoader load = ServiceLoader.load(clazz);
        return StreamSupport.stream(load.spliterator(), false);
    }

    public static  Stream findAll(Class clazz, Predicate predicate) {
        ServiceLoader load = ServiceLoader.load(clazz);
        return StreamSupport.stream(load.spliterator(), false).filter(predicate);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy