Add deregister ability to completions and contexts (#364)

* feat: add deregister methods to completions and dependencies

* fix: change unregister to deregister

* feat: add javadocs to deregisterCompleition

* fix: add throws to javadocs for dependency methods

* fix: missing negation thanks @JOO200

* fix: change deregister -> unregister

* Fix missed rename of deregister to unregister

---------

Co-authored-by: chickeneer <emcchickeneer@gmail.com>
This commit is contained in:
Christopher Nethercott
2023-04-02 22:47:44 +01:00
committed by GitHub
parent e9ca4fcd65
commit fa29b4de22
2 changed files with 39 additions and 0 deletions
@@ -82,6 +82,20 @@ public class CommandCompletions<C extends CommandCompletionContext> {
return this.completionMap.put(prepareCompletionId(id), handler);
}
/**
* Unregister a completion handler.
* @param id
* @return
* @throws IllegalStateException If the completion couldn't be found
*/
public CommandCompletionHandler unregisterCompletion(String id) {
if (!this.completionMap.containsKey(id)) {
throw new IllegalStateException("The supplied key " + id + " does not exist in any completions");
}
return this.completionMap.remove(id);
}
/**
* Registr a completion handler to provide command completions based on the user input.
* This handler is declared to be safe to be executed asynchronously.
@@ -532,6 +532,31 @@ public abstract class CommandManager<
dependencies.put(clazz, key, instance);
}
/**
* Unregisters an instance of the class, it will no longer be able to be injected
*
* @param clazz the class the injector should look for to remove
* @throws IllegalStateException If the dependency was not found.
*/
public <T> void unregisterDependency(Class<? extends T> clazz) {
unregisterDependency(clazz, clazz.getName());
}
/**
* Unregisters an instance of the class, it will no longer be able to be injected
*
* @param clazz the class the injector should look for to remove
* @param key the key which needs to be present if that
* @throws IllegalStateException If the dependency was not found.
*/
public <T> void unregisterDependency(Class<? extends T> clazz, String key) {
if (!dependencies.containsKey(clazz, key)) {
throw new IllegalStateException("Unable to unregister a dependency of " + clazz.getName() + " with the key " + key + " because it wasn't registered");
}
dependencies.remove(clazz, key);
}
/**
* Attempts to inject instances of classes registered with {@link CommandManager#registerDependency(Class, Object)}