Top Description Inners Fields Constructors Methods
javax.measure.spi

public abstract Class ServiceProvider

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.InvocationTargetException, .Method, java.util.ArrayList, .Comparator, .List, .Objects, .Optional, .ServiceConfigurationError, .ServiceLoader, java.util.concurrent.atomic.AtomicReference, java.util.function.Predicate, java.util.logging.Level, .Logger, java.util.stream.Collectors, .Stream, .StreamSupport, javax.measure.Quantity, javax.measure.format.QuantityFormat, .UnitFormat

Service Provider for Units of Measurement services.

All the methods in this class are safe to use by multiple concurrent threads.

Authors
Werner Keil, Martin Desruisseaux
Since
1.0
Version
2.3, May 19, 2023

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ServiceProvider.Selector

A filter and a comparator for processing the stream of service providers.

Field Summary

Modifier and TypeField and Description
private static final AtomicReference<ServiceProvider>
current

The current service provider, or null if not yet determined.

private static final String
LEGACY_NAMED_ANNOTATION

Class name of JSR-330 annotation for naming a service provider.

private static final String
LEGACY_PRIORITY_ANNOTATION

Class name of JSR-250 annotation for assigning a priority level to a service provider.

private static final String
NAMED_ANNOTATION

Class name of Jakarta Dependency Injection annotation for naming a service provider.

private static final String
PRIORITY_ANNOTATION

Class name of Jakarta Common Annotation for assigning a priority level to a service provider.

Constructor Summary

AccessConstructor and Description
protected
ServiceProvider()

Creates a new service provider.

Method Summary

Modifier and TypeMethod and Description
public static final List<ServiceProvider>

Returns:

all service providers available for the current thread's context class loader.
available
()

Returns the list of all service providers available for the current thread's context class loader.

public static final ServiceProvider

Returns:

the ServiceProvider used.
current
()

Returns the current ServiceProvider.

public abstract FormatService

Returns:

the service to obtain a UnitFormat and QuantityFormat, or null.
getFormatService
()

Returns the service to obtain UnitFormat and QuantityFormat or null if none.

public int

Returns:

the provider's priority (default is 0).
getPriority
()

Allows to define a priority for a registered ServiceProvider instance.

public abstract <
the type of the Quantity instances created by the factory
Q extends Quantity<Q>
>
QuantityFactory<Q>

Returns:

the QuantityFactory for the given type
getQuantityFactory
(Class<Q>
the quantity type
quantity
)

Returns a factory for the given Quantity type.

public abstract SystemOfUnitsService

Returns:

the service to obtain a SystemOfUnits, or null.
getSystemOfUnitsService
()

Returns the service to obtain a SystemOfUnits, or null if none.

public static ServiceProvider

Returns:

the ServiceProvider with the specified name
of
(String
the name of the service provider to return
name
)

Returns the ServiceProvider with the specified name.

public static final ServiceProvider

Returns:

the replaced provider, or null.
setCurrent
(ServiceProvider provider)

Replaces the current ServiceProvider.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
private static final AtomicReference<ServiceProvider> current

The current service provider, or null if 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 the ServiceProviders can be a source of memory leaks. See ServiceLoader#load(Class) API note for reference.

LEGACY_NAMED_ANNOTATIONback to summary
private static final String LEGACY_NAMED_ANNOTATION

Class name of JSR-330 annotation for naming a service provider. We use reflection for keeping JSR-330 an optional dependency.

LEGACY_PRIORITY_ANNOTATIONback to summary
private static final String 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.

NAMED_ANNOTATIONback to summary
private static final String NAMED_ANNOTATION

Class name of Jakarta Dependency Injection annotation for naming a service provider. We use reflection for keeping Jakata Injection an optional dependency.

PRIORITY_ANNOTATIONback to summary
private static final String 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.

Constructor Detail

ServiceProviderback to summary
protected ServiceProvider()

Creates a new service provider. Only to be used by subclasses.

Method Detail

availableback to summary
public static final List<ServiceProvider> 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:List<ServiceProvider>

all service providers available for the current thread's context class loader.

currentback to summary
public static final ServiceProvider current()

Returns the current ServiceProvider. If necessary the ServiceProvider will 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:ServiceProvider

the ServiceProvider used.

Exceptions
IllegalStateException:
if no ServiceProvider has been found.
See Also
getPriority(), setCurrent(ServiceProvider)
getFormatServiceback to summary
public abstract FormatService getFormatService()

Returns the service to obtain UnitFormat and QuantityFormat or null if none.

Returns:FormatService

the service to obtain a UnitFormat and QuantityFormat, or null.

Since
2.0
getPriorityback to summary
public int getPriority()

Allows to define a priority for a registered ServiceProvider instance. 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 ServiceProvider implementation class, then that annotation (first if both were present) is taken and this getPriority() method is ignored. Otherwise – if a Priority annotation is absent – this method is used as a fallback.

Returns:int

the provider's priority (default is 0).

getQuantityFactoryback to summary
public abstract <Q extends Quantity<Q>> QuantityFactory<Q> getQuantityFactory(Class<Q> quantity)

Returns a factory for the given Quantity type.

Parameters
<Q>
the type of the Quantity instances created by the factory
quantity:Class<Q>

the quantity type

Returns:QuantityFactory<Q>

the QuantityFactory for the given type

getSystemOfUnitsServiceback to summary
public abstract SystemOfUnitsService getSystemOfUnitsService()

Returns the service to obtain a SystemOfUnits, or null if none.

Returns:SystemOfUnitsService

the service to obtain a SystemOfUnits, or null.

ofback to summary
public static ServiceProvider of(String name)

Returns the ServiceProvider with 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 of toString() method for providers without Named annotation.

Implementors are encouraged to provide an Named annotation or to override 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:String

the name of the service provider to return

Returns:ServiceProvider

the ServiceProvider with the specified name

Exceptions
IllegalArgumentException:
if available service providers do not contain a provider with the specified name
NullPointerException:
if name is null
Since
2.0
See Also
toString()
setCurrentback to summary
public static final ServiceProvider setCurrent(ServiceProvider provider)

Replaces the current ServiceProvider.

Parameters
provider:ServiceProvider

the new ServiceProvider

Returns:ServiceProvider

the replaced provider, or null.

javax.measure.spi back to summary

private final Class ServiceProvider.Selector

extends Object
implements Predicate<ServiceProvider>, Comparator<ServiceProvider>
Class Inheritance
All Implemented Interfaces
java.util.Comparator, java.util.function.Predicate

A filter and a comparator for processing the stream of service providers. The two tasks (filtering and sorting) are implemented by the same class, but the filter task shall be used only if the name to search is non-null. The comparator is used in all cases, for sorting providers with higher priority first.

Field Summary

Modifier and TypeField and Description
private final Method
legacyNameGetter

The value() method in the javax.inject.Named annotation, or null if that class is not on the classpath.

private final Method
legacyPriorityGetter

The value() method in the javax.annotation.Priority annotation, or null if that class is not on the classpath.

private final Method
nameGetter

The value() method in the jakarta.inject.Named annotation, or null if that class is not on the classpath.

private final Method
priorityGetter

The value() method in the jakarta.annotation.Priority annotation, or null if that class is not on the classpath.

private final String
toSearch

The name of the provider to search, or null if no filtering by name is applied.

Constructor Summary

AccessConstructor and Description
pack-priv
Selector(String
name of the desired service provider, or null if no filtering by name is applied.
name
)

Creates a new filter and comparator for a stream of service providers.

Method Summary

Modifier and TypeMethod and Description
public int
compare(final ServiceProvider
the first object to be compared.
p1
,
final ServiceProvider
the second object to be compared.
p2
)

Implements java.util.Comparator.compare.

Compares the given service providers for order based on their priority.
private static Object

Returns:

the value, or null if none.
getValue
(final Class<?>
class of the provider on which to invoke annotation value().
provider
,
Method
the preferred value() method to invoke, or null.
getter
,
Method
an alternative value() method to invoke, or null.
fallback
)

Invokes the value() method on the annotation of the given class.

private static Method

Returns:

the value() method, or null if the annotation class was not found.
getValueMethod
(final String
name of the class from which to get the value() method.
classname
)

Returns the value() method in the given annotation class.

private int
priority(final ServiceProvider provider)

Returns the priority of the given service provider.

private Stream<ServiceProvider>
stream()

Gets all ServiceProviders sorted by priority and optionally filtered by the name in this selector.

public boolean
test(final ServiceProvider
the input argument
provider
)

Implements java.util.function.Predicate.test.

Returns true if the given service provider has the name we are looking for.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

legacyNameGetterback to summary
private final Method legacyNameGetter

The value() method in the javax.inject.Named annotation, or null if that class is not on the classpath.

legacyPriorityGetterback to summary
private final Method legacyPriorityGetter

The value() method in the javax.annotation.Priority annotation, or null if that class is not on the classpath.

nameGetterback to summary
private final Method nameGetter

The value() method in the jakarta.inject.Named annotation, or null if that class is not on the classpath.

priorityGetterback to summary
private final Method priorityGetter

The value() method in the jakarta.annotation.Priority annotation, or null if that class is not on the classpath.

toSearchback to summary
private final String toSearch

The name of the provider to search, or null if no filtering by name is applied.

Constructor Detail

Selectorback to summary
pack-priv Selector(String name)

Creates a new filter and comparator for a stream of service providers.

Parameters
name:String

name of the desired service provider, or null if no filtering by name is applied.

Method Detail

compareback to summary
public int compare(final ServiceProvider p1, final ServiceProvider p2)

Implements java.util.Comparator.compare.

Compares the given service providers for order based on their priority. The priority of each provider is determined as documented by ServiceProvider#getPriority().

Parameters
p1:ServiceProvider

Doc from java.util.Comparator.compare.

the first object to be compared.

p2:ServiceProvider

Doc from java.util.Comparator.compare.

the second object to be compared.

Returns:int

Doc from java.util.Comparator.compare.

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
getValueback to summary
private static Object getValue(final Class<?> provider, Method getter, Method fallback)

Invokes the value() method on the annotation of the given class. The annotation on which to invoke the method is given by Method#getDeclaringClass().

Parameters
provider:Class<?>

class of the provider on which to invoke annotation value().

getter:Method

the preferred value() method to invoke, or null.

fallback:Method

an alternative value() method to invoke, or null.

Returns:Object

the value, or null if none.

getValueMethodback to summary
private static Method getValueMethod(final String classname) throws NoSuchMethodException

Returns the value() method in the given annotation class.

Parameters
classname:String

name of the class from which to get the value() method.

Returns:Method

the value() method, or null if the annotation class was not found.

priorityback to summary
private int priority(final ServiceProvider provider)

Returns the priority of the given service provider. This method looks for the jakarta.annotation.Priority and javax.annotation.Priority annotations in that order, and if none are found falls back on ServiceProvider#getPriority().

streamback to summary
private Stream<ServiceProvider> stream()

Gets all ServiceProviders sorted by priority and optionally filtered by the name in this selector. The list of service providers is not cached because it depends on the context class loader, which itself depends on which thread is invoking this method.

testback to summary
public boolean test(final ServiceProvider provider)

Implements java.util.function.Predicate.test.

Returns true if the given service provider has the name we are looking for. This method shall be invoked only if a non-null name has been specified to the constructor. This method looks for the jakarta.inject.Named and javax.inject.Named annotations in that order, and if none are found fallbacks on ServiceProvider#toString().

Parameters
provider:ServiceProvider

Doc from java.util.function.Predicate.test.

the input argument

Returns:boolean

Doc from java.util.function.Predicate.test.

true if the input argument matches the predicate, otherwise false

Annotations
@Override