Skip to content

Commit

Permalink
Add filterSome and filterNone extensions for Flowable, Maybe and Sing…
Browse files Browse the repository at this point in the history
…le. (#12)
  • Loading branch information
ihar-abramovich authored and ming13 committed Jul 19, 2017
1 parent bb449fe commit 1b7b6f0
Show file tree
Hide file tree
Showing 2 changed files with 394 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.gojuno.koptional.rxjava2
import com.gojuno.koptional.None
import com.gojuno.koptional.Optional
import com.gojuno.koptional.Some
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single

private inline fun <reified R : Any> Observable<*>.ofType(): Observable<R> = ofType(R::class.java)
private inline fun <reified R : Any> Flowable<*>.ofType(): Flowable<R> = ofType(R::class.java)
private inline fun <reified R : Any> Maybe<*>.ofType(): Maybe<R> = ofType(R::class.java)
private inline fun <reified R : Any> Single<*>.ofType(): Maybe<R> = filter { it is R }.cast(R::class.java)

/**
* Filters items emitted by an ObservableSource by only emitting those that are `Some`.
Expand All @@ -21,6 +27,48 @@ private inline fun <reified R : Any> Observable<*>.ofType(): Observable<R> = ofT
*/
fun <T : Any> Observable<Optional<T>>.filterSome(): Observable<T> = ofType<Some<T>>().map { it.value }

/**
* Filters items emitted by a Publisher by only emitting those that are `Some`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Flowable that emits `Some.value` of those items emitted by the source Publisher that are `Some`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Flowable<Optional<T>>.filterSome(): Flowable<T> = ofType<Some<T>>().map { it.value }

/**
* Filters items emitted by an MaybeSource by only emitting those that are `Some`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Maybe that emits `Some.value` of those items emitted by the source MaybeSource that are `Some`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Maybe<Optional<T>>.filterSome(): Maybe<T> = ofType<Some<T>>().map { it.value }

/**
* Filters items emitted by an SingleSource by only emitting those that are `Some`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Maybe that emits `Some.value` of those items emitted by the source SingleSource that are `Some`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Single<Optional<T>>.filterSome(): Maybe<T> = ofType<Some<T>>().map { it.value }

/**
* Filters items emitted by an ObservableSource by only emitting those that are `None`.
* <p>
Expand All @@ -34,3 +82,45 @@ fun <T : Any> Observable<Optional<T>>.filterSome(): Observable<T> = ofType<Some<
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Observable<Optional<T>>.filterNone(): Observable<Unit> = ofType<None>().map { Unit }

/**
* Filters items emitted by an Publisher by only emitting those that are `None`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Flowable that emits `Unit` for each item emitted by the source Publisher that is `None`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Flowable<Optional<T>>.filterNone(): Flowable<Unit> = ofType<None>().map { Unit }

/**
* Filters items emitted by an MaybeSource by only emitting those that are `None`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Maybe that emits `Unit` for each item emitted by the source MaybeSource that is `None`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Maybe<Optional<T>>.filterNone(): Maybe<Unit> = ofType<None>().map { Unit }

/**
* Filters items emitted by an SingleSource by only emitting those that are `None`.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code filter} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Maybe that emits `Unit` for each item emitted by the source SingleSource that is `None`.
* @see <a href="http://reactivex.io/documentation/operators/filter.html">ReactiveX operators documentation: Filter</a>
*/
fun <T : Any> Single<Optional<T>>.filterNone(): Maybe<Unit> = ofType<None>().map { Unit }
Loading

0 comments on commit 1b7b6f0

Please sign in to comment.