Class InjectionEnvironment
java.lang.Object
de.tomatengames.util.injector.InjectionEnvironment
- All Implemented Interfaces:
AutoCloseable
An InjectionEnvironment is used to provide values for keys for a single thread and limited time period.
An InjectionEnvironment bound to a specified
InjectionProvider can be created by calling InjectionProvider.local(),
or Injector.local() to create an environment that is bound to the root provider.
The provide(...) methods can be used to provide values in this environment.
These provided values can be injected using the inject(...) methods of the InjectionProvider or Injector
while the environment is not closed.
The inject(...) methods will only be able to provide the values if they are called within the same thread.
InjectionEnvironments must be closed. It is recommended to create environments in a try-with-resources statement to ensure they are closed properly.
try (InjectionEnvironment env = Injector.local()) {
env.provide(KEY, "value");
// Here, Injector.inject(KEY) returns "value"
}
// Here, Injector.inject(KEY) returns the originally provided value or nothing
- Since:
- 1.9
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the environment.Returns the map containing the provided values visible for all threads.<T> voidprovide(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key.<T> voidProvides the specified value for the specified key class.voidProvides the specified value for the specified injection key.<T> booleanprovideIfAbsent(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key if the key is not already provided.<T> booleanprovideIfAbsent(@NotNull Class<T> key, @NotNull T value) Provides the specified value for the specified key class if the key class is not already provided.booleanprovideIfAbsent(@NotNull String key, @NotNull Object value) Provides the specified value for the specified injection key if the key is not already provided.voidsetDefaultProvider(@NotNull InjectionProviderFunction defaultProvider) Sets the default provider function for this environment.voidunprovide(@NotNull InjectionKey<?> key) Removes the specified injection key from this provider.voidRemoves the specified injection from this provider.voidRemoves the specified injection key from this provider.
-
Method Details
-
setDefaultProvider
Sets the default provider function for this environment. This function will be called if no other value was provided for a given key. This function should return a value that should be provided for the given key. If this function returns null, it will be handled as if the key is not found.- Parameters:
defaultProvider- The default provider function.
-
getInjectionMap
-
close
public void close()Closes the environment. After the environment is closed, values provided to the environment will no longer be visible toinjectcalls.- Specified by:
closein interfaceAutoCloseable
-
provide
Provides the specified value for the specified injection key.- Parameters:
key- The injection key.value- The value.- Throws:
InjectionException- If the key is already provided.
-
provideIfAbsent
Provides the specified value for the specified injection key if the key is not already provided. If the key is already provided, nothing happens.- Parameters:
key- The injection key.value- The value.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
-
provide
Provides the specified value for the specified injection key.- Type Parameters:
T- The type of values providable for the key.- Parameters:
key- injection key.value- The value.- Throws:
InjectionException- If the key is already provided.
-
provideIfAbsent
Provides the specified value for the specified injection key if the key is not already provided. If the key is already provided, nothing happens.- Type Parameters:
T- The type of values providable for the key.- Parameters:
key- injection key.value- The value.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
Removes the specified injection key from this provider. If the key is not provided, nothing happens.- Parameters:
key- The injection key.
-
provide
Provides the specified value for the specified key class. The value is only provided for the exact key class specified.- Type Parameters:
T- The type of the value.- Parameters:
key- The key class.value- The value to provide.- Throws:
InjectionException- If the specified key class is already provided.
-
provideIfAbsent
Provides the specified value for the specified key class if the key class is not already provided. The value is only provided for the exact key class specified.If the key class is already provided, nothing happens.
- Type Parameters:
T- The type of the value.- Parameters:
key- The key class.value- The value to provide.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
-