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

commonMain.com.makeevrserg.mobilex.di.Lateinit.kt Maven / Gradle / Ivy

The newest version!
package com.makeevrserg.mobilex.di

/**
 * [Lateinit] is used for components which can't be initialized internally
 * For example: Velocity @inject properties, Android context or spigot plugin instance
 *
 * It can't be initialized twice and can't be accessed until initialization
 */
class Lateinit : Dependency {
    private lateinit var instance: T

    fun initialize(value: T) {
        check(!::instance.isInitialized) { "Value already initialized" }
        this.instance = value
    }

    fun initialize(factory: Factory) {
        val value = factory.create()
        initialize(value)
    }

    override val value: T
        get() {
            check(::instance.isInitialized) { "Value is not initialized yet" }
            return instance
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy