Skip to content

Commit

Permalink
Changed context resolver factory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Dec 23, 2024
1 parent fc29825 commit 4ba03ed
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/dev/velix/imperat/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ConfigBuilder<S, I> postProcessingChain(CommandProcessingChain<S, Command
}

// Context Resolver Factory
public ConfigBuilder<S, I> contextResolverFactory(Type type, ContextResolverFactory<S> factory) {
public <T> ConfigBuilder<S, I> contextResolverFactory(Type type, ContextResolverFactory<S, T> factory) {
config.registerContextResolverFactory(type, factory);
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/dev/velix/imperat/ImperatConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ default <T> ContextResolver<S, T> getContextResolver(CommandParameter<S> command
* {@link ContextResolver}
*/
@Nullable
ContextResolverFactory<S> getContextResolverFactory(Type resolvingContextType);
<T> ContextResolverFactory<S, T> getContextResolverFactory(Type resolvingContextType);

/**
* @return {@link PermissionResolver} for the dispatcher
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/dev/velix/imperat/ImperatConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public boolean hasContextResolver(Type type) {
* @param factory the factory to register
*/
@Override
public void registerContextResolverFactory(Type type, ContextResolverFactory<S> factory) {
public <T> void registerContextResolverFactory(Type type, ContextResolverFactory<S, T> factory) {
contextResolverRegistry.registerFactory(type, factory);
}

Expand All @@ -292,8 +292,9 @@ public void registerContextResolverFactory(Type type, ContextResolverFactory<S>
* {@link ContextResolver}
*/
@Override
public @Nullable ContextResolverFactory<S> getContextResolverFactory(Type type) {
return contextResolverRegistry.getFactoryFor(type).orElse(null);
@SuppressWarnings("unchecked")
public <T> @Nullable ContextResolverFactory<S, T> getContextResolverFactory(Type type) {
return (ContextResolverFactory<S, T>) contextResolverRegistry.getFactoryFor(type).orElse(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed interface ResolverRegistrar<S extends Source> permits ImperatConfi
*
* @param factory the factory to register
*/
void registerContextResolverFactory(Type type, ContextResolverFactory<S> factory);
<T> void registerContextResolverFactory(Type type, ContextResolverFactory<S, T> factory);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @param <S> the command-sender valueType
*/
public interface ContextResolverFactory<S extends Source> {
public interface ContextResolverFactory<S extends Source, T> {

/**
* Creates a context resolver based on the parameter
Expand All @@ -22,6 +22,6 @@ public interface ContextResolverFactory<S extends Source> {
* @return the {@link ContextResolver} specific for that parameter
*/
@Nullable
<T> ContextResolver<S, T> create(Type type, @Nullable ParameterElement parameter);
ContextResolver<S, T> create(Type type, @Nullable ParameterElement parameter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.Optional;

@ApiStatus.AvailableSince("1.0.0")
public final class ContextResolverRegistry<S extends Source> extends Registry<Type, ContextResolver<S, ?>> implements ContextResolverFactory<S> {
public final class ContextResolverRegistry<S extends Source> extends Registry<Type, ContextResolver<S, ?>> {

private final Registry<Type, ContextResolverFactory<S>> factories = new Registry<>();
private final Registry<Type, ContextResolverFactory<S, ?>> factories = new Registry<>();

private ContextResolverRegistry(final ImperatConfig<S> config) {
super();
Expand All @@ -32,20 +32,24 @@ public <T> void registerResolver(Type type, ContextResolver<S, T> resolver) {
setData(type, resolver);
}

public void registerFactory(Type type, ContextResolverFactory<S> factory) {
public <T> void registerFactory(Type type, ContextResolverFactory<S, T> factory) {
factories.setData(type, factory);
}

public Optional<ContextResolverFactory<S>> getFactoryFor(Type type) {
public Optional<ContextResolverFactory<S, ?>> getFactoryFor(Type type) {
return factories.getData(type);
}

@SuppressWarnings("unchecked")
public <T> @Nullable ContextResolver<S, T> getContextResolver(Type type, @Nullable ParameterElement element) {
//we search for factories mainly
return (ContextResolver<S, T>) getFactoryFor(type)
.flatMap((factory) -> Optional.ofNullable(factory.create(type, element)))
.orElseGet(() -> this.create(type, element));
ContextResolverFactory<S, T> factory = (ContextResolverFactory<S, T>) getFactoryFor(type).orElse(null);
if (factory == null) {
return factories.getData(type)
.map((defaultFactory) -> ((ContextResolverFactory<S, T>) defaultFactory).create(type, element))
.orElse(null);
}
return factory.create(type, element);
}

public <T> @Nullable ContextResolver<S, T> getResolverWithoutParameterElement(Type type) {
Expand All @@ -62,15 +66,4 @@ public Optional<ContextResolverFactory<S>> getFactoryFor(Type type) {
return resultFromFactory;
}

/**
* Creates a context resolver based on the parameter
*
* @param parameter the parameter (null if used classic way)
* @return the {@link ContextResolver} specific for that parameter
*/
@Override
@SuppressWarnings("unchecked")
public @Nullable <T> ContextResolver<S, T> create(Type type, @Nullable ParameterElement parameter) {
return (ContextResolver<S, T>) getData(type).orElse(null);
}
}

0 comments on commit 4ba03ed

Please sign in to comment.