Class ServiceProvider.Selector

java.lang.Object
javax.measure.spi.ServiceProvider.Selector
All Implemented Interfaces:
Comparator<ServiceProvider>, Predicate<ServiceProvider>
Enclosing class:
ServiceProvider

private static final class ServiceProvider.Selector extends Object implements Predicate<ServiceProvider>, Comparator<ServiceProvider>
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 Details

    • toSearch

      private final String toSearch
      The name of the provider to search, or null if no filtering by name is applied.
    • nameGetter

      private final Method nameGetter
      The value() method in the "jakarta.inject.Named" annotation, or null if that class is not on the classpath.
    • priorityGetter

      private final Method priorityGetter
      The value() method in the "jakarta.annotation.Priority" annotation, or null if that class is not on the classpath.
    • legacyNameGetter

      private final Method legacyNameGetter
      The value() method in the "javax.inject.Named" annotation, or null if that class is not on the classpath.
    • legacyPriorityGetter

      private final Method legacyPriorityGetter
      The value() method in the "javax.annotation.Priority" annotation, or null if that class is not on the classpath.
  • Constructor Details

    • Selector

      Selector(String name)
      Creates a new filter and comparator for a stream of service providers.
      Parameters:
      name - name of the desired service provider, or null if no filtering by name is applied.
  • Method Details

    • getValueMethod

      private static Method getValueMethod(String classname) throws NoSuchMethodException
      Returns the value() method in the given annotation class.
      Parameters:
      classname - name of the class from which to get the value() method.
      Returns:
      the value() method, or null if the annotation class was not found.
      Throws:
      NoSuchMethodException
    • getValue

      private static Object getValue(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 of the provider on which to invoke annotation value().
      getter - the preferred value() method to invoke, or null.
      fallback - an alternative value() method to invoke, or null.
      Returns:
      the value, or null if none.
    • test

      public boolean test(ServiceProvider provider)
      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 Object.toString().
      Specified by:
      test in interface Predicate<ServiceProvider>
    • priority

      private int priority(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().
    • compare

      public int compare(ServiceProvider p1, ServiceProvider p2)
      Compares the given service providers for order based on their priority. The priority of each provider is determined as documented by ServiceProvider.getPriority().
      Specified by:
      compare in interface Comparator<ServiceProvider>
    • stream

      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.