From 840b02c2dbcfb6a4ebd2291bd8d9a97729d7ef2c Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 24 Nov 2022 22:32:12 -0600 Subject: [PATCH] reformat DependencyInjector and ApiEventInjector --- .../DependencyInjection/ApiEventInjector.java | 45 ++++++++++--------- .../DependencyInjector.java | 43 ++++++++---------- 2 files changed, 42 insertions(+), 46 deletions(-) diff --git a/api/src/main/java/com/seibel/lod/core/DependencyInjection/ApiEventInjector.java b/api/src/main/java/com/seibel/lod/core/DependencyInjection/ApiEventInjector.java index 4b25ec046..40fd168fd 100644 --- a/api/src/main/java/com/seibel/lod/core/DependencyInjection/ApiEventInjector.java +++ b/api/src/main/java/com/seibel/lod/core/DependencyInjection/ApiEventInjector.java @@ -37,11 +37,12 @@ import java.util.HashMap; */ public class ApiEventInjector extends DependencyInjector implements IDhApiEventInjector // Note to self: Don't try adding a generic type to IDhApiEvent, the consturctor won't accept it { - private static final Logger LOGGER = LogManager.getLogger(ApiEventInjector.class.getSimpleName()); - private static final HashMap, Object> FIRED_ONE_TIME_EVENT_PARAMETERS_BY_EVENT_INTERFACE = new HashMap<>(); - public static final ApiEventInjector INSTANCE = new ApiEventInjector(); + private static final Logger LOGGER = LogManager.getLogger(ApiEventInjector.class.getSimpleName()); + + private final HashMap, Object> firedOneTimeEventParamsByEventInterface = new HashMap<>(); + private ApiEventInjector() { super(IDhApiEvent.class, true); } @@ -49,38 +50,38 @@ public class ApiEventInjector extends DependencyInjector implements @Override - public void bind(Class eventInterface, IDhApiEvent eventImplementation) throws IllegalStateException, IllegalArgumentException + public void bind(Class abstractEvent, IDhApiEvent eventImplementation) throws IllegalStateException, IllegalArgumentException { // is this a one time event? - if (ApiEventDefinitionHandler.getEventDefinition(eventInterface).isOneTimeEvent) + if (ApiEventDefinitionHandler.getEventDefinition(abstractEvent).isOneTimeEvent) { // has this one time event been fired yet? - if (FIRED_ONE_TIME_EVENT_PARAMETERS_BY_EVENT_INTERFACE.containsKey(eventInterface)) + if (firedOneTimeEventParamsByEventInterface.containsKey(abstractEvent)) { // the one time event has happened, fire the handler // this has to be an unsafe cast since the hash map can't hold the generic objects - Object parameter = FIRED_ONE_TIME_EVENT_PARAMETERS_BY_EVENT_INTERFACE.get(eventInterface); + Object parameter = firedOneTimeEventParamsByEventInterface.get(abstractEvent); eventImplementation.fireEvent(parameter); } } // bind the event handler - super.bind(eventInterface, eventImplementation); + super.bind(abstractEvent, eventImplementation); } @Override - public boolean unbind(Class eventInterface, Class eventClassToRemove) throws IllegalArgumentException + public boolean unbind(Class abstractEvent, Class eventClassToRemove) throws IllegalArgumentException { // make sure the given dependency implements the necessary interfaces - boolean implementsInterface = this.checkIfClassImplements(eventClassToRemove, eventInterface) || - this.checkIfClassExtends(eventClassToRemove, eventInterface); + boolean implementsInterface = this.checkIfClassImplements(eventClassToRemove, abstractEvent) || + this.checkIfClassExtends(eventClassToRemove, abstractEvent); boolean implementsBindable = this.checkIfClassImplements(eventClassToRemove, this.bindableInterface); // display any errors if (!implementsInterface) { - throw new IllegalArgumentException("The event handler [" + eventClassToRemove.getSimpleName() + "] doesn't implement or extend: [" + eventInterface.getSimpleName() + "]."); + throw new IllegalArgumentException("The event handler [" + eventClassToRemove.getSimpleName() + "] doesn't implement or extend: [" + abstractEvent.getSimpleName() + "]."); } if (!implementsBindable) { @@ -89,9 +90,9 @@ public class ApiEventInjector extends DependencyInjector implements // actually remove the dependency - if (this.dependencies.containsKey(eventInterface)) + if (this.dependencies.containsKey(abstractEvent)) { - ArrayList dependencyList = this.dependencies.get(eventInterface); + ArrayList dependencyList = this.dependencies.get(abstractEvent); int indexToRemove = -1; for(int i = 0; i < dependencyList.size(); i++) { @@ -114,20 +115,22 @@ public class ApiEventInjector extends DependencyInjector implements } @Override - public > boolean fireAllEvents(Class eventInterface, T eventParameterObject) + public > boolean fireAllEvents(Class abstractEvent, T eventParameterObject) { boolean cancelEvent = false; - // if this is a one time event, record it - if (ApiEventDefinitionHandler.getEventDefinition(eventInterface).isOneTimeEvent && - !FIRED_ONE_TIME_EVENT_PARAMETERS_BY_EVENT_INTERFACE.containsKey(eventInterface)) + // if this is a one time event, record that it was called + if (ApiEventDefinitionHandler.getEventDefinition(abstractEvent).isOneTimeEvent && + !firedOneTimeEventParamsByEventInterface.containsKey(abstractEvent)) { - FIRED_ONE_TIME_EVENT_PARAMETERS_BY_EVENT_INTERFACE.put(eventInterface, eventParameterObject); + firedOneTimeEventParamsByEventInterface.put(abstractEvent, eventParameterObject); } + + // fire each bound event - ArrayList eventList = this.getAll(eventInterface); + ArrayList eventList = this.getAll(abstractEvent); for (IDhApiEvent event : eventList) { if (event != null) @@ -140,7 +143,7 @@ public class ApiEventInjector extends DependencyInjector implements } catch (Exception e) { - LOGGER.error("Exception thrown by event handler [" + event.getClass().getSimpleName() + "] for event type [" + eventInterface.getSimpleName() + "], error:" + e.getMessage(), e); + LOGGER.error("Exception thrown by event handler [" + event.getClass().getSimpleName() + "] for event type [" + abstractEvent.getSimpleName() + "], error:" + e.getMessage(), e); } } } diff --git a/api/src/main/java/com/seibel/lod/core/DependencyInjection/DependencyInjector.java b/api/src/main/java/com/seibel/lod/core/DependencyInjection/DependencyInjector.java index 4e13fdbfd..9a4f3b828 100644 --- a/api/src/main/java/com/seibel/lod/core/DependencyInjection/DependencyInjector.java +++ b/api/src/main/java/com/seibel/lod/core/DependencyInjection/DependencyInjector.java @@ -31,7 +31,7 @@ import java.util.Map; * * @param extends IBindable and defines what interfaces this dependency handler can deal with. * @author James Seibel - * @version 2022-8-15 + * @version 2022-11-24 */ public class DependencyInjector implements IDependencyInjector { @@ -62,16 +62,16 @@ public class DependencyInjector implements IDepe public void bind(Class dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException { // duplicate check if requested - if (dependencies.containsKey(dependencyInterface) && !this.allowDuplicateBindings) + if (this.dependencies.containsKey(dependencyInterface) && !this.allowDuplicateBindings) { throw new IllegalStateException("The dependency [" + dependencyInterface.getSimpleName() + "] has already been bound."); } // make sure the given dependency implements the necessary interfaces - boolean implementsInterface = checkIfClassImplements(dependencyImplementation.getClass(), dependencyInterface) - || checkIfClassExtends(dependencyImplementation.getClass(), dependencyInterface); - boolean implementsBindable = checkIfClassImplements(dependencyImplementation.getClass(), this.bindableInterface); + 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) @@ -85,13 +85,13 @@ public class DependencyInjector implements IDepe // make sure the hashSet has an array to hold the dependency - if (!dependencies.containsKey(dependencyInterface)) + if (!this.dependencies.containsKey(dependencyInterface)) { - dependencies.put(dependencyInterface, new ArrayList()); + this.dependencies.put(dependencyInterface, new ArrayList()); } // add the dependency - dependencies.get(dependencyInterface).add(dependencyImplementation); + this.dependencies.get(dependencyInterface).add(dependencyImplementation); } @Override public boolean checkIfClassImplements(Class classToTest, Class interfaceToLookFor) @@ -99,7 +99,7 @@ public class DependencyInjector implements IDepe // check the parent class (if applicable) if (classToTest.getSuperclass() != Object.class && classToTest.getSuperclass() != null) { - if (checkIfClassImplements(classToTest.getSuperclass(), interfaceToLookFor)) + if (this.checkIfClassImplements(classToTest.getSuperclass(), interfaceToLookFor)) { return true; } @@ -112,7 +112,7 @@ public class DependencyInjector implements IDepe // recurse to check interface parents if necessary if (implementationInterface.getInterfaces().length != 0) { - if (checkIfClassImplements(implementationInterface, interfaceToLookFor)) + if (this.checkIfClassImplements(implementationInterface, interfaceToLookFor)) { return true; } @@ -127,30 +127,26 @@ public class DependencyInjector implements IDepe return false; } @Override - public boolean checkIfClassExtends(Class classToTest, Class extensionToLookFor) - { - return extensionToLookFor.isAssignableFrom(classToTest); - } + public boolean checkIfClassExtends(Class classToTest, Class extensionToLookFor) { return extensionToLookFor.isAssignableFrom(classToTest); } @SuppressWarnings("unchecked") @Override public T get(Class interfaceClass) throws ClassCastException { - return (T) getInternalLogic(interfaceClass, false).get(0); + return (T) this.getInternalLogic(interfaceClass, false).get(0); } @Override public ArrayList getAll(Class interfaceClass) throws ClassCastException { - return getInternalLogic(interfaceClass, false); + return this.getInternalLogic(interfaceClass, false); } - @SuppressWarnings("unchecked") @Override public T get(Class interfaceClass, boolean allowIncompleteDependencies) throws ClassCastException { - return (T) getInternalLogic(interfaceClass, allowIncompleteDependencies).get(0); + return (T) this.getInternalLogic(interfaceClass, allowIncompleteDependencies).get(0); } /** @@ -160,7 +156,7 @@ public class DependencyInjector implements IDepe @SuppressWarnings("unchecked") private ArrayList getInternalLogic(Class interfaceClass, boolean allowIncompleteDependencies) throws ClassCastException { - ArrayList dependencyList = dependencies.get(interfaceClass); + ArrayList dependencyList = this.dependencies.get(interfaceClass); if (dependencyList != null && dependencyList.size() != 0) { // check if each dependencies' delayed setup has been completed @@ -188,10 +184,7 @@ public class DependencyInjector implements IDepe /** Removes all bound dependencies. */ @Override - public void clear() - { - this.dependencies.clear(); - } + public void clear() { this.dependencies.clear(); } @@ -199,9 +192,9 @@ public class DependencyInjector implements IDepe @Override public void runDelayedSetup() { - for (Class interfaceKey : dependencies.keySet()) + for (Class interfaceKey : this.dependencies.keySet()) { - IBindable concreteObject = get(interfaceKey, true); + IBindable concreteObject = this.get(interfaceKey, true); if (!concreteObject.getDelayedSetupComplete()) { concreteObject.finishDelayedSetup();