Class InjectionEnvironment

java.lang.Object
de.tomatengames.util.injector.InjectionEnvironment
All Implemented Interfaces:
AutoCloseable

public final class InjectionEnvironment extends Object implements 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 Details

    • setDefaultProvider

      public void setDefaultProvider(@NotNull InjectionProviderFunction defaultProvider)
      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

      protected @NotNull Map<Object,Object> getInjectionMap()
      Returns the map containing the provided values visible for all threads. To find a value provided for a key, you also have to check for InjectionEnvironments.
      Returns:
      The injection map.
    • close

      public void close()
      Closes the environment. After the environment is closed, values provided to the environment will no longer be visible to inject calls.
      Specified by:
      close in interface AutoCloseable
    • provide

      public void provide(@NotNull String key, @NotNull Object value) throws InjectionException
      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

      public boolean provideIfAbsent(@NotNull String key, @NotNull Object value)
      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

      public void unprovide(@NotNull String key)
      Removes the specified injection key from this provider. If the key is not provided, nothing happens.
      Parameters:
      key - The injection key.
    • provide

      public <T> void provide(@NotNull InjectionKey<T> key, @NotNull T value) throws InjectionException
      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

      public <T> boolean provideIfAbsent(@NotNull InjectionKey<T> key, @NotNull T value)
      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

      public void unprovide(@NotNull InjectionKey<?> key)
      Removes the specified injection key from this provider. If the key is not provided, nothing happens.
      Parameters:
      key - The injection key.
    • provide

      public <T> void provide(@NotNull Class<T> key, @NotNull T value) throws InjectionException
      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

      public <T> boolean provideIfAbsent(@NotNull Class<T> key, @NotNull T value)
      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

      public void unprovide(@NotNull Class<?> key)
      Removes the specified injection from this provider. If the key class is not provided, nothing happens.
      Parameters:
      key - The key class.