reformat DependencyInjector and ApiEventInjector

This commit is contained in:
James Seibel
2022-11-24 22:32:12 -06:00
parent 2b1837e812
commit 840b02c2db
2 changed files with 42 additions and 46 deletions
@@ -37,11 +37,12 @@ import java.util.HashMap;
*/
public class ApiEventInjector extends DependencyInjector<IDhApiEvent> 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<Class<? extends IDhApiEvent>, 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<Class<? extends IDhApiEvent>, Object> firedOneTimeEventParamsByEventInterface = new HashMap<>();
private ApiEventInjector() { super(IDhApiEvent.class, true); }
@@ -49,38 +50,38 @@ public class ApiEventInjector extends DependencyInjector<IDhApiEvent> implements
@Override
public void bind(Class<? extends IDhApiEvent> eventInterface, IDhApiEvent eventImplementation) throws IllegalStateException, IllegalArgumentException
public void bind(Class<? extends IDhApiEvent> 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<? extends IDhApiEvent> eventInterface, Class<? extends IDhApiEvent> eventClassToRemove) throws IllegalArgumentException
public boolean unbind(Class<? extends IDhApiEvent> abstractEvent, Class<? extends IDhApiEvent> 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<IDhApiEvent> implements
// actually remove the dependency
if (this.dependencies.containsKey(eventInterface))
if (this.dependencies.containsKey(abstractEvent))
{
ArrayList<IDhApiEvent> dependencyList = this.dependencies.get(eventInterface);
ArrayList<IDhApiEvent> 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<IDhApiEvent> implements
}
@Override
public <T, U extends IDhApiEvent<T>> boolean fireAllEvents(Class<U> eventInterface, T eventParameterObject)
public <T, U extends IDhApiEvent<T>> boolean fireAllEvents(Class<U> 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<U> eventList = this.getAll(eventInterface);
ArrayList<U> eventList = this.getAll(abstractEvent);
for (IDhApiEvent<T> event : eventList)
{
if (event != null)
@@ -140,7 +143,7 @@ public class ApiEventInjector extends DependencyInjector<IDhApiEvent> 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);
}
}
}
@@ -31,7 +31,7 @@ import java.util.Map;
*
* @param <BindableType> 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<BindableType extends IBindable> implements IDependencyInjector<BindableType>
{
@@ -62,16 +62,16 @@ public class DependencyInjector<BindableType extends IBindable> implements IDepe
public void bind(Class<? extends BindableType> 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<BindableType extends IBindable> 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<BindableType>());
this.dependencies.put(dependencyInterface, new ArrayList<BindableType>());
}
// 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<BindableType extends IBindable> 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<BindableType extends IBindable> 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<BindableType extends IBindable> 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 extends BindableType> T get(Class<T> interfaceClass) throws ClassCastException
{
return (T) getInternalLogic(interfaceClass, false).get(0);
return (T) this.getInternalLogic(interfaceClass, false).get(0);
}
@Override
public <T extends BindableType> ArrayList<T> getAll(Class<T> interfaceClass) throws ClassCastException
{
return getInternalLogic(interfaceClass, false);
return this.getInternalLogic(interfaceClass, false);
}
@SuppressWarnings("unchecked")
@Override
public <T extends BindableType> T get(Class<T> 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<BindableType extends IBindable> implements IDepe
@SuppressWarnings("unchecked")
private <T extends BindableType> ArrayList<T> getInternalLogic(Class<T> interfaceClass, boolean allowIncompleteDependencies) throws ClassCastException
{
ArrayList<BindableType> dependencyList = dependencies.get(interfaceClass);
ArrayList<BindableType> 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<BindableType extends IBindable> 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<BindableType extends IBindable> implements IDepe
@Override
public void runDelayedSetup()
{
for (Class<? extends BindableType> interfaceKey : dependencies.keySet())
for (Class<? extends BindableType> interfaceKey : this.dependencies.keySet())
{
IBindable concreteObject = get(interfaceKey, true);
IBindable concreteObject = this.get(interfaceKey, true);
if (!concreteObject.getDelayedSetupComplete())
{
concreteObject.finishDelayedSetup();