From 5071c496c069f2dce81391bbd0cb07fcdbc96930 Mon Sep 17 00:00:00 2001 From: KVelasco Date: Wed, 10 Jun 2020 10:56:32 -0700 Subject: [PATCH] Add coroutines module (#9) * Updating Gradle Wrapper * Adding Coroutines Module * PR feedback * PR feedback * Adding stub test class * adding test for combine extension functions * matching coroutines package name * fixing tests * fixing build * adding path fix to codecov.yml * fixing build --- README.md | 5 + codecov.yml | 1 + coroutines/api/coroutines.api | 8 + coroutines/build.gradle | 16 ++ coroutines/src/main/kotlin/Flows.kt | 141 ++++++++++++++++++ .../src/test/kotlin/CombineParametersTest.kt | 95 ++++++++++++ settings.gradle | 1 + 7 files changed, 267 insertions(+) create mode 100644 coroutines/api/coroutines.api create mode 100644 coroutines/build.gradle create mode 100644 coroutines/src/main/kotlin/Flows.kt create mode 100644 coroutines/src/test/kotlin/CombineParametersTest.kt diff --git a/README.md b/README.md index e76efad0..eab62442 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Library is organized into the following modules: | Type | Module | Artifact ID | Description | |:-------------------------------:|:----------------------------:|:---------------:|------------------------------------| | ![Kotlin](artwork/kotlin.png) | [`collections`](collections) | [`collections`] | Tools/utilities for [Collections]. | +| ![Kotlin](artwork/kotlin.png) | [`coroutines`](coroutines) | [`coroutines`] | Tools/utilities for [Coroutines]. | + All artifacts have the Maven group ID: `com.juul.tuulbox` @@ -41,3 +43,6 @@ table above) and_ `$version` _with the desired version (can be found by followin [`collections`]: https://github.com/JuulLabs/android-github-packages/packages/215143 [Collections]: https://kotlinlang.org/docs/reference/collections-overview.html +[`coroutines`]: https://github.com/JuulLabs/android-github-packages/packages/261605 +[Coroutines]: https://kotlinlang.org/docs/reference/coroutines-overview.html + diff --git a/codecov.yml b/codecov.yml index 3cba3842..d63a59b6 100644 --- a/codecov.yml +++ b/codecov.yml @@ -3,3 +3,4 @@ coverage: patch: off fixes: - "com/juul/tuulbox/collections::" + - "com/juul/tuulbox/coroutines/flow::" diff --git a/coroutines/api/coroutines.api b/coroutines/api/coroutines.api new file mode 100644 index 00000000..0c4535f1 --- /dev/null +++ b/coroutines/api/coroutines.api @@ -0,0 +1,8 @@ +public final class com/juul/tuulbox/coroutines/flow/FlowsKt { + public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function7;)Lkotlinx/coroutines/flow/Flow; + public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function8;)Lkotlinx/coroutines/flow/Flow; + public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function9;)Lkotlinx/coroutines/flow/Flow; + public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function10;)Lkotlinx/coroutines/flow/Flow; + public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function11;)Lkotlinx/coroutines/flow/Flow; +} + diff --git a/coroutines/build.gradle b/coroutines/build.gradle new file mode 100644 index 00000000..e8177d18 --- /dev/null +++ b/coroutines/build.gradle @@ -0,0 +1,16 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' + id 'org.jmailen.kotlinter' + id 'jacoco' + id 'maven-publish' +} + +apply from: rootProject.file('gradle/kotlin.gradle') +apply from: rootProject.file('gradle/jacoco.gradle') +apply from: rootProject.file('gradle/publish.gradle') + +dependencies { + api deps.kotlin.stdlib + api deps.kotlin.coroutines + testImplementation deps.kotlin.junit +} diff --git a/coroutines/src/main/kotlin/Flows.kt b/coroutines/src/main/kotlin/Flows.kt new file mode 100644 index 00000000..2454bb93 --- /dev/null +++ b/coroutines/src/main/kotlin/Flows.kt @@ -0,0 +1,141 @@ +@file:Suppress("UNCHECKED_CAST") + +package com.juul.tuulbox.coroutines.flow + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine + +/** + * Returns a [Flow] whose values are generated with [transform] function by combining + * the most recently emitted values by each flow. + */ +inline fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + crossinline transform: suspend (T1, T2, T3, T4, T5, T6) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6 + ) +} + +/** + * Returns a [Flow] whose values are generated with [transform] function by combining + * the most recently emitted values by each flow. + */ +inline fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + flow7: Flow, + crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6, + args[6] as T7 + ) +} + +/** + * Returns a [Flow] whose values are generated with [transform] function by combining + * the most recently emitted values by each flow. + */ +inline fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + flow7: Flow, + flow8: Flow, + crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6, + args[6] as T7, + args[7] as T8 + ) +} + +/** + * Returns a [Flow] whose values are generated with [transform] function by combining + * the most recently emitted values by each flow. + */ +inline fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + flow7: Flow, + flow8: Flow, + flow9: Flow, + crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8, flow9) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6, + args[6] as T7, + args[7] as T8, + args[8] as T9 + ) +} + +/** + * Returns a [Flow] whose values are generated with [transform] function by combining + * the most recently emitted values by each flow. + */ +inline fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + flow7: Flow, + flow8: Flow, + flow9: Flow, + flow10: Flow, + crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8, flow9, flow10) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6, + args[6] as T7, + args[7] as T8, + args[8] as T9, + args[9] as T10 + ) +} diff --git a/coroutines/src/test/kotlin/CombineParametersTest.kt b/coroutines/src/test/kotlin/CombineParametersTest.kt new file mode 100644 index 00000000..24114346 --- /dev/null +++ b/coroutines/src/test/kotlin/CombineParametersTest.kt @@ -0,0 +1,95 @@ +package com.juul.tuulbox.coroutines.flow + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.single +import kotlinx.coroutines.runBlocking + +class CombineParametersTest { + + @Test + fun testSixParameters() = runBlocking { + val flow = combine( + flowOf("1"), + flowOf(2), + flowOf("3"), + flowOf(4.toByte()), + flowOf(null), + flowOf("6") + ) { a, b, c, d, e, f -> + a + b + c + d + e + f + } + assertEquals("1234null6", flow.single()) + } + + @Test + fun testSevenParameters() = runBlocking { + val flow = combine( + flowOf("1"), + flowOf(2), + flowOf("3"), + flowOf(4.toByte()), + flowOf(null), + flowOf("6"), + flowOf(7) + ) { a, b, c, d, e, f, g -> + a + b + c + d + e + f + g + } + assertEquals("1234null67", flow.single()) + } + + @Test + fun testEightParameters() = runBlocking { + val flow = combine( + flowOf("1"), + flowOf(2), + flowOf("3"), + flowOf(4.toByte()), + flowOf(null), + flowOf("6"), + flowOf(7), + flowOf("8") + ) { a, b, c, d, e, f, g, h -> + a + b + c + d + e + f + g + h + } + assertEquals("1234null678", flow.single()) + } + + @Test + fun testNineParameters() = runBlocking { + val flow = combine( + flowOf("1"), + flowOf(2), + flowOf("3"), + flowOf(4.toByte()), + flowOf(null), + flowOf("6"), + flowOf(7), + flowOf("8"), + flowOf(9.toByte()) + ) { a, b, c, d, e, f, g, h, i -> + a + b + c + d + e + f + g + h + i + } + assertEquals("1234null6789", flow.single()) + } + + @Test + fun testTenParameters() = runBlocking { + val flow = combine( + flowOf("1"), + flowOf(2), + flowOf("3"), + flowOf(4.toByte()), + flowOf(null), + flowOf("6"), + flowOf(7), + flowOf("8"), + flowOf(9.toByte()), + flowOf(null) + ) { a, b, c, d, e, f, g, h, i, j -> + a + b + c + d + e + f + g + h + i + j + } + assertEquals("1234null6789null", flow.single()) + } +} diff --git a/settings.gradle b/settings.gradle index 6337cf56..16dfb232 100644 --- a/settings.gradle +++ b/settings.gradle @@ -15,3 +15,4 @@ pluginManagement { } include ':collections' +include ':coroutines'