Skip to content

Commit

Permalink
Run ktlintFormat
Browse files Browse the repository at this point in the history
Signed-off-by: mramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Mar 16, 2024
1 parent 3f232e7 commit d858116
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,42 +13,43 @@ import org.mobilenativefoundation.store.core5.StoreKey
* Depends on [StoreMultiCacheAccessor] for internal data management.
* @see [Cache].
*/
class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id>, Collection : StoreData.Collection<Id, Single>, Output : StoreData<Id>>(
private val keyProvider: KeyProvider<Id, Single>,
singlesCache: Cache<StoreKey.Single<Id>, Single> = CacheBuilder<StoreKey.Single<Id>, Single>().build(),
collectionsCache: Cache<StoreKey.Collection<Id>, Collection> = CacheBuilder<StoreKey.Collection<Id>, Collection>().build(),
) : Cache<Key, Output> {
@ExperimentalStoreApi
class StoreMultiCache<Id : Any, K : StoreKey<Id>, S : StoreData.Single<Id>, C : StoreData.Collection<Id, S>, O : StoreData<Id>>(
private val keyProvider: KeyProvider<Id, S>,
singlesCache: Cache<StoreKey.Single<Id>, S> = CacheBuilder<StoreKey.Single<Id>, S>().build(),
collectionsCache: Cache<StoreKey.Collection<Id>, C> = CacheBuilder<StoreKey.Collection<Id>, C>().build(),
) : Cache<K, O> {
private val accessor =
StoreMultiCacheAccessor(
singlesCache = singlesCache,
collectionsCache = collectionsCache,
)

private fun Key.castSingle() = this as StoreKey.Single<Id>
private fun K.castSingle() = this as StoreKey.Single<Id>

private fun Key.castCollection() = this as StoreKey.Collection<Id>
private fun K.castCollection() = this as StoreKey.Collection<Id>

private fun StoreKey.Collection<Id>.cast() = this as Key
private fun StoreKey.Collection<Id>.cast() = this as K

private fun StoreKey.Single<Id>.cast() = this as Key
private fun StoreKey.Single<Id>.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))
}
}
}

override fun getOrPut(
key: Key,
valueProducer: () -> 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 {
Expand All @@ -58,7 +60,7 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
}

is StoreKey.Collection<*> -> {
val collection = accessor.getCollection(key.castCollection()) as? Output
val collection = accessor.getCollection(key.castCollection()) as? O
if (collection != null) {
collection
} else {
Expand All @@ -74,47 +76,47 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
}
}

override fun getAllPresent(keys: List<*>): Map<Key, Output> {
val map = mutableMapOf<Key, Output>()
override fun getAllPresent(keys: List<*>): Map<K, O> {
val map = mutableMapOf<K, O>()
keys.filterIsInstance<StoreKey<Id>>().forEach { key ->
when (key) {
is StoreKey.Collection<Id> -> {
val collection = accessor.getCollection(key)
collection?.let { map[key.cast()] = it as Output }
collection?.let { map[key.cast()] = it as O }
}

is StoreKey.Single<Id> -> {
val single = accessor.getSingle(key)
single?.let { map[key.cast()] = it as Output }
single?.let { map[key.cast()] = it as O }
}
}
}

return map
}

override fun invalidateAll(keys: List<Key>) {
override fun invalidateAll(keys: List<K>) {
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<Key, Output>) {
override fun putAll(map: Map<K, O>) {
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)
Expand All @@ -128,17 +130,17 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
it
}
}
val updatedCollection = existingCollection.copyWith(items = updatedItems) as Collection
val updatedCollection = existingCollection.copyWith(items = updatedItems) as C
accessor.putCollection(collectionKey, updatedCollection)
}
}

is StoreKey.Collection<*> -> {
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)
}
Expand Down

0 comments on commit d858116

Please sign in to comment.