All Downloads are FREE. Search and download functionalities are using the official Maven repository.

e.ultra.kontainer.0.77.0.source-code.LazyServiceLookup.kt Maven / Gradle / Ivy

The newest version!
package de.peekandpoke.ultra.kontainer

import de.peekandpoke.ultra.common.Lookup
import kotlin.reflect.KClass

/**
 * Lazy [Lookup] implementation
 */
class LazyServiceLookup(
    /** The kontainer */
    private val kontainer: Kontainer,
    /** The context */
    private val context: InjectionContext,
    /** Map from class to function that produces a service */
    private val map: Map, (Kontainer, InjectionContext) -> T>,
) : Lookup {

    val classes: Set> = map.keys

    override fun  has(cls: KClass): Boolean = map.contains(cls)

    override fun  get(cls: KClass): X = getOrNull(cls) ?: error("There is no service of type '$cls'")

    @Suppress("UNCHECKED_CAST")
    override fun  getOrNull(cls: KClass): X? = map[cls]?.invoke(kontainer, context) as X?

    override fun all(): List {
        return classes.map { get(it) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy