diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt index b8926d597..4ef39459d 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt @@ -2,6 +2,7 @@ package org.mobilenativefoundation.store.cache5 +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi import org.mobilenativefoundation.store.core5.KeyProvider import org.mobilenativefoundation.store.core5.StoreData import org.mobilenativefoundation.store.core5.StoreKey @@ -12,29 +13,30 @@ import org.mobilenativefoundation.store.core5.StoreKey * Depends on [StoreMultiCacheAccessor] for internal data management. * @see [Cache]. */ -class StoreMultiCache, Single : StoreData.Single, Collection : StoreData.Collection, Output : StoreData>( - private val keyProvider: KeyProvider, - singlesCache: Cache, Single> = CacheBuilder, Single>().build(), - collectionsCache: Cache, Collection> = CacheBuilder, Collection>().build(), -) : Cache { +@ExperimentalStoreApi +class StoreMultiCache, S : StoreData.Single, C : StoreData.Collection, O : StoreData>( + private val keyProvider: KeyProvider, + singlesCache: Cache, S> = CacheBuilder, S>().build(), + collectionsCache: Cache, C> = CacheBuilder, C>().build(), +) : Cache { private val accessor = StoreMultiCacheAccessor( singlesCache = singlesCache, collectionsCache = collectionsCache, ) - private fun Key.castSingle() = this as StoreKey.Single + private fun K.castSingle() = this as StoreKey.Single - private fun Key.castCollection() = this as StoreKey.Collection + private fun K.castCollection() = this as StoreKey.Collection - private fun StoreKey.Collection.cast() = this as Key + private fun StoreKey.Collection.cast() = this as K - private fun StoreKey.Single.cast() = this as Key + private fun StoreKey.Single.cast() = this as K - override fun getIfPresent(key: Key): Output? { + override fun getIfPresent(key: K): O? { return when (key) { - is StoreKey.Single<*> -> accessor.getSingle(key.castSingle()) as? Output - is StoreKey.Collection<*> -> accessor.getCollection(key.castCollection()) as? Output + is StoreKey.Single<*> -> accessor.getSingle(key.castSingle()) as? O + is StoreKey.Collection<*> -> accessor.getCollection(key.castCollection()) as? O else -> { throw UnsupportedOperationException(invalidKeyErrorMessage(key)) } @@ -42,12 +44,12 @@ class StoreMultiCache, Single : StoreData.Single Output, - ): Output { + key: K, + valueProducer: () -> O, + ): O { return when (key) { is StoreKey.Single<*> -> { - val single = accessor.getSingle(key.castSingle()) as? Output + val single = accessor.getSingle(key.castSingle()) as? O if (single != null) { single } else { @@ -58,7 +60,7 @@ class StoreMultiCache, Single : StoreData.Single -> { - val collection = accessor.getCollection(key.castCollection()) as? Output + val collection = accessor.getCollection(key.castCollection()) as? O if (collection != null) { collection } else { @@ -74,18 +76,18 @@ class StoreMultiCache, Single : StoreData.Single): Map { - val map = mutableMapOf() + override fun getAllPresent(keys: List<*>): Map { + val map = mutableMapOf() keys.filterIsInstance>().forEach { key -> when (key) { is StoreKey.Collection -> { val collection = accessor.getCollection(key) - collection?.let { map[key.cast()] = it as Output } + collection?.let { map[key.cast()] = it as O } } is StoreKey.Single -> { val single = accessor.getSingle(key) - single?.let { map[key.cast()] = it as Output } + single?.let { map[key.cast()] = it as O } } } } @@ -93,28 +95,28 @@ class StoreMultiCache, Single : StoreData.Single) { + override fun invalidateAll(keys: List) { keys.forEach { key -> invalidate(key) } } - override fun invalidate(key: Key) { + override fun invalidate(key: K) { when (key) { is StoreKey.Single<*> -> accessor.invalidateSingle(key.castSingle()) is StoreKey.Collection<*> -> accessor.invalidateCollection(key.castCollection()) } } - override fun putAll(map: Map) { + override fun putAll(map: Map) { map.entries.forEach { (key, value) -> put(key, value) } } override fun put( - key: Key, - value: Output, + key: K, + value: O, ) { when (key) { is StoreKey.Single<*> -> { - val single = value as Single + val single = value as S accessor.putSingle(key.castSingle(), single) val collectionKey = keyProvider.fromSingle(key.castSingle(), single) @@ -128,17 +130,17 @@ class StoreMultiCache, Single : StoreData.Single -> { - val collection = value as Collection + val collection = value as C accessor.putCollection(key.castCollection(), collection) collection.items.forEach { - val single = it as? Single + val single = it as? S if (single != null) { accessor.putSingle(keyProvider.fromCollection(key.castCollection(), single), single) } diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/clock.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/ClockExtensions.kt similarity index 100% rename from store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/clock.kt rename to store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/ClockExtensions.kt diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/StoreExtensions.kt similarity index 100% rename from store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt rename to store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/StoreExtensions.kt