From d67d4a1204796377040a1769175d5798ce09408b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 13:02:25 -0800 Subject: Fix kpref tests --- .../src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 6 ++++++ .../kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt index 2f8a6aa..a45d66d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt @@ -42,6 +42,12 @@ open class KPref private constructor( internal val prefMap: MutableMap> = mutableMapOf() + fun add(entry : KPrefDelegate<*>) { + if (prefMap.containsKey(entry.key)) + throw KPrefException("${entry.key} is already used elsewhere in preference $preferenceName") + prefMap[entry.key] = entry + } + fun reset() { prefMap.values.forEach { it.invalidate() } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt index d54c54d..c17d3df 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt @@ -27,13 +27,14 @@ import ca.allanwang.kau.kotlin.ILazyResettable */ interface KPrefDelegate : ILazyResettable { + val key: String operator fun setValue(any: Any, property: kotlin.reflect.KProperty<*>, t: T) } class KPrefException(message: String) : IllegalAccessException(message) class KPrefDelegateAndroid internal constructor( - private val key: String, + override val key: String, private val fallback: T, private val pref: KPref, private val prefBuilder: KPrefBuilderAndroid, @@ -50,9 +51,7 @@ class KPrefDelegateAndroid internal constructor( private val lock = this init { - if (pref.prefMap.containsKey(key)) - throw KPrefException("$key is already used elsewhere in preference ${pref.preferenceName}") - pref.prefMap[key] = this@KPrefDelegateAndroid + pref.add(this) } override fun invalidate() { @@ -79,7 +78,8 @@ class KPrefDelegateAndroid internal constructor( override fun isInitialized(): Boolean = _value !== UNINITIALIZED - override fun toString(): String = if (isInitialized()) value.toString() else "Lazy kPref $key not initialized yet." + override fun toString(): String = + if (isInitialized()) value.toString() else "Lazy kPref $key not initialized yet." override operator fun setValue(any: Any, property: kotlin.reflect.KProperty<*>, t: T) { _value = t @@ -91,7 +91,7 @@ class KPrefDelegateAndroid internal constructor( } class KPrefDelegateInMemory internal constructor( - private val key: String, + override val key: String, private val fallback: T, private val pref: KPref, private var postSetter: (value: T) -> Unit = {} @@ -104,13 +104,11 @@ class KPrefDelegateInMemory internal constructor( private val lock = this init { - if (pref.prefMap.containsKey(key)) - throw KPrefException("$key is already used elsewhere in preference ${pref.preferenceName}") - pref.prefMap[key] = this + pref.add(this) } override fun invalidate() { - // No op + _value = UNINITIALIZED } @Suppress("UNCHECKED_CAST") @@ -133,7 +131,8 @@ class KPrefDelegateInMemory internal constructor( override fun isInitialized(): Boolean = _value !== UNINITIALIZED - override fun toString(): String = if (isInitialized()) value.toString() else "Lazy kPref $key not initialized yet." + override fun toString(): String = + if (isInitialized()) value.toString() else "Lazy kPref $key not initialized yet." override operator fun setValue(any: Any, property: kotlin.reflect.KProperty<*>, t: T) { _value = t -- cgit v1.2.3