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

com.justai.jaicf.helpers.kotlin.PropertyWithBackingField.kt Maven / Gradle / Ivy

package com.justai.jaicf.helpers.kotlin

import com.justai.jaicf.helpers.java.WeakIdentityHashMap
import kotlin.reflect.KProperty

class PropertyWithBackingField(
    val initializer: (R) -> T = { throw IllegalStateException("Not initialized.") }
) {
    private val map = WeakIdentityHashMap()

    operator fun getValue(thisRef: R, property: KProperty<*>): T =
        map[thisRef] ?: setValue(thisRef, property, initializer(thisRef))

    operator fun setValue(thisRef: R, property: KProperty<*>, value: T): T {
        map[thisRef] = value
        return value
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy