Add support for nullable types with Flowable

This commit is contained in:
Moez Bhatti
2017-12-28 03:03:00 -05:00
parent 0e6ccc5401
commit 43bd7db1d1
2 changed files with 14 additions and 0 deletions
@@ -0,0 +1,5 @@
package com.moez.QKSMS.common.util
data class Optional<out T>(val value: T?) {
fun notNull() = value != null
}
@@ -0,0 +1,9 @@
package com.moez.QKSMS.common.util.extensions
import com.moez.QKSMS.common.util.Optional
import io.reactivex.Flowable
fun <T, R> Flowable<T>.mapNotNull(mapper: (T) -> R?): Flowable<R>
= map { input -> Optional(mapper(input)) }
.filter { optional -> optional.notNull() }
.map { optional -> optional.value }