Restored MultiParser

This commit is contained in:
fabioCollini 2019-02-11 17:40:53 -05:00
parent 2ceb990d5d
commit 77dfdd97fa
4 changed files with 57 additions and 85 deletions

View file

@ -1,40 +1,26 @@
//package com.nytimes.android.external.store3.base.impl
//
//import com.nytimes.android.external.store3.util.KeyParser
//import com.nytimes.android.external.store3.util.ParserException
//
//import java.util.ArrayList
//
//import io.reactivex.annotations.NonNull
//
//import com.nytimes.android.external.cache3.Preconditions.checkArgument
//import com.nytimes.android.external.cache3.Preconditions.checkNotNull
//
//class MultiParser<Key, Raw, Parsed>(parsers: List<KeyParser<Key, Any, Any>>) : KeyParser<Key, Raw, Parsed> {
//
// private val parsers = ArrayList<KeyParser<*, *, *>>()
//
// init {
// this.parsers.addAll(parsers)
// }
//
// private fun createParserException(): ParserException {
// return ParserException("One of the provided parsers has a wrong typing. " + "Make sure that parsers are passed in a correct order and the fromTypes match each other.")
// }
//
// @NonNull
// @Throws(ParserException::class)
// override
// suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed {
// var parsed: Any = raw!!
// for (parser in parsers) {
// try {
// parsed = parser.apply(key, parsed)!!
// } catch (exception: ClassCastException) {
// throw createParserException()
// }
//
// }
// return parsed as Parsed
// }
//}
package com.nytimes.android.external.store3.base.impl
import com.nytimes.android.external.store3.util.KeyParser
import com.nytimes.android.external.store3.util.ParserException
import io.reactivex.annotations.NonNull
class MultiParser<Key, Raw, Parsed>(private val parsers: List<KeyParser<Any?, Any?, Any?>>) : KeyParser<Key, Raw, Parsed> {
private fun createParserException(): ParserException {
return ParserException("One of the provided parsers has a wrong typing. " +
"Make sure that parsers are passed in a correct order and the fromTypes match each other.")
}
@NonNull
override suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed {
var parsed: Any = raw!!
for (parser in parsers) {
try {
parsed = parser.apply(key, parsed)!!
} catch (exception: ClassCastException) {
throw createParserException()
}
}
return parsed as Parsed
}
}

View file

@ -6,13 +6,14 @@ import com.nytimes.android.external.store3.util.KeyParser
import com.nytimes.android.external.store3.util.NoKeyParser
import com.nytimes.android.external.store3.util.NoopParserFunc
import com.nytimes.android.external.store3.util.NoopPersister
import java.util.*
/**
* Builder where there parser is used.
*/
class RealStoreBuilder<Raw, Parsed, Key> {
private var parser: KeyParser<Key, Raw, Parsed>? = null
private val parsers = ArrayList<KeyParser<Any?, Any?, Any?>>()
private var persister: Persister<Raw, Key>? = null
private var fetcher: Fetcher<Raw, Key>? = null
private var memoryPolicy: MemoryPolicy? = null
@ -20,27 +21,24 @@ class RealStoreBuilder<Raw, Parsed, Key> {
private//remove when it is implemented...
var stalePolicy = StalePolicy.UNSPECIFIED
fun fetcher(fetcher: Fetcher<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> {
fun fetcher(fetcher: Fetcher<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.fetcher = fetcher
return this
}
fun fetcher(fetcher: suspend (Key) -> Raw): RealStoreBuilder<Raw, Parsed, Key> {
fun fetcher(fetcher: suspend (Key) -> Raw): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.fetcher = object : Fetcher<Raw, Key> {
override suspend fun fetch(key: Key): Raw {
return fetcher(key)
}
}
return this
}
fun persister(persister: Persister<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> {
fun persister(persister: Persister<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.persister = persister
return this
}
fun persister(diskRead: DiskRead<Raw, Key>,
diskWrite: DiskWrite<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> {
diskWrite: DiskWrite<Raw, Key>): RealStoreBuilder<Raw, Parsed, Key> = apply {
persister = object : Persister<Raw, Key> {
override suspend fun read(key: Key): Raw? =
diskRead.read(key)
@ -48,55 +46,44 @@ class RealStoreBuilder<Raw, Parsed, Key> {
override suspend fun write(key: Key, raw: Raw): Boolean =
diskWrite.write(key, raw)
}
return this
}
fun parser(parser: Parser<Raw, Parsed>): RealStoreBuilder<Raw, Parsed, Key> {
this.parser = NoKeyParser(parser)
return this
fun parser(parser: Parser<Raw, Parsed>): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.parsers.clear()
this.parsers.add(NoKeyParser(parser as Parser<Any?, Any?>))
}
fun parser(parser: suspend (Raw) -> Parsed): RealStoreBuilder<Raw, Parsed, Key> {
this.parser = NoKeyParser(object : Parser<Raw, Parsed> {
override suspend fun apply(raw: Raw): Parsed {
return parser(raw)
}
})
return this
fun parser(parser: suspend (Raw) -> Parsed): RealStoreBuilder<Raw, Parsed, Key> =
parser(NoKeyParser(object : Parser<Raw, Parsed> {
override suspend fun apply(raw: Raw): Parsed {
return parser(raw)
}
}))
fun parser(parser: KeyParser<Key, Raw, Parsed>): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.parsers.clear()
this.parsers.add(parser as KeyParser<Any?, Any?, Any?>)
}
fun parser(parser: KeyParser<Key, Raw, Parsed>): RealStoreBuilder<Raw, Parsed, Key> {
this.parser = parser
return this
fun parsers(parsers: List<Parser<*, *>>): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.parsers.clear()
this.parsers.addAll(parsers.map { NoKeyParser<Any?, Any?, Any?>(it as Parser<Any?, Any?>) })
}
fun parsers(parsers: List<Parser<Raw, Parsed>>): RealStoreBuilder<Raw, Parsed, Key> {
TODO("not implemented")
// this.parsers.clear()
// for (parser in parsers) {
// this.parsers.add(NoKeyParser<Key,Raw, Parsed>(parser))
// }
// return this
}
fun memoryPolicy(memoryPolicy: MemoryPolicy): RealStoreBuilder<Raw, Parsed, Key> {
fun memoryPolicy(memoryPolicy: MemoryPolicy): RealStoreBuilder<Raw, Parsed, Key> = apply {
this.memoryPolicy = memoryPolicy
return this
}
//Store will backfill the disk cache anytime a record is stale
//User will still get the stale record returned to them
fun refreshOnStale(): RealStoreBuilder<Raw, Parsed, Key> {
fun refreshOnStale(): RealStoreBuilder<Raw, Parsed, Key> = apply {
stalePolicy = StalePolicy.REFRESH_ON_STALE
return this
}
//Store will try to get network source when disk data is stale
//if network source throws error or is empty, stale disk data will be returned
fun networkBeforeStale(): RealStoreBuilder<Raw, Parsed, Key> {
fun networkBeforeStale(): RealStoreBuilder<Raw, Parsed, Key> = apply {
stalePolicy = StalePolicy.NETWORK_BEFORE_STALE
return this
}
fun open(): Store<Parsed, Key> {
@ -104,15 +91,15 @@ class RealStoreBuilder<Raw, Parsed, Key> {
persister = NoopPersister.create(memoryPolicy)
}
if (parser == null) {
if (parsers.isEmpty()) {
parser(NoopParserFunc())
}
// val multiParser = MultiParser<Key, Raw, Parsed>(parsers)
val multiParser = MultiParser<Key, Raw, Parsed>(parsers)
val realInternalStore: InternalStore<Parsed, Key> = RealInternalStore(fetcher!!, persister!!, parser!!, memoryPolicy, stalePolicy)
val realInternalStore: RealInternalStore<Raw, Parsed, Key> = RealInternalStore(fetcher!!, persister!!, multiParser, memoryPolicy, stalePolicy)
return RealStore(realInternalStore)
return RealStore<Parsed, Key>(realInternalStore)
}
companion object {

View file

@ -1,9 +1,8 @@
package com.nytimes.android.external.store3.util
import io.reactivex.annotations.NonNull
import io.reactivex.functions.BiFunction
interface KeyParser<Key, Raw, Parsed> {
interface KeyParser<in Key, in Raw, out Parsed> {
@Throws(ParserException::class)
suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed

View file

@ -4,7 +4,7 @@ import com.nytimes.android.external.store3.base.Parser
import io.reactivex.annotations.NonNull
class NoKeyParser<Key, Raw, Parsed>(private val parser: Parser<Raw, Parsed>) : KeyParser<Key, Raw, Parsed> {
class NoKeyParser<in Key, in Raw, out Parsed>(private val parser: Parser<Raw, out Parsed>) : KeyParser<Key, Raw, Parsed> {
@Throws(ParserException::class)
override suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed {