Skip to content

Commit

Permalink
Clean Up (#499)
Browse files Browse the repository at this point in the history
* Remove "Representation" from generics

Signed-off-by: mramotar <[email protected]>

* Remove "Representation" from generics

Signed-off-by: mramotar <[email protected]>

* Rename to Validator

Signed-off-by: mramotar <[email protected]>

* Add logo!

Signed-off-by: mramotar <[email protected]>

* Update README.md

Signed-off-by: mramotar <[email protected]>

* Format

Signed-off-by: mramotar <[email protected]>

* Update CI

Signed-off-by: Matt <[email protected]>

* Remove "Representation" from generics

Signed-off-by: mramotar <[email protected]>

* Remove "Representation" from generics

Signed-off-by: mramotar <[email protected]>

* Rename to Validator

Signed-off-by: mramotar <[email protected]>

* Add logo!

Signed-off-by: mramotar <[email protected]>

* Update README.md

Signed-off-by: mramotar <[email protected]>

* Format

Signed-off-by: mramotar <[email protected]>

* Rename generics

Signed-off-by: mramotar <[email protected]>

* Rename input to value

Signed-off-by: mramotar <[email protected]>

* Update README

Signed-off-by: mramotar <[email protected]>

* Update README

Signed-off-by: mramotar <[email protected]>

Signed-off-by: mramotar <[email protected]>
Signed-off-by: Matt <[email protected]>
  • Loading branch information
matt-ramotar authored and aclassen committed Jan 16, 2023
1 parent 855a7bb commit ae07e06
Show file tree
Hide file tree
Showing 45 changed files with 450 additions and 694 deletions.
Binary file added Images/friendly_robot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
375 changes: 68 additions & 307 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.mobilenativefoundation.store.cache5

import kotlin.time.Duration

class CacheBuilder<Key : Any, CommonRepresentation : Any> {
class CacheBuilder<Key : Any, Output : Any> {
internal var concurrencyLevel = 4
private set
internal val initialCapacity = 16
Expand All @@ -14,41 +14,41 @@ class CacheBuilder<Key : Any, CommonRepresentation : Any> {
private set
internal var expireAfterWrite: Duration = Duration.INFINITE
private set
internal var weigher: Weigher<Key, CommonRepresentation>? = null
internal var weigher: Weigher<Key, Output>? = null
private set
internal var ticker: Ticker? = null
private set

fun concurrencyLevel(producer: () -> Int): CacheBuilder<Key, CommonRepresentation> = apply {
fun concurrencyLevel(producer: () -> Int): CacheBuilder<Key, Output> = apply {
concurrencyLevel = producer.invoke()
}

fun maximumSize(maximumSize: Long): CacheBuilder<Key, CommonRepresentation> = apply {
fun maximumSize(maximumSize: Long): CacheBuilder<Key, Output> = apply {
if (maximumSize < 0) {
throw IllegalArgumentException("Maximum size must be non-negative.")
}
this.maximumSize = maximumSize
}

fun expireAfterAccess(duration: Duration): CacheBuilder<Key, CommonRepresentation> = apply {
fun expireAfterAccess(duration: Duration): CacheBuilder<Key, Output> = apply {
if (duration.isNegative()) {
throw IllegalArgumentException("Duration must be non-negative.")
}
expireAfterAccess = duration
}

fun expireAfterWrite(duration: Duration): CacheBuilder<Key, CommonRepresentation> = apply {
fun expireAfterWrite(duration: Duration): CacheBuilder<Key, Output> = apply {
if (duration.isNegative()) {
throw IllegalArgumentException("Duration must be non-negative.")
}
expireAfterWrite = duration
}

fun ticker(ticker: Ticker): CacheBuilder<Key, CommonRepresentation> = apply {
fun ticker(ticker: Ticker): CacheBuilder<Key, Output> = apply {
this.ticker = ticker
}

fun weigher(maximumWeight: Long, weigher: Weigher<Key, CommonRepresentation>): CacheBuilder<Key, CommonRepresentation> = apply {
fun weigher(maximumWeight: Long, weigher: Weigher<Key, Output>): CacheBuilder<Key, Output> = apply {
if (maximumWeight < 0) {
throw IllegalArgumentException("Maximum weight must be non-negative.")
}
Expand All @@ -57,7 +57,7 @@ class CacheBuilder<Key : Any, CommonRepresentation : Any> {
this.weigher = weigher
}

fun build(): Cache<Key, CommonRepresentation> {
fun build(): Cache<Key, Output> {
if (maximumSize != -1L && weigher != null) {
throw IllegalStateException("Maximum size cannot be combined with weigher.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.mobilenativefoundation.store.store5

interface Converter<Network : Any, Output : Any, Local : Any> {
fun fromNetworkToOutput(network: Network): Output?
fun fromOutputToLocal(common: Output): Local?
fun fromLocalToOutput(sourceOfTruth: Local): Output?

class Builder<Network : Any, Output : Any, Local : Any> {

private var fromOutputToLocal: ((value: Output) -> Local)? = null
private var fromNetworkToOutput: ((value: Network) -> Output)? = null
private var fromLocalToOutput: ((value: Local) -> Output)? = null

fun build(): Converter<Network, Output, Local> =
RealConverter(fromOutputToLocal, fromNetworkToOutput, fromLocalToOutput)

fun fromOutputToLocal(converter: (value: Output) -> Local): Builder<Network, Output, Local> {
fromOutputToLocal = converter
return this
}

fun fromLocalToOutput(converter: (value: Local) -> Output): Builder<Network, Output, Local> {
fromLocalToOutput = converter
return this
}

fun fromNetworkToOutput(converter: (value: Network) -> Output): Builder<Network, Output, Local> {
fromNetworkToOutput = converter
return this
}
}
}

private class RealConverter<Network : Any, Output : Any, Local : Any>(
private val fromOutputToLocal: ((value: Output) -> Local)?,
private val fromNetworkToOutput: ((value: Network) -> Output)?,
private val fromLocalToOutput: ((value: Local) -> Output)?,
) : Converter<Network, Output, Local> {
override fun fromNetworkToOutput(network: Network): Output? =
fromNetworkToOutput?.invoke(network)

override fun fromOutputToLocal(common: Output): Local? =
fromOutputToLocal?.invoke(common)

override fun fromLocalToOutput(sourceOfTruth: Local): Output? =
fromLocalToOutput?.invoke(sourceOfTruth)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import org.mobilenativefoundation.store.store5.Fetcher.Companion.ofResult
* See [ofFlow], [of] for easily translating to [FetcherResult] (and
* automatically transforming exceptions into [FetcherResult.Error].
*/
interface Fetcher<Key : Any, NetworkRepresentation : Any> {
interface Fetcher<Key : Any, Network : Any> {
/**
* Returns a flow of the item represented by the given [key].
*/
operator fun invoke(key: Key): Flow<FetcherResult<NetworkRepresentation>>
operator fun invoke(key: Key): Flow<FetcherResult<Network>>

companion object {
/**
Expand All @@ -37,9 +37,9 @@ interface Fetcher<Key : Any, NetworkRepresentation : Any> {
*
* @param flowFactory a factory for a [Flow]ing source of network records.
*/
fun <Key : Any, NetworkRepresentation : Any> ofResultFlow(
flowFactory: (Key) -> Flow<FetcherResult<NetworkRepresentation>>
): Fetcher<Key, NetworkRepresentation> = FactoryFetcher(factory = flowFactory)
fun <Key : Any, Network : Any> ofResultFlow(
flowFactory: (Key) -> Flow<FetcherResult<Network>>
): Fetcher<Key, Network> = FactoryFetcher(factory = flowFactory)

/**
* "Creates" a [Fetcher] from a non-[Flow] source.
Expand All @@ -52,9 +52,9 @@ interface Fetcher<Key : Any, NetworkRepresentation : Any> {
*
* @param fetch a source of network records.
*/
fun <Key : Any, NetworkRepresentation : Any> ofResult(
fetch: suspend (Key) -> FetcherResult<NetworkRepresentation>
): Fetcher<Key, NetworkRepresentation> = ofResultFlow(fetch.asFlow())
fun <Key : Any, Network : Any> ofResult(
fetch: suspend (Key) -> FetcherResult<Network>
): Fetcher<Key, Network> = ofResultFlow(fetch.asFlow())

/**
* "Creates" a [Fetcher] from a [flowFactory] and translate the results to a [FetcherResult].
Expand All @@ -68,11 +68,11 @@ interface Fetcher<Key : Any, NetworkRepresentation : Any> {
*
* @param flowFactory a factory for a [Flow]ing source of network records.
*/
fun <Key : Any, NetworkRepresentation : Any> ofFlow(
flowFactory: (Key) -> Flow<NetworkRepresentation>
): Fetcher<Key, NetworkRepresentation> = FactoryFetcher { key: Key ->
fun <Key : Any, Network : Any> ofFlow(
flowFactory: (Key) -> Flow<Network>
): Fetcher<Key, Network> = FactoryFetcher { key: Key ->
flowFactory(key)
.map<NetworkRepresentation, FetcherResult<NetworkRepresentation>> { FetcherResult.Data(it) }
.map<Network, FetcherResult<Network>> { FetcherResult.Data(it) }
.catch { throwable: Throwable -> emit(FetcherResult.Error.Exception(throwable)) }
}

Expand All @@ -87,19 +87,19 @@ interface Fetcher<Key : Any, NetworkRepresentation : Any> {
*
* @param fetch a source of network records.
*/
fun <Key : Any, NetworkRepresentation : Any> of(fetch: suspend (key: Key) -> NetworkRepresentation): Fetcher<Key, NetworkRepresentation> =
fun <Key : Any, Network : Any> of(fetch: suspend (key: Key) -> Network): Fetcher<Key, Network> =
ofFlow(fetch.asFlow())

private fun <Key : Any, NetworkRepresentation : Any> (suspend (key: Key) -> NetworkRepresentation).asFlow() = { key: Key ->
private fun <Key : Any, Network : Any> (suspend (key: Key) -> Network).asFlow() = { key: Key ->
flow {
emit(invoke(key))
}
}

private class FactoryFetcher<Key : Any, NetworkRepresentation : Any>(
private val factory: (Key) -> Flow<FetcherResult<NetworkRepresentation>>
) : Fetcher<Key, NetworkRepresentation> {
override fun invoke(key: Key): Flow<FetcherResult<NetworkRepresentation>> = factory(key)
private class FactoryFetcher<Key : Any, Network : Any>(
private val factory: (Key) -> Flow<FetcherResult<Network>>
) : Fetcher<Key, Network> {
override fun invoke(key: Key): Flow<FetcherResult<Network>> = factory(key)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.mobilenativefoundation.store.store5

sealed class FetcherResult<out NetworkRepresentation : Any> {
data class Data<NetworkRepresentation : Any>(val value: NetworkRepresentation) : FetcherResult<NetworkRepresentation>()
sealed class FetcherResult<out Network : Any> {
data class Data<Network : Any>(val value: Network) : FetcherResult<Network>()
sealed class Error : FetcherResult<Nothing>() {
data class Exception(val error: Throwable) : Error()
data class Message(val message: String) : Error()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.mobilenativefoundation.store.store5

interface MutableStore<Key : Any, CommonRepresentation : Any> :
Read.StreamWithConflictResolution<Key, CommonRepresentation>,
Write<Key, CommonRepresentation>,
Write.Stream<Key, CommonRepresentation>,
interface MutableStore<Key : Any, Output : Any> :
Read.StreamWithConflictResolution<Key, Output>,
Write<Key, Output>,
Write.Stream<Key, Output>,
Clear.Key<Key>,
Clear
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.mobilenativefoundation.store.store5

data class OnFetcherCompletion<NetworkRepresentation : Any>(
val onSuccess: (FetcherResult.Data<NetworkRepresentation>) -> Unit,
data class OnFetcherCompletion<Network : Any>(
val onSuccess: (FetcherResult.Data<Network>) -> Unit,
val onFailure: (FetcherResult.Error) -> Unit
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.mobilenativefoundation.store.store5

data class OnUpdaterCompletion<NetworkWriteResponse : Any>(
data class OnUpdaterCompletion<Response : Any>(
val onSuccess: (UpdaterResult.Success) -> Unit,
val onFailure: (UpdaterResult.Error) -> Unit
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package org.mobilenativefoundation.store.store5
import kotlinx.coroutines.flow.Flow

interface Read {
interface Stream<Key : Any, CommonRepresentation : Any> {
interface Stream<Key : Any, Output : Any> {
/**
* Return a flow for the given key
* @param request - see [StoreReadRequest] for configurations
*/
fun stream(request: StoreReadRequest<Key>): Flow<StoreReadResponse<CommonRepresentation>>
fun stream(request: StoreReadRequest<Key>): Flow<StoreReadResponse<Output>>
}

interface StreamWithConflictResolution<Key : Any, CommonRepresentation : Any> {
fun <NetworkWriteResponse : Any> stream(request: StoreReadRequest<Key>): Flow<StoreReadResponse<CommonRepresentation>>
interface StreamWithConflictResolution<Key : Any, Output : Any> {
fun <Response : Any> stream(request: StoreReadRequest<Key>): Flow<StoreReadResponse<Output>>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ import kotlin.jvm.JvmName
* transform them to another type when placing them in local storage.
*
*/
interface SourceOfTruth<Key : Any, SourceOfTruthRepresentation : Any> {
interface SourceOfTruth<Key : Any, Local : Any> {

/**
* Used by [Store] to read records from the source of truth.
*
* @param key The key to read for.
*/
fun reader(key: Key): Flow<SourceOfTruthRepresentation?>
fun reader(key: Key): Flow<Local?>

/**
* Used by [Store] to write records **coming in from the fetcher (network)** to the source of
Expand All @@ -66,7 +66,7 @@ interface SourceOfTruth<Key : Any, SourceOfTruthRepresentation : Any> {
*
* @param key The key to update for.
*/
suspend fun write(key: Key, value: SourceOfTruthRepresentation)
suspend fun write(key: Key, value: Local)

/**
* Used by [Store] to delete records in the source of truth for the given key.
Expand All @@ -90,12 +90,12 @@ interface SourceOfTruth<Key : Any, SourceOfTruthRepresentation : Any> {
* @param delete function for deleting records in the source of truth for the given key
* @param deleteAll function for deleting all records in the source of truth
*/
fun <Key : Any, SourceOfTruthRepresentation : Any> of(
nonFlowReader: suspend (Key) -> SourceOfTruthRepresentation?,
writer: suspend (Key, SourceOfTruthRepresentation) -> Unit,
fun <Key : Any, Local : Any> of(
nonFlowReader: suspend (Key) -> Local?,
writer: suspend (Key, Local) -> Unit,
delete: (suspend (Key) -> Unit)? = null,
deleteAll: (suspend () -> Unit)? = null
): SourceOfTruth<Key, SourceOfTruthRepresentation> = PersistentNonFlowingSourceOfTruth(
): SourceOfTruth<Key, Local> = PersistentNonFlowingSourceOfTruth(
realReader = nonFlowReader,
realWriter = writer,
realDelete = delete,
Expand All @@ -112,12 +112,12 @@ interface SourceOfTruth<Key : Any, SourceOfTruthRepresentation : Any> {
* @param deleteAll function for deleting all records in the source of truth
*/
@JvmName("ofFlow")
fun <Key : Any, SourceOfTruthRepresentation : Any> of(
reader: (Key) -> Flow<SourceOfTruthRepresentation?>,
writer: suspend (Key, SourceOfTruthRepresentation) -> Unit,
fun <Key : Any, Local : Any> of(
reader: (Key) -> Flow<Local?>,
writer: suspend (Key, Local) -> Unit,
delete: (suspend (Key) -> Unit)? = null,
deleteAll: (suspend () -> Unit)? = null
): SourceOfTruth<Key, SourceOfTruthRepresentation> = PersistentSourceOfTruth(
): SourceOfTruth<Key, Local> = PersistentSourceOfTruth(
realReader = reader,
realWriter = writer,
realDelete = delete,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package org.mobilenativefoundation.store.store5
* }
*
*/
interface Store<Key : Any, CommonRepresentation : Any> :
Read.Stream<Key, CommonRepresentation>,
interface Store<Key : Any, Output : Any> :
Read.Stream<Key, Output>,
Clear.Key<Key>,
Clear.All
Loading

0 comments on commit ae07e06

Please sign in to comment.