main.com.sceyt.chatuikit.persistence.LazyVarDelegate.kt Maven / Gradle / Ivy
package com.sceyt.chatuikit.persistence
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun lazyVar(initializer: () -> T): ReadWriteProperty {
return LazyVarDelegate(initializer)
}
class LazyVarDelegate(initializer: () -> T) : ReadWriteProperty {
private val defaultValue: T by lazy(initializer)
private var overrideValue: T? = null
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
overrideValue = value
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return overrideValue ?: defaultValue
}
}