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 super T> 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 super T> predicate) {
ServiceLoader load = ServiceLoader.load(clazz);
return StreamSupport.stream(load.spliterator(), false).filter(predicate);
}
}