From 77dfdd97faf2efd87987a4a9da6db7feb3f8d350 Mon Sep 17 00:00:00 2001 From: fabioCollini Date: Mon, 11 Feb 2019 17:40:53 -0500 Subject: [PATCH] Restored MultiParser --- .../external/store3/base/impl/MultiParser.kt | 66 +++++++---------- .../store3/base/impl/RealStoreBuilder.kt | 71 ++++++++----------- .../android/external/store3/util/KeyParser.kt | 3 +- .../external/store3/util/NoKeyParser.kt | 2 +- 4 files changed, 57 insertions(+), 85 deletions(-) diff --git a/store/src/main/java/com/nytimes/android/external/store3/base/impl/MultiParser.kt b/store/src/main/java/com/nytimes/android/external/store3/base/impl/MultiParser.kt index 7af6116..e41d3c2 100644 --- a/store/src/main/java/com/nytimes/android/external/store3/base/impl/MultiParser.kt +++ b/store/src/main/java/com/nytimes/android/external/store3/base/impl/MultiParser.kt @@ -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(parsers: List>) : KeyParser { -// -// private val parsers = ArrayList>() -// -// 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(private val parsers: List>) : KeyParser { + + 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 + } +} diff --git a/store/src/main/java/com/nytimes/android/external/store3/base/impl/RealStoreBuilder.kt b/store/src/main/java/com/nytimes/android/external/store3/base/impl/RealStoreBuilder.kt index 14807be..adf7976 100644 --- a/store/src/main/java/com/nytimes/android/external/store3/base/impl/RealStoreBuilder.kt +++ b/store/src/main/java/com/nytimes/android/external/store3/base/impl/RealStoreBuilder.kt @@ -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 { - private var parser: KeyParser? = null + private val parsers = ArrayList>() private var persister: Persister? = null private var fetcher: Fetcher? = null private var memoryPolicy: MemoryPolicy? = null @@ -20,27 +21,24 @@ class RealStoreBuilder { private//remove when it is implemented... var stalePolicy = StalePolicy.UNSPECIFIED - fun fetcher(fetcher: Fetcher): RealStoreBuilder { + fun fetcher(fetcher: Fetcher): RealStoreBuilder = apply { this.fetcher = fetcher - return this } - fun fetcher(fetcher: suspend (Key) -> Raw): RealStoreBuilder { + fun fetcher(fetcher: suspend (Key) -> Raw): RealStoreBuilder = apply { this.fetcher = object : Fetcher { override suspend fun fetch(key: Key): Raw { return fetcher(key) } } - return this } - fun persister(persister: Persister): RealStoreBuilder { + fun persister(persister: Persister): RealStoreBuilder = apply { this.persister = persister - return this } fun persister(diskRead: DiskRead, - diskWrite: DiskWrite): RealStoreBuilder { + diskWrite: DiskWrite): RealStoreBuilder = apply { persister = object : Persister { override suspend fun read(key: Key): Raw? = diskRead.read(key) @@ -48,55 +46,44 @@ class RealStoreBuilder { override suspend fun write(key: Key, raw: Raw): Boolean = diskWrite.write(key, raw) } - return this } - fun parser(parser: Parser): RealStoreBuilder { - this.parser = NoKeyParser(parser) - return this + fun parser(parser: Parser): RealStoreBuilder = apply { + this.parsers.clear() + this.parsers.add(NoKeyParser(parser as Parser)) } - fun parser(parser: suspend (Raw) -> Parsed): RealStoreBuilder { - this.parser = NoKeyParser(object : Parser { - override suspend fun apply(raw: Raw): Parsed { - return parser(raw) - } - }) - return this + fun parser(parser: suspend (Raw) -> Parsed): RealStoreBuilder = + parser(NoKeyParser(object : Parser { + override suspend fun apply(raw: Raw): Parsed { + return parser(raw) + } + })) + + fun parser(parser: KeyParser): RealStoreBuilder = apply { + this.parsers.clear() + this.parsers.add(parser as KeyParser) } - fun parser(parser: KeyParser): RealStoreBuilder { - this.parser = parser - - return this + fun parsers(parsers: List>): RealStoreBuilder = apply { + this.parsers.clear() + this.parsers.addAll(parsers.map { NoKeyParser(it as Parser) }) } - fun parsers(parsers: List>): RealStoreBuilder { - TODO("not implemented") -// this.parsers.clear() -// for (parser in parsers) { -// this.parsers.add(NoKeyParser(parser)) -// } -// return this - } - - fun memoryPolicy(memoryPolicy: MemoryPolicy): RealStoreBuilder { + fun memoryPolicy(memoryPolicy: MemoryPolicy): RealStoreBuilder = 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 { + fun refreshOnStale(): RealStoreBuilder = 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 { + fun networkBeforeStale(): RealStoreBuilder = apply { stalePolicy = StalePolicy.NETWORK_BEFORE_STALE - return this } fun open(): Store { @@ -104,15 +91,15 @@ class RealStoreBuilder { persister = NoopPersister.create(memoryPolicy) } - if (parser == null) { + if (parsers.isEmpty()) { parser(NoopParserFunc()) } -// val multiParser = MultiParser(parsers) + val multiParser = MultiParser(parsers) - val realInternalStore: InternalStore = RealInternalStore(fetcher!!, persister!!, parser!!, memoryPolicy, stalePolicy) + val realInternalStore: RealInternalStore = RealInternalStore(fetcher!!, persister!!, multiParser, memoryPolicy, stalePolicy) - return RealStore(realInternalStore) + return RealStore(realInternalStore) } companion object { diff --git a/store/src/main/java/com/nytimes/android/external/store3/util/KeyParser.kt b/store/src/main/java/com/nytimes/android/external/store3/util/KeyParser.kt index 477233e..5909e60 100644 --- a/store/src/main/java/com/nytimes/android/external/store3/util/KeyParser.kt +++ b/store/src/main/java/com/nytimes/android/external/store3/util/KeyParser.kt @@ -1,9 +1,8 @@ package com.nytimes.android.external.store3.util import io.reactivex.annotations.NonNull -import io.reactivex.functions.BiFunction -interface KeyParser { +interface KeyParser { @Throws(ParserException::class) suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed diff --git a/store/src/main/java/com/nytimes/android/external/store3/util/NoKeyParser.kt b/store/src/main/java/com/nytimes/android/external/store3/util/NoKeyParser.kt index 496237a..3754840 100644 --- a/store/src/main/java/com/nytimes/android/external/store3/util/NoKeyParser.kt +++ b/store/src/main/java/com/nytimes/android/external/store3/util/NoKeyParser.kt @@ -4,7 +4,7 @@ import com.nytimes.android.external.store3.base.Parser import io.reactivex.annotations.NonNull -class NoKeyParser(private val parser: Parser) : KeyParser { +class NoKeyParser(private val parser: Parser) : KeyParser { @Throws(ParserException::class) override suspend fun apply(@NonNull key: Key, @NonNull raw: Raw): Parsed {