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
}
}