package com.lemonhc.mplus.framework.util.kotlinExtension

import java.util.*
import kotlin.reflect.KProperty

//geter와 seter가 구현되지 않은 항목에 강제로 구현
//ex) var View.isChecked: Boolean by FieldProperty { false }
class FieldProperty<R, T : Any>(
        val initializer: (R) -> T = { throw IllegalStateException("Not initialized.") }
) {

    private val map = WeakHashMap<R, T>()

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