22
33package org.mobilenativefoundation.store.cache5
44
5+ import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
56import org.mobilenativefoundation.store.core5.KeyProvider
67import org.mobilenativefoundation.store.core5.StoreData
78import org.mobilenativefoundation.store.core5.StoreKey
@@ -12,42 +13,43 @@ import org.mobilenativefoundation.store.core5.StoreKey
1213 * Depends on [StoreMultiCacheAccessor] for internal data management.
1314 * @see [Cache].
1415 */
15- class StoreMultiCache <Id : Any , Key : StoreKey <Id >, Single : StoreData .Single <Id >, Collection : StoreData .Collection <Id , Single >, Output : StoreData <Id >>(
16- private val keyProvider : KeyProvider <Id , Single >,
17- singlesCache : Cache <StoreKey .Single <Id >, Single > = CacheBuilder <StoreKey .Single <Id >, Single >().build(),
18- collectionsCache : Cache <StoreKey .Collection <Id >, Collection > = CacheBuilder <StoreKey .Collection <Id >, Collection >().build(),
19- ) : Cache<Key, Output> {
16+ @ExperimentalStoreApi
17+ class StoreMultiCache <Id : Any , K : StoreKey <Id >, S : StoreData .Single <Id >, C : StoreData .Collection <Id , S >, O : StoreData <Id >>(
18+ private val keyProvider : KeyProvider <Id , S >,
19+ singlesCache : Cache <StoreKey .Single <Id >, S > = CacheBuilder <StoreKey .Single <Id >, S >().build(),
20+ collectionsCache : Cache <StoreKey .Collection <Id >, C > = CacheBuilder <StoreKey .Collection <Id >, C >().build(),
21+ ) : Cache<K, O> {
2022 private val accessor =
2123 StoreMultiCacheAccessor (
2224 singlesCache = singlesCache,
2325 collectionsCache = collectionsCache,
2426 )
2527
26- private fun Key .castSingle () = this as StoreKey .Single <Id >
28+ private fun K .castSingle () = this as StoreKey .Single <Id >
2729
28- private fun Key .castCollection () = this as StoreKey .Collection <Id >
30+ private fun K .castCollection () = this as StoreKey .Collection <Id >
2931
30- private fun StoreKey.Collection<Id>.cast () = this as Key
32+ private fun StoreKey.Collection<Id>.cast () = this as K
3133
32- private fun StoreKey.Single<Id>.cast () = this as Key
34+ private fun StoreKey.Single<Id>.cast () = this as K
3335
34- override fun getIfPresent (key : Key ): Output ? {
36+ override fun getIfPresent (key : K ): O ? {
3537 return when (key) {
36- is StoreKey .Single <* > -> accessor.getSingle(key.castSingle()) as ? Output
37- is StoreKey .Collection <* > -> accessor.getCollection(key.castCollection()) as ? Output
38+ is StoreKey .Single <* > -> accessor.getSingle(key.castSingle()) as ? O
39+ is StoreKey .Collection <* > -> accessor.getCollection(key.castCollection()) as ? O
3840 else -> {
3941 throw UnsupportedOperationException (invalidKeyErrorMessage(key))
4042 }
4143 }
4244 }
4345
4446 override fun getOrPut (
45- key : Key ,
46- valueProducer : () -> Output ,
47- ): Output {
47+ key : K ,
48+ valueProducer : () -> O ,
49+ ): O {
4850 return when (key) {
4951 is StoreKey .Single <* > -> {
50- val single = accessor.getSingle(key.castSingle()) as ? Output
52+ val single = accessor.getSingle(key.castSingle()) as ? O
5153 if (single != null ) {
5254 single
5355 } else {
@@ -58,7 +60,7 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
5860 }
5961
6062 is StoreKey .Collection <* > -> {
61- val collection = accessor.getCollection(key.castCollection()) as ? Output
63+ val collection = accessor.getCollection(key.castCollection()) as ? O
6264 if (collection != null ) {
6365 collection
6466 } else {
@@ -74,47 +76,47 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
7476 }
7577 }
7678
77- override fun getAllPresent (keys : List <* >): Map <Key , Output > {
78- val map = mutableMapOf<Key , Output >()
79+ override fun getAllPresent (keys : List <* >): Map <K , O > {
80+ val map = mutableMapOf<K , O >()
7981 keys.filterIsInstance<StoreKey <Id >>().forEach { key ->
8082 when (key) {
8183 is StoreKey .Collection <Id > -> {
8284 val collection = accessor.getCollection(key)
83- collection?.let { map[key.cast()] = it as Output }
85+ collection?.let { map[key.cast()] = it as O }
8486 }
8587
8688 is StoreKey .Single <Id > -> {
8789 val single = accessor.getSingle(key)
88- single?.let { map[key.cast()] = it as Output }
90+ single?.let { map[key.cast()] = it as O }
8991 }
9092 }
9193 }
9294
9395 return map
9496 }
9597
96- override fun invalidateAll (keys : List <Key >) {
98+ override fun invalidateAll (keys : List <K >) {
9799 keys.forEach { key -> invalidate(key) }
98100 }
99101
100- override fun invalidate (key : Key ) {
102+ override fun invalidate (key : K ) {
101103 when (key) {
102104 is StoreKey .Single <* > -> accessor.invalidateSingle(key.castSingle())
103105 is StoreKey .Collection <* > -> accessor.invalidateCollection(key.castCollection())
104106 }
105107 }
106108
107- override fun putAll (map : Map <Key , Output >) {
109+ override fun putAll (map : Map <K , O >) {
108110 map.entries.forEach { (key, value) -> put(key, value) }
109111 }
110112
111113 override fun put (
112- key : Key ,
113- value : Output ,
114+ key : K ,
115+ value : O ,
114116 ) {
115117 when (key) {
116118 is StoreKey .Single <* > -> {
117- val single = value as Single
119+ val single = value as S
118120 accessor.putSingle(key.castSingle(), single)
119121
120122 val collectionKey = keyProvider.fromSingle(key.castSingle(), single)
@@ -128,17 +130,17 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
128130 it
129131 }
130132 }
131- val updatedCollection = existingCollection.copyWith(items = updatedItems) as Collection
133+ val updatedCollection = existingCollection.copyWith(items = updatedItems) as C
132134 accessor.putCollection(collectionKey, updatedCollection)
133135 }
134136 }
135137
136138 is StoreKey .Collection <* > -> {
137- val collection = value as Collection
139+ val collection = value as C
138140 accessor.putCollection(key.castCollection(), collection)
139141
140142 collection.items.forEach {
141- val single = it as ? Single
143+ val single = it as ? S
142144 if (single != null ) {
143145 accessor.putSingle(keyProvider.fromCollection(key.castCollection(), single), single)
144146 }
0 commit comments