- java.lang.Object
-
- eu.ess.xaos.tools.annotation.ServiceLoaderUtilities
-
public class ServiceLoaderUtilities extends Object
Provides few more methods toServiceLoader.Note: when using
ServiceLoaderUtilitiesthe service provider interface type's package must be opened to thexaos.toolsmodule, i.e. it must be included into an opens statement in themodule-infoclass.- Author:
- claudio.rosati@esss.se
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <S> List<Class<? extends S>>classesOf(ServiceLoader<S> loader)Returns theListof classes implementing the given service provider interface (spi).static <S> Optional<S>findFirst(ServiceLoader<S> loader)Load the first available service provider of the given service provider interface (spi).static <S> List<S>of(ServiceLoader<S> loader)Returns theListof implementers of the given service provider interface (spi).static <S> List<ServiceLoader.Provider<S>>providersOf(ServiceLoader<S> loader)Returns theListofServiceLoader.Providers implementing the given service provider interface (spi).static <S> Stream<ServiceLoader.Provider<S>>stream(ServiceLoader<S> loader)Returns a sorted stream to lazily load available providers of the givenloader's service.
-
-
-
Method Detail
-
classesOf
public static <S> List<Class<? extends S>> classesOf(ServiceLoader<S> loader)
Returns theListof classes implementing the given service provider interface (spi). AServiceLoaderis created for the givenservicetype, using the current thread's context class loader.This is equivalent to call
ServiceLoaderUtilities.stream(loader) .map(Provider::type) .collect(Collectors.toList());- Type Parameters:
S- The service provider interface type.- Parameters:
loader- TheServiceLoaderfor which a sortedStreammust be returned.- Returns:
- The
Listof found service provider classes.
-
findFirst
public static <S> Optional<S> findFirst(ServiceLoader<S> loader)
Load the first available service provider of the given service provider interface (spi).This is equivalent to call
ServiceLoaderUtilities.stream(loader) .map(Provider::get) .findFirst();The following example loads the first available service provider. If no service providers are located then it uses a default implementation.
CodecFactory factory = ServiceLoaderUtilities.findFirst(CodecFactory.class) .orElse(DEFAULT_CODECSET_FACTORY);- Type Parameters:
S- The service provider interface type.- Parameters:
loader- TheServiceLoaderfor which a sortedStreammust be returned.- Returns:
- The first service provider or empty
Optionalif no service providers are located.
-
of
public static <S> List<S> of(ServiceLoader<S> loader)
Returns theListof implementers of the given service provider interface (spi).This is equivalent to call
ServiceLoaderUtilities.stream(loader) .map(Provider::get) .collect(Collectors.toList());- Type Parameters:
S- The service provider interface type.- Parameters:
loader- TheServiceLoaderfor which a sortedStreammust be returned.- Returns:
- The
Listof found service provider implementers.
-
providersOf
public static <S> List<ServiceLoader.Provider<S>> providersOf(ServiceLoader<S> loader)
Returns theListofServiceLoader.Providers implementing the given service provider interface (spi).This is equivalent to call
ServiceLoaderUtilities.stream(loader) .collect(Collectors.toList());- Type Parameters:
S- The service provider interface type.- Parameters:
loader- TheServiceLoaderfor which a sortedStreammust be returned.- Returns:
- The
Listof found serviceServiceLoader.Providers.
-
stream
public static <S> Stream<ServiceLoader.Provider<S>> stream(ServiceLoader<S> loader)
Returns a sorted stream to lazily load available providers of the givenloader's service. The stream elements are of typeServiceLoader.Provider. TheServiceLoader.Provider's get method must be invoked to get or instantiate the provider.If the provider class is annotated with
ServiceProvider, and a specific order number is given, then it will be used to sort the elements of the returned stream (low order first). Non annotated providers and annotated ones whose order is not explicitly set will be the last ones in the returned stream.This is equivalent to call
loader .stream() .sorted(( p1, p2 ) -> { Class<? extends S> t1 = p1.type(); int o1 = t1.isAnnotationPresent(ServiceProvider.class) ? t1.getAnnotation(ServiceProvider.class).order() : Integer.MAX_VALUE; Class<? extends S> t2 = p2.type(); int o2 = t2.isAnnotationPresent(ServiceProvider.class) ? t2.getAnnotation(ServiceProvider.class).order() : Integer.MAX_VALUE; return o1 - o2; });- Type Parameters:
S- The service provider interface type.- Parameters:
loader- TheServiceLoaderfor which a sortedStreammust be returned.- Returns:
- A sorted stream that lazily loads providers for this loader's service.
- See Also:
ServiceLoader.stream()
-
-