Class ServiceProvider

Object
ServiceProvider

public abstract class ServiceProvider extends Object
Service Provider for Units of Measurement services.

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

Since:
1.0
Version:
2.3, May 19, 2023
Author:
Werner Keil, Martin Desruisseaux
  • 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 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:
      the provider's priority (default is 0).
    • getSystemOfUnitsService

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

      public abstract FormatService getFormatService()
      Returns the service to obtain UnitFormat and QuantityFormat or null if none.
      Returns:
      the service to obtain a UnitFormat and QuantityFormat, or null.
      Since:
      2.0
    • getQuantityFactory

      public abstract <Q extends Quantity<Q>> QuantityFactory<Q> getQuantityFactory(Class<Q> quantity)
      Returns a factory for the given Quantity type.
      Type Parameters:
      Q - the type of the Quantity instances created by the factory
      Parameters:
      quantity - the quantity type
      Returns:
      the QuantityFactory for the given type
    • available

      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:
      all service providers available for the current thread's context class loader.
    • of

      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 Object.toString() method for providers without Named annotation.

      Implementors are encouraged to provide an Named annotation or to override Object.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 ServiceProvider with the specified name
      Throws:
      IllegalArgumentException - if available service providers do not contain a provider with the specified name
      NullPointerException - if name is null
      Since:
      2.0
      See Also:
    • current

      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:
      the ServiceProvider used.
      Throws:
      IllegalStateException - if no ServiceProvider has been found.
      See Also:
    • setCurrent

      public static final ServiceProvider setCurrent(ServiceProvider provider)
      Replaces the current ServiceProvider.
      Parameters:
      provider - the new ServiceProvider
      Returns:
      the replaced provider, or null.