Class ServiceProvider
All the methods in this class are safe to use by multiple concurrent threads.
- Since:
- 1.0
- Version:
- 2.3, May 19, 2023
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA filter and a comparator for processing the stream of service providers. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final AtomicReference<ServiceProvider> The current service provider, ornullif not yet determined.private static final StringClass name of JSR-330 annotation for naming a service provider.private static final StringClass name of JSR-250 annotation for assigning a priority level to a service provider.private static final StringClass name of Jakarta Dependency Injection annotation for naming a service provider.private static final StringClass name of Jakarta Common Annotation for assigning a priority level to a service provider. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final List<ServiceProvider> Returns the list of all service providers available for the current thread's context class loader.static final ServiceProvidercurrent()Returns the currentServiceProvider.abstract FormatServiceintAllows to define a priority for a registeredServiceProviderinstance.abstract <Q extends Quantity<Q>>
QuantityFactory<Q> getQuantityFactory(Class<Q> quantity) Returns a factory for the givenQuantitytype.abstract SystemOfUnitsServiceReturns the service to obtain aSystemOfUnits, ornullif none.static ServiceProviderReturns theServiceProviderwith the specified name.static final ServiceProvidersetCurrent(ServiceProvider provider) Replaces the currentServiceProvider.
-
Field Details
-
LEGACY_NAMED_ANNOTATION
Class name of JSR-330 annotation for naming a service provider. We use reflection for keeping JSR-330 an optional dependency.- See Also:
-
LEGACY_PRIORITY_ANNOTATION
Class name of JSR-250 annotation for assigning a priority level to a service provider. We use reflection for keeping JSR-250 an optional dependency.- See Also:
-
NAMED_ANNOTATION
Class name of Jakarta Dependency Injection annotation for naming a service provider. We use reflection for keeping Jakata Injection an optional dependency.- See Also:
-
PRIORITY_ANNOTATION
Class name of Jakarta Common Annotation for assigning a priority level to a service provider. We use reflection for keeping Jakarta Annotations an optional dependency.- See Also:
-
current
The current service provider, ornullif not yet determined.Implementation Note: We do not cache a list of all service providers because that list depends indirectly on the thread invoking the
available()method. More specifically, it depends on the context class loader. Furthermore caching theServiceProviders can be a source of memory leaks. SeeServiceLoader.load(Class)API note for reference.
-
-
Constructor Details
-
ServiceProvider
protected ServiceProvider()Creates a new service provider. Only to be used by subclasses.
-
-
Method Details
-
getPriority
public int getPriority()Allows to define a priority for a registeredServiceProviderinstance. When multiple providers are registered in the system, the provider with the highest priority value is taken.If the "jakarta.annotation.Priority" annotation (from Jakarta Annotations) or "javax.annotation.Priority" annotation (from JSR-250) is present on the
ServiceProviderimplementation class, then that annotation (first if both were present) is taken and thisgetPriority()method is ignored. Otherwise – if aPriorityannotation is absent – this method is used as a fallback.- Returns:
- the provider's priority (default is 0).
-
getSystemOfUnitsService
Returns the service to obtain aSystemOfUnits, ornullif none.- Returns:
- the service to obtain a
SystemOfUnits, ornull.
-
getFormatService
- Returns:
- the service to obtain a
UnitFormatandQuantityFormat, ornull. - Since:
- 2.0
-
getQuantityFactory
Returns a factory for the givenQuantitytype.- Type Parameters:
Q- the type of theQuantityinstances created by the factory- Parameters:
quantity- the quantity type- Returns:
- the
QuantityFactoryfor the given type
-
available
Returns the list of all service providers available for the current thread's context class loader. The current service provider is always the first item in the returned list. Other service providers after the first item may depend on the caller thread (see service loader API note).- Returns:
- all service providers available for the current thread's context class loader.
-
of
Returns theServiceProviderwith the specified name. The given name must match the name of at least one service provider available in the current thread's context class loader. The service provider names are the values of "jakarta.inject.Named" (from Jakarta Annotations) or "javax.inject.Named" (from JSR-330) annotations when present (first if both were present), or the value ofObject.toString()method for providers withoutNamedannotation.Implementors are encouraged to provide an
Namedannotation or to overrideObject.toString()and use a unique enough name, e.g. the class name or other distinct attributes. Should multiple service providers nevertheless use the same name, the one with the highest priority wins.- Parameters:
name- the name of the service provider to return- Returns:
- the
ServiceProviderwith the specified name - Throws:
IllegalArgumentException- if available service providers do not contain a provider with the specified nameNullPointerException- ifnameis null- Since:
- 2.0
- See Also:
-
current
Returns the currentServiceProvider. If necessary theServiceProviderwill be lazily loaded.If there are no providers available, an IllegalStateException is thrown. Otherwise the provider with the highest priority is used or the one explicitly designated via
setCurrent(ServiceProvider).- Returns:
- the
ServiceProviderused. - Throws:
IllegalStateException- if noServiceProviderhas been found.- See Also:
-
setCurrent
Replaces the currentServiceProvider.- Parameters:
provider- the newServiceProvider- Returns:
- the replaced provider, or null.
-