- java.lang.Object
-
- eu.ess.xaos.tools.annotation.ServiceLoaderUtilities
-
public class ServiceLoaderUtilities extends Object
Provides few more methods toServiceLoader
.Note: when using
ServiceLoaderUtilities
the service provider interface type's package must be opened to thexaos.tools
module, i.e. it must be included into an opens statement in themodule-info
class.- 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 theList
of 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 theList
of implementers of the given service provider interface (spi
).static <S> List<ServiceLoader.Provider<S>>
providersOf(ServiceLoader<S> loader)
Returns theList
ofServiceLoader.Provider
s 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 theList
of classes implementing the given service provider interface (spi
). AServiceLoader
is created for the givenservice
type, 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
- TheServiceLoader
for which a sortedStream
must be returned.- Returns:
- The
List
of 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
- TheServiceLoader
for which a sortedStream
must be returned.- Returns:
- The first service provider or empty
Optional
if no service providers are located.
-
of
public static <S> List<S> of(ServiceLoader<S> loader)
Returns theList
of 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
- TheServiceLoader
for which a sortedStream
must be returned.- Returns:
- The
List
of found service provider implementers.
-
providersOf
public static <S> List<ServiceLoader.Provider<S>> providersOf(ServiceLoader<S> loader)
Returns theList
ofServiceLoader.Provider
s 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
- TheServiceLoader
for which a sortedStream
must be returned.- Returns:
- The
List
of found serviceServiceLoader.Provider
s.
-
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
- TheServiceLoader
for which a sortedStream
must be returned.- Returns:
- A sorted stream that lazily loads providers for this loader's service.
- See Also:
ServiceLoader.stream()
-
-