From 328336bd29c9081b037366fb5a7338b1805d2046 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 2 Oct 2025 07:32:58 -0500 Subject: [PATCH] Allow unbinding Dependencies TODO replacing may be a better way to handle it --- .../DependencyInjector.java | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/DependencyInjector.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/DependencyInjector.java index 3e1da962a..b2e16dc5b 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/DependencyInjector.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/DependencyInjector.java @@ -43,11 +43,9 @@ public class DependencyInjector implements IDepe protected final boolean allowDuplicateBindings; - public DependencyInjector(Class newBindableInterface) - { - this.bindableInterface = newBindableInterface; - this.allowDuplicateBindings = false; - } + //==============// + // constructors // + //==============// public DependencyInjector(Class newBindableInterface, boolean newAllowDuplicateBindings) { @@ -57,12 +55,16 @@ public class DependencyInjector implements IDepe + //=========// + // binding // + //=========// @Override public void bind(Class dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException { // duplicate check if requested - if (this.dependencies.containsKey(dependencyInterface) && !this.allowDuplicateBindings) + if (this.dependencies.containsKey(dependencyInterface) + && !this.allowDuplicateBindings) { throw new IllegalStateException("The dependency [" + dependencyInterface.getSimpleName() + "] has already been bound."); } @@ -130,6 +132,54 @@ public class DependencyInjector implements IDepe public boolean checkIfClassExtends(Class classToTest, Class extensionToLookFor) { return extensionToLookFor.isAssignableFrom(classToTest); } + + //===========// + // unbinding // + //===========// + + // TODO having a bindOrReplace method would probably be better since it wouldn't have the possiblity of having nothing bound + public void unbind(Class dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException + { + // check if this object is bound + if (!this.dependencies.containsKey(dependencyInterface)) + { + return; + } + + + // make sure the given dependency implements the necessary interfaces + boolean implementsInterface = this.checkIfClassImplements(dependencyImplementation.getClass(), dependencyInterface) + || this.checkIfClassExtends(dependencyImplementation.getClass(), dependencyInterface); + boolean implementsBindable = this.checkIfClassImplements(dependencyImplementation.getClass(), this.bindableInterface); + + // display any errors + if (!implementsInterface) + { + throw new IllegalArgumentException("The dependency [" + dependencyImplementation.getClass().getSimpleName() + "] doesn't implement or extend: [" + dependencyInterface.getSimpleName() + "]."); + } + if (!implementsBindable) + { + throw new IllegalArgumentException("The dependency [" + dependencyImplementation.getClass().getSimpleName() + "] doesn't implement the interface: [" + IBindable.class.getSimpleName() + "]."); + } + + + // make sure the hashSet has an array to hold the dependency + if (!this.dependencies.containsKey(dependencyInterface)) + { + this.dependencies.put(dependencyInterface, new ArrayList()); + } + + // remove the dependency if present + this.dependencies.get(dependencyInterface).remove(dependencyImplementation); + this.dependencies.remove(dependencyInterface); + } + + + + //=========// + // getters // + //=========// + @SuppressWarnings("unchecked") @Override public T get(Class interfaceClass) throws ClassCastException