Class ServicePipeline


  • public final class ServicePipeline
    extends java.lang.Object
    Service pipeline
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static @NonNull ServicePipelineBuilder builder()
      <Context,​Result,​S extends Service<Context,​Result>>
      @NonNull java.util.Collection<io.leangen.geantyref.TypeToken<? extends S>>
      getImplementations​(@NonNull io.leangen.geantyref.TypeToken<S> type)
      Get a collection of all the TypeToken of all implementations for a given type.
      @NonNull java.util.Collection<java.lang.reflect.Type> getRecognizedTypes()
      Get a collection of all the recognised service types.
      <Context> @NonNull ServicePump<Context> pump​(@NonNull Context context)
      Start traversing the pipeline by providing the context that will be used to generate the results
      <T> @NonNull ServicePipeline registerMethods​(@NonNull T instance)
      Scan a given class for methods annotated with ServiceImplementation and register them as service implementations.
      <Context,​Result>
      ServicePipeline
      registerServiceImplementation​(@NonNull io.leangen.geantyref.TypeToken<? extends Service<Context,​Result>> type, @NonNull Service<Context,​Result> implementation, @NonNull java.util.Collection<java.util.function.Predicate<Context>> filters)
      Register a service implementation for a type that is recognized by the pipeline.
      <Context,​Result>
      ServicePipeline
      registerServiceImplementation​(@NonNull java.lang.Class<? extends Service<Context,​Result>> type, @NonNull Service<Context,​Result> implementation, @NonNull java.util.Collection<java.util.function.Predicate<Context>> filters)
      Register a service implementation for a type that is recognized by the pipeline.
      <Context,​Result>
      @NonNull ServicePipeline
      registerServiceType​(@NonNull io.leangen.geantyref.TypeToken<? extends Service<@NonNull Context,​@NonNull Result>> type, @NonNull Service<@NonNull Context,​@NonNull Result> defaultImplementation)
      Register a service type so that it is recognized by the pipeline
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • registerServiceType

        public <Context,​Result> @NonNull ServicePipeline registerServiceType​(@NonNull io.leangen.geantyref.TypeToken<? extends Service<@NonNull Context,​@NonNull Result>> type,
                                                                                   @NonNull Service<@NonNull Context,​@NonNull Result> defaultImplementation)
        Register a service type so that it is recognized by the pipeline
        Type Parameters:
        Context - Service context type
        Result - Service result type
        Parameters:
        type - Service type
        defaultImplementation - Default implementation of the service. This must *always* be supplied and will be the last service implementation that the pipeline attempts to access. This will never be filtered out.
        Returns:
        ServicePipeline The service pipeline instance
      • registerMethods

        public <T> @NonNull ServicePipeline registerMethods​(@NonNull T instance)
                                                     throws java.lang.Exception
        Scan a given class for methods annotated with ServiceImplementation and register them as service implementations.

        The methods should be of the form:

        
         {@literal @}Nullable
         {@literal @}ServiceImplementation(YourService.class)
         public YourResult handle(YourContext) {
              return result;
         }
         
        Type Parameters:
        T - Type of the instance
        Parameters:
        instance - Instance of the class to scan
        Returns:
        Service pipeline instance
        Throws:
        java.lang.Exception - Any exceptions thrown during the registration process
      • registerServiceImplementation

        public <Context,​Result> ServicePipeline registerServiceImplementation​(@NonNull io.leangen.geantyref.TypeToken<? extends Service<Context,​Result>> type,
                                                                                    @NonNull Service<Context,​Result> implementation,
                                                                                    @NonNull java.util.Collection<java.util.function.Predicate<Context>> filters)
        Register a service implementation for a type that is recognized by the pipeline. It is important that a call to registerServiceType(TypeToken, Service) has been made beforehand, otherwise a IllegalArgumentException will be thrown
        Type Parameters:
        Context - Service context type
        Result - Service result type
        Parameters:
        type - Service type
        implementation - Implementation of the service
        filters - Filters that will be used to determine whether or not the service gets used
        Returns:
        ServicePipeline The service pipeline instance
      • registerServiceImplementation

        public <Context,​Result> ServicePipeline registerServiceImplementation​(@NonNull java.lang.Class<? extends Service<Context,​Result>> type,
                                                                                    @NonNull Service<Context,​Result> implementation,
                                                                                    @NonNull java.util.Collection<java.util.function.Predicate<Context>> filters)
        Register a service implementation for a type that is recognized by the pipeline. It is important that a call to registerServiceType(TypeToken, Service) has been made beforehand, otherwise a IllegalArgumentException will be thrown
        Type Parameters:
        Context - Service context type
        Result - Service result type
        Parameters:
        type - Service type
        implementation - Implementation of the service
        filters - Filters that will be used to determine whether or not the service gets used
        Returns:
        ServicePipeline The service pipeline instance
      • pump

        public <Context> @NonNull ServicePump<Context> pump​(@NonNull Context context)
        Start traversing the pipeline by providing the context that will be used to generate the results
        Type Parameters:
        Context - Context type
        Parameters:
        context - Context
        Returns:
        Service pumper instance
      • getRecognizedTypes

        public @NonNull java.util.Collection<java.lang.reflect.Type> getRecognizedTypes()
        Get a collection of all the recognised service types.
        Returns:
        Returns an Immutable collection of the service types registered.
      • getImplementations

        public <Context,​Result,​S extends Service<Context,​Result>> @NonNull java.util.Collection<io.leangen.geantyref.TypeToken<? extends S>> getImplementations​(@NonNull io.leangen.geantyref.TypeToken<S> type)
        Get a collection of all the TypeToken of all implementations for a given type.
        Type Parameters:
        Context - The context type.
        Result - The result type.
        S - The service type.
        Parameters:
        type - The TypeToken of the service to get implementations for.
        Returns:
        Returns an collection of the TypeTokens of the implementations for a given service. Iterator order matches the priority when pumping contexts through the pipeline