@@ -883,6 +883,95 @@ trait Observable[+T]
883
883
}))
884
884
}
885
885
886
+ /**
887
+ * Returns an Observable that applies a function to each item emitted or notification raised by the source
888
+ * Observable and then flattens the Observables returned from these functions and emits the resulting items.
889
+ *
890
+ * <img width="640" height="410" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeMap.nce.png">
891
+ *
892
+ * @tparam R the result type
893
+ * @param onNext a function that returns an Observable to merge for each item emitted by the source Observable
894
+ * @param onError a function that returns an Observable to merge for an onError notification from the source
895
+ * Observable
896
+ * @param onCompleted a function that returns an Observable to merge for an onCompleted notification from the source
897
+ * Observable
898
+ * @return an Observable that emits the results of merging the Observables returned from applying the
899
+ * specified functions to the emissions and notifications of the source Observable
900
+ */
901
+ def flatMap [R ](onNext : T => Observable [R ], onError : Throwable => Observable [R ], onCompleted : () => Observable [R ]): Observable [R ] = {
902
+ val jOnNext = new Func1 [T , rx.Observable [_ <: R ]] {
903
+ override def call (t : T ): rx.Observable [_ <: R ] = onNext(t).asJavaObservable
904
+ }
905
+ val jOnError = new Func1 [Throwable , rx.Observable [_ <: R ]] {
906
+ override def call (e : Throwable ): rx.Observable [_ <: R ] = onError(e).asJavaObservable
907
+ }
908
+ val jOnCompleted = new Func0 [rx.Observable [_ <: R ]] {
909
+ override def call (): rx.Observable [_ <: R ] = onCompleted().asJavaObservable
910
+ }
911
+ toScalaObservable[R ](asJavaObservable.mergeMap[R ](jOnNext, jOnError, jOnCompleted))
912
+ }
913
+
914
+ /**
915
+ * Returns an Observable that emits the results of a specified function to the pair of values emitted by the
916
+ * source Observable and a specified collection Observable.
917
+ *
918
+ * <img width="640" height="390" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeMap.r.png">
919
+ *
920
+ * @tparam U the type of items emitted by the collection Observable
921
+ * @tparam R the type of items emitted by the resulting Observable
922
+ * @param collectionSelector a function that returns an Observable for each item emitted by the source Observable
923
+ * @param resultSelector a function that combines one item emitted by each of the source and collection Observables and
924
+ * returns an item to be emitted by the resulting Observable
925
+ * @return an Observable that emits the results of applying a function to a pair of values emitted by the
926
+ * source Observable and the collection Observable
927
+ */
928
+ def flatMap [U , R ](collectionSelector : T => Observable [U ], resultSelector : (T , U ) => R ): Observable [R ] = {
929
+ val jCollectionSelector = new Func1 [T , rx.Observable [_ <: U ]] {
930
+ override def call (t : T ): rx.Observable [_ <: U ] = collectionSelector(t).asJavaObservable
931
+ }
932
+ toScalaObservable[R ](asJavaObservable.mergeMap[U , R ](jCollectionSelector, resultSelector))
933
+ }
934
+
935
+ /**
936
+ * Returns an Observable that merges each item emitted by the source Observable with the values in an
937
+ * Iterable corresponding to that item that is generated by a selector.
938
+ *
939
+ * <img width="640" height="310" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeMapIterable.png">
940
+ *
941
+ * @tparam R the type of item emitted by the resulting Observable
942
+ * @param collectionSelector a function that returns an Iterable sequence of values for when given an item emitted by the
943
+ * source Observable
944
+ * @return an Observable that emits the results of merging the items emitted by the source Observable with
945
+ * the values in the Iterables corresponding to those items, as generated by `collectionSelector
946
+ */
947
+ def flatMapIterable [R ](collectionSelector : T => Iterable [R ]): Observable [R ] = {
948
+ val jCollectionSelector = new Func1 [T , java.lang.Iterable [_ <: R ]] {
949
+ override def call (t : T ): java.lang.Iterable [_ <: R ] = collectionSelector(t).asJava
950
+ }
951
+ toScalaObservable[R ](asJavaObservable.mergeMapIterable[R ](jCollectionSelector))
952
+ }
953
+
954
+ /**
955
+ * Returns an Observable that emits the results of applying a function to the pair of values from the source
956
+ * Observable and an Iterable corresponding to that item that is generated by a selector.
957
+ *
958
+ * <img width="640" height="390" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeMapIterable.r.png">
959
+ *
960
+ * @tparam U the collection element type
961
+ * @tparam R the type of item emited by the resulting Observable
962
+ * @param collectionSelector a function that returns an Iterable sequence of values for each item emitted by the source
963
+ * Observable
964
+ * @param resultSelector a function that returns an item based on the item emitted by the source Observable and the
965
+ * Iterable returned for that item by the `collectionSelector`
966
+ * @return an Observable that emits the items returned by `resultSelector` for each item in the source Observable
967
+ */
968
+ def flatMapIterable [U , R ](collectionSelector : T => Iterable [U ], resultSelector : (T , U ) => R ): Observable [R ] = {
969
+ val jCollectionSelector = new Func1 [T , java.lang.Iterable [_ <: U ]] {
970
+ override def call (t : T ): java.lang.Iterable [_ <: U ] = collectionSelector(t).asJava
971
+ }
972
+ toScalaObservable[R ](asJavaObservable.mergeMapIterable[U , R ](jCollectionSelector, resultSelector))
973
+ }
974
+
886
975
/**
887
976
* Returns an Observable that applies the given function to each item emitted by an
888
977
* Observable and emits the result.
0 commit comments