Package org.incendo.cloud.injection
Class ParameterInjectorRegistry<C>
java.lang.Object
org.incendo.cloud.injection.ParameterInjectorRegistry<C>
- Type Parameters:
C- command sender type
- All Implemented Interfaces:
Function<InjectionRequest<C>,,Object> InjectionService<C>,Service<InjectionRequest<C>,Object>
@API(status=STABLE)
public final class ParameterInjectorRegistry<C>
extends Object
implements InjectionService<C>
Registry containing mappings between
Class Predicates
and injectors.
The order injectors are tested is the same order they were registered in.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetInjectable(@NonNull TypeToken<T> type, @NonNull CommandContext<C> context, @NonNull AnnotationAccessor annotationAccessor) Attempts to get an injectable value for the given context.getInjectable(@NonNull Class<T> clazz, @NonNull CommandContext<C> context, @NonNull AnnotationAccessor annotationAccessor) Attempts to get an injectable value for the given context.handle(@NonNull InjectionRequest<C> request) registerInjectionService(InjectionService<C> service) Registers an injection service that will be able to provide injections usinggetInjectable(Class, CommandContext, AnnotationAccessor).registerInjector(@NonNull TypeToken<T> type, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type or any of it's assignable supertypes.registerInjector(@NonNull Class<T> clazz, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type or any of it's assignable supertypes.registerInjector(@NonNull Predicate<TypeToken<?>> predicate, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type predicate.
-
Constructor Details
-
ParameterInjectorRegistry
public ParameterInjectorRegistry()Creates a new parameter injector registry
-
-
Method Details
-
registerInjector
public <T> @NonNull ParameterInjectorRegistry<C> registerInjector(@NonNull Class<T> clazz, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type or any of it's assignable supertypes.- Type Parameters:
T- injected type- Parameters:
clazz- type that the injector should inject forinjector- the injector that should inject the value into the command method- Returns:
this
-
registerInjector
@API(status=STABLE) public <T> @NonNull ParameterInjectorRegistry<C> registerInjector(@NonNull TypeToken<T> type, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type or any of it's assignable supertypes.- Type Parameters:
T- injected type- Parameters:
type- type that the injector should inject forinjector- the injector that should inject the value into the command method- Returns:
this
-
registerInjector
@API(status=STABLE) public <T> @NonNull ParameterInjectorRegistry<C> registerInjector(@NonNull Predicate<TypeToken<?>> predicate, @NonNull ParameterInjector<C, T> injector) Registers an injector for a particular type predicate.The predicate should only return true if the injected type is assignable to the tested type. This predicate overload is provided in addition to
registerInjector(Class, ParameterInjector)to allow for exact, non-exact, or custom predicates, however is still bound by the aforementioned constraint. Failure to adhere to this will result in runtime exceptions.- Type Parameters:
T- injected type- Parameters:
predicate- a predicate that matches if the injector should be used for a typeinjector- the injector that should inject the value into the command method- Returns:
this
-
handle
- Specified by:
handlein interfaceService<InjectionRequest<C>,Object>
-
getInjectable
@API(status=STABLE) public <T> @NonNull Optional<T> getInjectable(@NonNull Class<T> clazz, @NonNull CommandContext<C> context, @NonNull AnnotationAccessor annotationAccessor) Attempts to get an injectable value for the given context.This will consider all registered
injection services, and not just theinjectorsregistered usingregisterInjector(Class, ParameterInjector).- Type Parameters:
T- type to inject- Parameters:
clazz- class to injectcontext- the command context that requests the injectionannotationAccessor- annotation accessor for the injection. If the object is requested without access to annotations, useAnnotationAccessor.empty()- Returns:
- the injected value, if an injector was able to provide a value
-
getInjectable
@API(status=STABLE) public <T> @NonNull Optional<T> getInjectable(@NonNull TypeToken<T> type, @NonNull CommandContext<C> context, @NonNull AnnotationAccessor annotationAccessor) Attempts to get an injectable value for the given context.This will consider all registered
injection services, and not just theinjectorsregistered usingregisterInjector(Class, ParameterInjector).- Type Parameters:
T- type to inject- Parameters:
type- type to injectcontext- the command context that requests the injectionannotationAccessor- annotation accessor for the injection. If the object is requested without access to annotations, useAnnotationAccessor.empty()- Returns:
- the injected value, if an injector was able to provide a value
- Throws:
InjectionException- if any of theinjection servicesthrows an exception
-
registerInjectionService
@API(status=STABLE) public @NonNull ParameterInjectorRegistry<C> registerInjectionService(InjectionService<C> service) Registers an injection service that will be able to provide injections usinggetInjectable(Class, CommandContext, AnnotationAccessor).- Parameters:
service- Service implementation- Returns:
this
-