Class SoftServiceLoader<S>

  • Type Parameters:
    S - The service type
    All Implemented Interfaces:
    java.lang.Iterable<ServiceDefinition<S>>

    public final class SoftServiceLoader<S>
    extends java.lang.Object
    implements java.lang.Iterable<ServiceDefinition<S>>

    Variation of ServiceLoader that allows soft loading and conditional loading of META-INF/services classes.

    Since:
    1.0
    • Field Detail

      • META_INF_SERVICES

        public static final java.lang.String META_INF_SERVICES
        See Also:
        Constant Field Values
    • Method Detail

      • load

        public static <S> SoftServiceLoader<S> load​(java.lang.Class<S> service)
        Creates a new SoftServiceLoader using the thread context loader by default.
        Type Parameters:
        S - The service generic type
        Parameters:
        service - The service type
        Returns:
        A new service loader
      • load

        public static <S> SoftServiceLoader<S> load​(java.lang.Class<S> service,
                                                    java.lang.ClassLoader loader)
        Creates a new SoftServiceLoader using the given type and class loader.
        Type Parameters:
        S - The service generic type
        Parameters:
        service - The service type
        loader - The class loader
        Returns:
        A new service loader
      • load

        public static <S> SoftServiceLoader<S> load​(java.lang.Class<S> service,
                                                    java.lang.ClassLoader loader,
                                                    java.util.function.Predicate<java.lang.String> condition)
        Creates a new SoftServiceLoader using the given type and class loader.
        Type Parameters:
        S - The service generic type
        Parameters:
        service - The service type
        loader - The class loader to use
        condition - A Predicate to use to conditionally load the service. The predicate is passed the service class name
        Returns:
        A new service loader
      • first

        public java.util.Optional<ServiceDefinition<S>> first()
        Returns:
        Return the first such instance
      • firstAvailable

        public java.util.Optional<S> firstAvailable()
        Find the first service definition that is present, and then load it.
        Returns:
        Return the first such instance, or Optional.empty() if there is no definition or none of the definitions are present on the classpath.
      • firstOr

        public java.util.Optional<ServiceDefinition<S>> firstOr​(java.lang.String alternative,
                                                                java.lang.ClassLoader classLoader)
        Parameters:
        alternative - An alternative type to use if the this type is not present
        classLoader - The classloader
        Returns:
        Return the first such instance
      • collectAll

        public void collectAll​(@NonNull
                               java.util.Collection<S> values,
                               @Nullable
                               java.util.function.Predicate<S> predicate)
        Collects all initialized instances.
        Parameters:
        values - The collection to be populated.
        predicate - The predicated to filter the instances or null if not needed.
      • collectAll

        public void collectAll​(@NonNull
                               java.util.Collection<S> values)
        Collects all initialized instances.
        Parameters:
        values - The collection to be populated.
      • collectAll

        public java.util.List<S> collectAll()
        Collects all initialized instances.
        Returns:
        The instances of this service.
      • collectAll

        public java.util.List<S> collectAll​(java.util.function.Predicate<S> predicate)
        Collects all initialized instances.
        Parameters:
        predicate - The predicated to filter the instances or null if not needed.
        Returns:
        The instances of this service.
      • iterator

        @NonNull
        public java.util.Iterator<ServiceDefinition<S>> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<S>
        Returns:
        The iterator
      • newService

        @Deprecated
        protected ServiceDefinition<S> newService​(java.lang.String name,
                                                  java.util.Optional<java.lang.Class> loadedClass)
        Deprecated.
        No longer used
        Parameters:
        name - The name
        loadedClass - The loaded class
        Returns:
        The service definition
      • newCollector

        public static <S> SoftServiceLoader.ServiceCollector<S> newCollector​(java.lang.String serviceName,
                                                                             java.util.function.Predicate<java.lang.String> lineCondition,
                                                                             java.lang.ClassLoader classLoader,
                                                                             java.util.function.Function<java.lang.String,​S> transformer)