Skip to content

Commit 959065c

Browse files
Add flatMap alias to mapMany
This is to match with what Java 8 is adding to Stream so we support the duality of Iterable/Collection to Observable. http://download.java.net/lambda/b86/docs/api/java/util/stream/Stream.html#flatMap(java.util.function.Function)
1 parent 874554f commit 959065c

File tree

1 file changed

+123
-10
lines changed

1 file changed

+123
-10
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import rx.operators.OperationConcat;
4343
import rx.operators.OperationDefer;
4444
import rx.operators.OperationDematerialize;
45-
import rx.operators.OperationGroupBy;
4645
import rx.operators.OperationFilter;
4746
import rx.operators.OperationFinally;
47+
import rx.operators.OperationGroupBy;
4848
import rx.operators.OperationMap;
4949
import rx.operators.OperationMaterialize;
5050
import rx.operators.OperationMerge;
@@ -590,9 +590,11 @@ public void call(Object args) {
590590

591591
/**
592592
* Returns a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
593-
*
594-
* @param subject the subject to push source elements into.
595-
* @param <R> result type
593+
*
594+
* @param subject
595+
* the subject to push source elements into.
596+
* @param <R>
597+
* result type
596598
* @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
597599
*/
598600
public <R> ConnectableObservable<R> multicast(Subject<T, R> subject) {
@@ -1172,6 +1174,8 @@ public R call(T t1) {
11721174
* and then merges the results of that function applied to every item emitted by the original
11731175
* Observable, emitting these merged results as its own sequence.
11741176
* <p>
1177+
* Note: mapMany and flatMap are equivalent.
1178+
* <p>
11751179
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
11761180
*
11771181
* @param sequence
@@ -1186,6 +1190,7 @@ public R call(T t1) {
11861190
* @return an Observable that emits a sequence that is the result of applying the transformation
11871191
* function to each item emitted by the source Observable and merging the results of
11881192
* the Observables obtained from this transformation
1193+
* @see {@link #flatMap(Observable, Func1)}
11891194
*/
11901195
public static <T, R> Observable<R> mapMany(Observable<T> sequence, Func1<T, Observable<R>> func) {
11911196
return create(OperationMap.mapMany(sequence, func));
@@ -1351,6 +1356,62 @@ public static <T> Observable<T> finallyDo(Observable<T> source, Action0 action)
13511356
return create(OperationFinally.finallyDo(source, action));
13521357
}
13531358

1359+
/**
1360+
* Creates a new Observable sequence by applying a function that you supply to each object in the
1361+
* original Observable sequence, where that function is itself an Observable that emits objects,
1362+
* and then merges the results of that function applied to every item emitted by the original
1363+
* Observable, emitting these merged results as its own sequence.
1364+
* <p>
1365+
* Note: mapMany and flatMap are equivalent.
1366+
* <p>
1367+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
1368+
*
1369+
* @param sequence
1370+
* the source Observable
1371+
* @param func
1372+
* a function to apply to each item emitted by the source Observable, generating a
1373+
* Observable
1374+
* @param <T>
1375+
* the type emitted by the source Observable
1376+
* @param <R>
1377+
* the type emitted by the Observables emitted by <code>func</code>
1378+
* @return an Observable that emits a sequence that is the result of applying the transformation
1379+
* function to each item emitted by the source Observable and merging the results of
1380+
* the Observables obtained from this transformation
1381+
* @see {@link #mapMany(Observable, Func1)}
1382+
*/
1383+
public static <T, R> Observable<R> flatMap(Observable<T> sequence, Func1<T, Observable<R>> func) {
1384+
return mapMany(sequence, func);
1385+
}
1386+
1387+
/**
1388+
* Creates a new Observable sequence by applying a function that you supply to each object in the
1389+
* original Observable sequence, where that function is itself an Observable that emits objects,
1390+
* and then merges the results of that function applied to every item emitted by the original
1391+
* Observable, emitting these merged results as its own sequence.
1392+
* <p>
1393+
* Note: mapMany and flatMap are equivalent.
1394+
* <p>
1395+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
1396+
*
1397+
* @param sequence
1398+
* the source Observable
1399+
* @param func
1400+
* a function to apply to each item emitted by the source Observable, generating a
1401+
* Observable
1402+
* @param <T>
1403+
* the type emitted by the source Observable
1404+
* @param <R>
1405+
* the type emitted by the Observables emitted by <code>func</code>
1406+
* @return an Observable that emits a sequence that is the result of applying the transformation
1407+
* function to each item emitted by the source Observable and merging the results of
1408+
* the Observables obtained from this transformation
1409+
* @see {@link #mapMany(Observable, Func1)}
1410+
*/
1411+
public static <T, R> Observable<R> flatMap(Observable<T> sequence, final Object func) {
1412+
return mapMany(sequence, func);
1413+
}
1414+
13541415
/**
13551416
* Groups the elements of an observable and selects the resulting elements by using a specified function.
13561417
*
@@ -2088,11 +2149,15 @@ public static <T> Iterable<T> mostRecent(Observable<T> source, T initialValue) {
20882149

20892150
/**
20902151
* Returns a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
2091-
*
2092-
* @param source the source sequence whose elements will be pushed into the specified subject.
2093-
* @param subject the subject to push source elements into.
2094-
* @param <T> source type
2095-
* @param <R> result type
2152+
*
2153+
* @param source
2154+
* the source sequence whose elements will be pushed into the specified subject.
2155+
* @param subject
2156+
* the subject to push source elements into.
2157+
* @param <T>
2158+
* source type
2159+
* @param <R>
2160+
* result type
20962161
* @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
20972162
*/
20982163
public static <T, R> ConnectableObservable<R> multicast(Observable<T> source, final Subject<T, R> subject) {
@@ -2101,7 +2166,7 @@ public static <T, R> ConnectableObservable<R> multicast(Observable<T> source, fi
21012166

21022167
/**
21032168
* Returns the only element of an observable sequence and throws an exception if there is not exactly one element in the observable sequence.
2104-
*
2169+
*
21052170
* @param that
21062171
* the source Observable
21072172
* @return The single element in the observable sequence.
@@ -2670,6 +2735,48 @@ public Boolean call(T t1) {
26702735
});
26712736
}
26722737

2738+
/**
2739+
* Creates a new Observable sequence by applying a function that you supply to each item in the
2740+
* original Observable sequence, where that function is itself an Observable that emits items, and
2741+
* then merges the results of that function applied to every item emitted by the original
2742+
* Observable, emitting these merged results as its own sequence.
2743+
* <p>
2744+
* Note: mapMany and flatMap are equivalent.
2745+
* <p>
2746+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
2747+
*
2748+
* @param func
2749+
* a function to apply to each item in the sequence, that returns an Observable.
2750+
* @return an Observable that emits a sequence that is the result of applying the transformation
2751+
* function to each item in the input sequence and merging the results of the
2752+
* Observables obtained from this transformation.
2753+
* @see {@link #mapMany(Func1)}
2754+
*/
2755+
public <R> Observable<R> flatMap(Func1<T, Observable<R>> func) {
2756+
return mapMany(func);
2757+
}
2758+
2759+
/**
2760+
* Creates a new Observable sequence by applying a function that you supply to each item in the
2761+
* original Observable sequence, where that function is itself an Observable that emits items, and
2762+
* then merges the results of that function applied to every item emitted by the original
2763+
* Observable, emitting these merged results as its own sequence.
2764+
* <p>
2765+
* Note: mapMany and flatMap are equivalent.
2766+
* <p>
2767+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
2768+
*
2769+
* @param callback
2770+
* a function to apply to each item in the sequence that returns an Observable.
2771+
* @return an Observable that emits a sequence that is the result of applying the transformation'
2772+
* function to each item in the input sequence and merging the results of the
2773+
* Observables obtained from this transformation.
2774+
* @see {@link #mapMany(Object)}
2775+
*/
2776+
public <R> Observable<R> flatMap(final Object callback) {
2777+
return mapMany(callback);
2778+
}
2779+
26732780
/**
26742781
* Filters an Observable by discarding any of its emissions that do not meet some test.
26752782
* <p>
@@ -2805,13 +2912,16 @@ public R call(T t1) {
28052912
* then merges the results of that function applied to every item emitted by the original
28062913
* Observable, emitting these merged results as its own sequence.
28072914
* <p>
2915+
* Note: mapMany and flatMap are equivalent.
2916+
* <p>
28082917
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
28092918
*
28102919
* @param func
28112920
* a function to apply to each item in the sequence, that returns an Observable.
28122921
* @return an Observable that emits a sequence that is the result of applying the transformation
28132922
* function to each item in the input sequence and merging the results of the
28142923
* Observables obtained from this transformation.
2924+
* @see {@link #flatMap(Func1)}
28152925
*/
28162926
public <R> Observable<R> mapMany(Func1<T, Observable<R>> func) {
28172927
return mapMany(this, func);
@@ -2823,13 +2933,16 @@ public <R> Observable<R> mapMany(Func1<T, Observable<R>> func) {
28232933
* then merges the results of that function applied to every item emitted by the original
28242934
* Observable, emitting these merged results as its own sequence.
28252935
* <p>
2936+
* Note: mapMany and flatMap are equivalent.
2937+
* <p>
28262938
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
28272939
*
28282940
* @param callback
28292941
* a function to apply to each item in the sequence that returns an Observable.
28302942
* @return an Observable that emits a sequence that is the result of applying the transformation'
28312943
* function to each item in the input sequence and merging the results of the
28322944
* Observables obtained from this transformation.
2945+
* @see {@link #flatMap(Object))}
28332946
*/
28342947
public <R> Observable<R> mapMany(final Object callback) {
28352948
@SuppressWarnings("rawtypes")

0 commit comments

Comments
 (0)