Package guru.zoroark.tegral.di.environment

Environment classes and functions. Environments manage pools of inter-dependent and injectable components using different heuristics.

Types

Link copied to clipboard
sealed class Declaration<T : Any>

A declaration within an environment that is being built. These declarations can be:

Link copied to clipboard
typealias Declarations = Map<Identifier<*>, Declaration<*>>

A map that maps identifiers to declarations.

The general contract is:

  • For any key-value pair, key == value.identifier

  • For any key-value pair, key: Identifier of T => value: Declaration of T

Link copied to clipboard
object EmptyQualifier : Qualifier

The empty qualifier object must be used when there is no qualifier being used.

Link copied to clipboard
typealias EnvironmentComponents = Map<Identifier<*>, IdentifierResolver<*>>
Link copied to clipboard
class EnvironmentContext(val declarations: Declarations)

Data objects that contain all of the information passed to InjectionEnvironment constructors to let them initialize their data.

Link copied to clipboard

An injection scope that delegates the injection to the given environment (for inject) or its meta-environment (for meta).

Link copied to clipboard
data class FullTypeQualifier(val type: KType) : Qualifier

A qualifier specifically intended to be used for fully qualifying a type.

Link copied to clipboard
data class Identifier<T : Any>(val kclass: KClass<T>, val qualifier: Qualifier = EmptyQualifier)

Identifies an injectable component via its type and optionally via other elements called qualifiers.

Link copied to clipboard
class InjectableModule(val name: String, defs: Collection<Declaration<*>>)

A module that can be injected in environments. At their core, injectable modules are just lists of declarations that get copied over when adding this module to environments.

Link copied to clipboard
interface InjectionEnvironment

An injection environment is, in a nutshell, a container for injectable components. These injectable components can be retrieved in two ways:

Link copied to clipboard
fun interface InjectionEnvironmentKind<E : InjectionEnvironment>

An environment kind is a facility for building InjectionEnvironment in a nicer way. This should be implemented by the companion object of an InjectionEnvironment class.

Link copied to clipboard
interface InjectionScope : MetalessInjectionScope

An injection scope provides an entrypoint for components to retrieve the dependencies they need.

Link copied to clipboard
fun interface Injector<out T> : ReadOnlyProperty<Any?, T>

An injector is a read-only property delegator that has constraints on T.

Link copied to clipboard
interface MetalessInjectionScope

An injection scope without meta-related operations. Because InjectionScope.meta itself returns (in theory) an InjectionScope, people may do scope.meta.meta.meta.meta... to infinity and beyond, which does not make sense. In order to prevent this, InjectionScope.meta instead returns a MetalessInjectionScope.

Link copied to clipboard
class MixedImmutableEnvironment(context: ExtensibleEnvironmentContext, metaEnvironmentKind: InjectionEnvironmentKind<*> = EagerImmutableMetaEnvironment) : DefaultExtensibleInjectionEnvironment

An injection environment implementation with a mixed evaluation strategy.

Link copied to clipboard
class MultiQualifier(val qualifiers: Set<Qualifier>) : Qualifier

A multi-qualifier represents multiple qualifiers grouped as a set. This is useful for combining qualifiers (e.g. a named qualifier and a type qualifier).

Link copied to clipboard
data class NameQualifier(val name: String) : Qualifier

A qualifier that is based on a string. You can also use named to construct name qualifiers in a more DSL-ish approach.

Link copied to clipboard
interface Qualifier

Qualifiers are simple objects that can be used within Identifier objects to provide additional differentiators between two same-type objects or components.

Link copied to clipboard
abstract class ResolvableDeclaration<T : Any>(identifier: Identifier<T>) : Declaration<T>

A declaration that can be built into an IdentifierResolver.

Link copied to clipboard
interface ScopedContext

An object that contains a scope. For use as a receiver in lambdas (e.g. ScopedSupplier)

Link copied to clipboard
typealias ScopedSupplier<T> = ScopedContext.() -> T

A supplier of T that takes a ScopedContext as a receiver.

Link copied to clipboard
class ScopedSupplierDeclaration<T : Any>(identifier: Identifier<T>, val supplier: ScopedSupplier<T>) : Declaration<T>

A declaration within an EnvironmentContext.

Link copied to clipboard
class SimpleEnvironmentBasedScope(env: InjectionEnvironment) : InjectionScope

An injection scope that delegates the injection to the given environment, without any support for injecting from meta-environments.

Functions

Link copied to clipboard
fun <T : Any> ensureInstance(kclass: KClass<T>, obj: Any): T

Utility function - asserts that obj is an instance of kclass. Throws an exception if it is not or returns obj cast to T on success.

Link copied to clipboard
fun EnvironmentBasedScope(env: InjectionEnvironment): InjectionScope

Creates an injection scope based on the given environment. If the given environment supports meta-environments (i.e. if it is extensible), an ExtensibleEnvironmentBasedScope will be returned. Otherwise, a SimpleEnvironmentBasedScope is used.

Link copied to clipboard
inline fun <T : Any> Declarations.get(): Declaration<T>

Retrieves the declaration that corresponds to the given type parameter. This function does not check for the validity or coherence of the returned declaration.

inline fun <T : Any> InjectionEnvironment.get(qualifier: Qualifier = EmptyQualifier): T

Gets the component identified by the given type turned into an Identifier with an optional qualifier. Throws an exception if no component with this identifier exists.

Link copied to clipboard
inline fun <T : Any> InjectionEnvironment.getOrNull(qualifier: Qualifier = EmptyQualifier): T?

Gets the component identified by the given type turned into an Identifier with an optional qualifier. Returns null if no component with this identifier exists.

Link copied to clipboard
operator fun <T : Any> InjectionScope.invoke(identifier: Identifier<T>): ReadOnlyProperty<Any?, T>

Create an injector for the given identifier. See InjectionScope.inject for more information.

inline operator fun <T : Any> MetalessInjectionScope.invoke(qualifier: Qualifier = EmptyQualifier): ReadOnlyProperty<Any?, T>

Create an injector for the given class, turned to an identifier, and an optional qualifier. See InjectionScope.inject for more information.

Link copied to clipboard
fun named(name: String): NameQualifier

Creates a NameQualifier with the given name.

Link copied to clipboard
inline fun <T : Any> MetalessInjectionScope.optional(qualifier: Qualifier = EmptyQualifier): ReadOnlyProperty<Any?, T?>

Create an injector for the given class and optional qualifier. If such an identifier cannot be found within the environment, returns null (unlike other functions which throw a ComponentNotFoundException).

Link copied to clipboard
operator fun Qualifier.plus(other: Qualifier): Qualifier

Combines this qualifier with the other.

Link copied to clipboard
fun ScopedContext(scope: InjectionScope): ScopedContext

Creates a ScopedContext that contains the given scope as-is as a property.

Link copied to clipboard
inline fun <T : Any> typed(): FullTypeQualifier

Creates a full type qualifier with the given T as the type.

fun typed(type: KType): FullTypeQualifier

Creates a full type qualifier with the given KType as the type.