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 boolean
protected 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 TypeMethodDescriptionvoid
bind
(Class<? extends BindableType> dependencyInterface, BindableType dependencyImplementation) Links the given implementation object to an interface, so it can be referenced later.boolean
checkIfClassExtends
(Class<?> classToTest, Class<?> extensionToLookFor) Checks if classToTest extends the given class.boolean
checkIfClassImplements
(Class<?> classToTest, Class<?> interfaceToLookFor) Checks if classToTest (or one of its ancestors) implements the given interface.void
clear()
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.void
Runs 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:IDependencyInjector
Links the given implementation object to an interface, so it can be referenced later.- Specified by:
bind
in 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:IDependencyInjector
Checks if classToTest (or one of its ancestors) implements the given interface.- Specified by:
checkIfClassImplements
in interfaceIDependencyInjector<BindableType extends IBindable>
-
checkIfClassExtends
Description copied from interface:IDependencyInjector
Checks if classToTest extends the given class.- Specified by:
checkIfClassExtends
in interfaceIDependencyInjector<BindableType extends IBindable>
-
get
Description copied from interface:IDependencyInjector
Does not return incomplete dependencies.
Seeget(Class, boolean)
for full documentation.- Specified by:
get
in 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:IDependencyInjector
Returns all dependencies of type T that have been bound.
Returns an empty list if no dependencies have been bound.- Specified by:
getAll
in 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:IDependencyInjector
Returns 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:
get
in 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:
clear
in interfaceIDependencyInjector<BindableType extends IBindable>
-
runDelayedSetup
public void runDelayedSetup()Runs delayed setup for any dependencies that require it.- Specified by:
runDelayedSetup
in interfaceIDependencyInjector<BindableType extends IBindable>
-