Skip to content

Commit

Permalink
feat(core): make the ParameterInjectorRegistry methods fluent
Browse files Browse the repository at this point in the history
Fixes #655
  • Loading branch information
Citymonstret committed Jan 22, 2024
1 parent 847cb91 commit 881a6a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ void setup() {
@Test
void testInjection() {
// Arrange
this.commandManager.parameterInjectorRegistry().registerInjector(Integer.class, ParameterInjector.constantInjector(5));
this.commandManager.parameterInjectorRegistry().registerInjector(new TypeToken<StringWrapper>() {
}, ParameterInjector.constantInjector(new StringWrapper("abc")));
this.commandManager.parameterInjectorRegistry().registerInjector(new TypeToken<Wrapper<Boolean>>() {
}, ParameterInjector.constantInjector(new Wrapper<>(true)));
this.commandManager.parameterInjectorRegistry().registerInjector(SomeImplementation.class,
ParameterInjector.constantInjector(new SomeImplementation()));
this.commandManager.parameterInjectorRegistry()
.registerInjector(Integer.class, ParameterInjector.constantInjector(5))
.registerInjector(new TypeToken<StringWrapper>() {}, ParameterInjector.constantInjector(new StringWrapper("abc")))
.registerInjector(
new TypeToken<Wrapper<Boolean>>() {},
ParameterInjector.constantInjector(new Wrapper<>(true))
).registerInjector(
SomeImplementation.class,
ParameterInjector.constantInjector(new SomeImplementation())
);
this.annotationParser.parse(new TestClass());

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.common.returnsreceiver.qual.This;

/**
* Registry containing mappings between {@link Class} {@link Predicate Predicates}
Expand Down Expand Up @@ -71,12 +72,13 @@ public ParameterInjectorRegistry() {
* @param <T> injected type
* @param clazz type that the injector should inject for
* @param injector the injector that should inject the value into the command method
* @return {@code this}
*/
public synchronized <T> void registerInjector(
public synchronized <T> @This @NonNull ParameterInjectorRegistry<C> registerInjector(
final @NonNull Class<T> clazz,
final @NonNull ParameterInjector<C, T> injector
) {
this.registerInjector(TypeToken.get(clazz), injector);
return this.registerInjector(TypeToken.get(clazz), injector);
}

/**
Expand All @@ -85,14 +87,15 @@ public synchronized <T> void registerInjector(
* @param <T> injected type
* @param type type that the injector should inject for
* @param injector the injector that should inject the value into the command method
* @return {@code this}
* @since 2.0.0
*/
@API(status = API.Status.STABLE, since = "2.0.0")
public synchronized <T> void registerInjector(
public synchronized <T> @This @NonNull ParameterInjectorRegistry<C> registerInjector(
final @NonNull TypeToken<T> type,
final @NonNull ParameterInjector<C, T> injector
) {
this.registerInjector(cl -> GenericTypeReflector.isSuperType(cl.getType(), type.getType()), injector);
return this.registerInjector(cl -> GenericTypeReflector.isSuperType(cl.getType(), type.getType()), injector);
}

/**
Expand All @@ -107,14 +110,16 @@ public synchronized <T> void registerInjector(
* @param <T> injected type
* @param predicate a predicate that matches if the injector should be used for a type
* @param injector the injector that should inject the value into the command method
* @return {@code this}
* @since 1.8.0
*/
@API(status = API.Status.STABLE, since = "1.8.0")
public synchronized <T> void registerInjector(
public synchronized <T> @This @NonNull ParameterInjectorRegistry<C> registerInjector(
final @NonNull Predicate<TypeToken<?>> predicate,
final @NonNull ParameterInjector<C, T> injector
) {
this.injectors.add(Pair.of(predicate, injector));
return this;
}

@Override
Expand Down Expand Up @@ -205,12 +210,14 @@ public synchronized <T> void registerInjector(
* {@link #getInjectable(Class, CommandContext, AnnotationAccessor)}.
*
* @param service Service implementation
* @return {@code this}
* @since 1.4.0
*/
@API(status = API.Status.STABLE, since = "1.4.0")
public void registerInjectionService(final InjectionService<C> service) {
public @This @NonNull ParameterInjectorRegistry<C> registerInjectionService(final InjectionService<C> service) {
this.servicePipeline.registerServiceImplementation(new TypeToken<InjectionService<C>>() {
}, service, Collections.emptyList());
return this;
}

private synchronized <T> @NonNull Collection<@NonNull ParameterInjector<C, ?>> injectors(final @NonNull TypeToken<T> type) {
Expand Down

0 comments on commit 881a6a9

Please sign in to comment.