Class PriorityServiceLoader<S>
- All Implemented Interfaces:
Iterable<S>
A service loader which loads classes aggressively sorting the implementations by the value in the Priority
annotation. If the implementation does not include the annotation Integer.MAX_VALUE
is used for the priority.
The instances themselves are lazily created.
Constructor
If a constructor function is used the argument passed to the function is the resolved type. The function is free to construct the type however it sees fit.Example:
PriorityServiceLoader.load(Service.class, (service) -> {
try {
return service.getConstructor(String.class).newInstance("init value");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
- Since:
- 6.1
- Author:
- James R. Perkins
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionfirst()
If there are services available the first one is returned.getTypes()
Returns the types found for this service.iterator()
last()
If there are services available the last one is returned.static <S> PriorityServiceLoader
<S> Creates a new service loader for the type.static <S> PriorityServiceLoader
<S> load
(Class<S> type, ClassLoader cl) Creates a new service loader for the type and class loader.static <S> PriorityServiceLoader
<S> load
(Class<S> type, ClassLoader cl, Function<Class<? extends S>, S> constructor) Creates a new service loader for the type and class loader.static <S> PriorityServiceLoader
<S> Creates a new service loader for the type.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
load
Creates a new service loader for the type.To resolve the class loader this first attempts to get the current threads context class loader. If that is
null
the services class loader is used. Finally if the class loader from the service isnull
then the system class loader is used.- Parameters:
type
- the type to load the services for- Returns:
- a new service loader
- Throws:
SecurityException
- if the security manager is enabled there is a security issue loading the class or retrieving the class loader
-
load
public static <S> PriorityServiceLoader<S> load(Class<S> type, Function<Class<? extends S>, S> constructor) Creates a new service loader for the type.To resolve the class loader this first attempts to get the current threads context class loader. If that is
null
the services class loader is used. Finally if the class loader from the service isnull
then the system class loader is used.- Parameters:
type
- the type to load the services forconstructor
- an optional constructor used to construct the type- Returns:
- a new service loader
- Throws:
SecurityException
- if the security manager is enabled there is a security issue loading the class or retrieving the class loader
-
load
Creates a new service loader for the type and class loader.- Parameters:
type
- the type to load the services forcl
- the class loader used to load the found services- Returns:
- a new service loader
- Throws:
SecurityException
- if the security manager is enabled and there is a security issue loading the class
-
load
public static <S> PriorityServiceLoader<S> load(Class<S> type, ClassLoader cl, Function<Class<? extends S>, S> constructor) Creates a new service loader for the type and class loader.- Parameters:
type
- the type to load the services forcl
- the class loader used to load the found servicesconstructor
- an optional constructor used to construct the type- Returns:
- a new service loader
- Throws:
SecurityException
- if the security manager is enabled and there is a security issue loading the class
-
first
If there are services available the first one is returned.- Returns:
- the first service or an empty optional
- Throws:
SecurityException
- if the security manager is enabled and there is a security error instantiating the object
-
last
If there are services available the last one is returned.- Returns:
- the last service or an empty optional
- Throws:
SecurityException
- if the security manager is enabled and there is a security error instantiating the object
-
getTypes
-
iterator
-
toString
-