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 b2e16dc5b..dc9a4e5e6 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,9 +43,11 @@ public class DependencyInjector implements IDepe protected final boolean allowDuplicateBindings; + //==============// // constructors // //==============// + //region public DependencyInjector(Class newBindableInterface, boolean newAllowDuplicateBindings) { @@ -53,11 +55,14 @@ public class DependencyInjector implements IDepe this.allowDuplicateBindings = newAllowDuplicateBindings; } + //endregion + //=========// // binding // //=========// + //region @Override public void bind(Class dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException @@ -131,13 +136,27 @@ public class DependencyInjector implements IDepe @Override public boolean checkIfClassExtends(Class classToTest, Class extensionToLookFor) { return extensionToLookFor.isAssignableFrom(classToTest); } + //endregion + //===========// // unbinding // //===========// + //region + + public void replaceBinding(Class dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException + { + this.unbindAll(dependencyInterface); + this.bind(dependencyInterface, dependencyImplementation); + } + + public void unbindAll(Class dependencyInterface) throws IllegalStateException, IllegalArgumentException + { + // remove the dependency if present + this.dependencies.remove(dependencyInterface); + } - // 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 @@ -174,30 +193,27 @@ public class DependencyInjector implements IDepe this.dependencies.remove(dependencyInterface); } + //endregion + //=========// // getters // //=========// + //region @SuppressWarnings("unchecked") @Override public T get(Class interfaceClass) throws ClassCastException - { - return (T) this.getInternalLogic(interfaceClass, false).get(0); - } + { return (T) this.getInternalLogic(interfaceClass, false).get(0); } @Override public ArrayList getAll(Class interfaceClass) throws ClassCastException - { - return this.getInternalLogic(interfaceClass, false); - } + { return this.getInternalLogic(interfaceClass, false); } @Override public T get(Class interfaceClass, boolean allowIncompleteDependencies) throws ClassCastException - { - return (T) this.getInternalLogic(interfaceClass, allowIncompleteDependencies).get(0); - } + { return (T) this.getInternalLogic(interfaceClass, allowIncompleteDependencies).get(0); } /** * Always returns a list of size 1 or greater, @@ -230,6 +246,7 @@ public class DependencyInjector implements IDepe return emptyList; } + //endregion /** Removes all bound dependencies. */