Class DependencyInjector<BindableType extends IBindable>
java.lang.Object
com.seibel.distanthorizons.coreapi.DependencyInjection.DependencyInjector<BindableType>
- Type Parameters:
BindableType- extends IBindable and defines what interfaces this dependency handler can deal with.
- All Implemented Interfaces:
IDependencyInjector<BindableType>
- Direct Known Subclasses:
ApiEventInjector
public class DependencyInjector<BindableType extends IBindable>
extends Object
implements IDependencyInjector<BindableType>
This class takes care of tracking objects used in dependency injection.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanprotected final Class<? extends BindableType> Internal class reference to BindableType since we can't get it any other way.protected final Map<Class<? extends BindableType>, ArrayList<BindableType>> -
Constructor Summary
ConstructorsConstructorDescriptionDependencyInjector(Class<BindableType> newBindableInterface) DependencyInjector(Class<BindableType> newBindableInterface, boolean newAllowDuplicateBindings) -
Method Summary
Modifier and TypeMethodDescriptionvoidbind(Class<? extends BindableType> dependencyInterface, BindableType dependencyImplementation) Links the given implementation object to an interface, so it can be referenced later.booleancheckIfClassExtends(Class<?> classToTest, Class<?> extensionToLookFor) Checks if classToTest extends the given class.booleancheckIfClassImplements(Class<?> classToTest, Class<?> interfaceToLookFor) Checks if classToTest (or one of its ancestors) implements the given interface.voidclear()Removes all bound dependencies.<T extends BindableType>
TDoes not return incomplete dependencies.<T extends BindableType>
TReturns a dependency of type T if one has been bound.<T extends BindableType>
ArrayList<T> Returns all dependencies of type T that have been bound.voidRuns delayed setup for any dependencies that require it.
-
Field Details
-
dependencies
protected final Map<Class<? extends BindableType extends IBindable>,ArrayList<BindableType extends IBindable>> dependencies -
bindableInterface
Internal class reference to BindableType since we can't get it any other way. -
allowDuplicateBindings
protected final boolean allowDuplicateBindings
-
-
Constructor Details
-
DependencyInjector
-
DependencyInjector
public DependencyInjector(Class<BindableType> newBindableInterface, boolean newAllowDuplicateBindings)
-
-
Method Details
-
bind
public void bind(Class<? extends BindableType> dependencyInterface, BindableType dependencyImplementation) throws IllegalStateException, IllegalArgumentException Description copied from interface:IDependencyInjectorLinks the given implementation object to an interface, so it can be referenced later.- Specified by:
bindin interfaceIDependencyInjector<BindableType extends IBindable>- Parameters:
dependencyInterface- The interface (or parent class) the implementation object should implement.dependencyImplementation- An object that implements the dependencyInterface interface.- Throws:
IllegalStateException- if the interface has already been bound and duplicates aren't allowedIllegalArgumentException- if the implementation object doesn't implement the interface
-
checkIfClassImplements
Description copied from interface:IDependencyInjectorChecks if classToTest (or one of its ancestors) implements the given interface.- Specified by:
checkIfClassImplementsin interfaceIDependencyInjector<BindableType extends IBindable>
-
checkIfClassExtends
Description copied from interface:IDependencyInjectorChecks if classToTest extends the given class.- Specified by:
checkIfClassExtendsin interfaceIDependencyInjector<BindableType extends IBindable>
-
get
Description copied from interface:IDependencyInjectorDoes not return incomplete dependencies.
Seeget(Class, boolean)for full documentation.- Specified by:
getin interfaceIDependencyInjector<BindableType extends IBindable>- Throws:
ClassCastException- See Also:
-
getAll
public <T extends BindableType> ArrayList<T> getAll(Class<T> interfaceClass) throws ClassCastException Description copied from interface:IDependencyInjectorReturns all dependencies of type T that have been bound.
Returns an empty list if no dependencies have been bound.- Specified by:
getAllin interfaceIDependencyInjector<BindableType extends IBindable>- Type Parameters:
T- class of the dependency (inferred from the objectClass parameter)- Parameters:
interfaceClass- Interface of the dependency- Returns:
- the dependency of type T
- Throws:
ClassCastException- If the dependency isn't able to be cast to type T. (this shouldn't normally happen, unless the bound object changed somehow)
-
get
public <T extends BindableType> T get(Class<T> interfaceClass, boolean allowIncompleteDependencies) throws ClassCastException Description copied from interface:IDependencyInjectorReturns a dependency of type T if one has been bound.
Returns null if a dependency hasn't been bound.
If the handler allows duplicate bindings, this will return the first bound dependency.- Specified by:
getin interfaceIDependencyInjector<BindableType extends IBindable>- Type Parameters:
T- class of the dependency (inferred from the interfaceClass parameter)- Parameters:
interfaceClass- Interface of the dependencyallowIncompleteDependencies- If true this method will also return dependencies that haven't completed their delayed setup.- Returns:
- the dependency of type T
- Throws:
ClassCastException- If the dependency isn't able to be cast to type T. (this shouldn't normally happen, unless the bound object changed somehow)
-
clear
public void clear()Removes all bound dependencies.- Specified by:
clearin interfaceIDependencyInjector<BindableType extends IBindable>
-
runDelayedSetup
public void runDelayedSetup()Runs delayed setup for any dependencies that require it.- Specified by:
runDelayedSetupin interfaceIDependencyInjector<BindableType extends IBindable>
-