Class InjectionProvider

java.lang.Object
de.tomatengames.util.injector.InjectionProvider

public class InjectionProvider extends Object
Allows to provide values and inject them afterward.

An InjectionProvider shares the same functionality as the Injector class, but allows to create multiple instances. All calls to the Injector class are forwarded to a runtime-wide shared InjectionProvider, called the root provider.

Most applications should use the Injector class instead of this class. This class is useful if a subsystem needs to provide its own values without interfering with the main application.

Since:
1.9
  • Constructor Details

    • InjectionProvider

      public InjectionProvider()
      Creates a new InjectionProvider with no values provided.
  • Method Details

    • 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.
    • injectOptional

      public <T> @Nullable T injectOptional(@NotNull String key)
    • injectOptional

      public <T> @Nullable T injectOptional(@NotNull InjectionKey<T> key)
    • injectOptional

      public <T> @Nullable T injectOptional(@NotNull Class<T> key)
    • local

      Creates a new local InjectionEnvironment for this InjectionProvider. This environment can be used to provide local injection values.

      While the environment is open, values provided to the environment will be visible to inject calls. The environment is only open for the current thread. That means, values provided to the environment will only be visible to inject calls of the current thread. When the environment is closed, it is no longer open, i.e. values provided to the environment will no longer be visible to inject calls.

      If multiple environments are open at the same time in the same thread and provide the same key, the value of the last environment created will be used for inject calls.

      If an environment is open that provides the same key as this InjectionProvider directly, the value of the environment will be used for inject calls, effectively hiding the provider's value.

      Returns:
      The new environment.
    • inject

      default <T> @NotNull T inject(@NotNull String key) throws InjectionException
      Returns the value provided for the specified key.

      Note that the return value is auto-casted to the requested type. This is an unchecked cast. It must be ensured that the requested type is compatible with the value.

      Type Parameters:
      T - The requested type of the value. The return value is auto-casted to this type.
      Parameters:
      key - The key.
      Returns:
      The value provided for the key. If no such value exists, an InjectionException is thrown. The return value is auto-casted to the requested type.
      Throws:
      InjectionException - If the injection key was not provided.
    • inject

      default <T> @NotNull T inject(@NotNull InjectionKey<T> key) throws InjectionException
      Returns the value provided for the specified key.
      Type Parameters:
      T - The type of the value provided by the key.
      Parameters:
      key - The injection key.
      Returns:
      The value provided for the key. If no value was provided for the key, an InjectionException is thrown.
      Throws:
      InjectionException - If no value was provided for the key.
    • inject

      default <T> @NotNull T inject(@NotNull Class<T> key) throws InjectionException
      Returns the value provided for the specified key class.
      Type Parameters:
      T - The type of the class and the value provided by the key class.
      Parameters:
      key - The injection key class.
      Returns:
      The value provided for the key. If no value was provided for the key, an InjectionException is thrown.
      Throws:
      InjectionException - If no value was provided for the key class.
    • 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.