Skip to content

Commit 48a845c

Browse files
ForEach aliases to Subscribe to match Java 8 Convention
As per discussion at #678 Code like this is now supported: ```java Observable.from(1, 2, 3).forEach(System.out::println); Observable.from(1, 2, 3).toBlocking().forEach(System.out::println); ```
1 parent 06ba752 commit 48a845c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,69 @@ public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? e
39023902
return mergeMap(func);
39033903
}
39043904

3905+
/**
3906+
* Subscribes to the {@link Observable} and receives notifications for each element.
3907+
* <p>
3908+
* Alias to {@link #subscribe(Action1)}
3909+
*
3910+
* @param onNext
3911+
* {@link Action1} to execute for each item.
3912+
* @throws IllegalArgumentException
3913+
* if {@code onNext} is null
3914+
* @throws IllegalArgumentException
3915+
* if {@code onError} is null
3916+
* @throws IllegalArgumentException
3917+
* if {@code onComplete} is null
3918+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable#wiki-onnext-oncompleted-and-onerror">RxJava Wiki: onNext, onCompleted, and onError</a>
3919+
* */
3920+
public final void forEach(final Action1<? super T> onNext) {
3921+
subscribe(onNext);
3922+
}
3923+
3924+
/**
3925+
* Subscribes to the {@link Observable} and receives notifications for each element and error events.
3926+
* <p>
3927+
* Alias to {@link #subscribe(Action1, Action1)}
3928+
*
3929+
* @param onNext
3930+
* {@link Action1} to execute for each item.
3931+
* @param onError
3932+
* {@link Action1} to execute when an error is emitted.
3933+
* @throws IllegalArgumentException
3934+
* if {@code onNext} is null
3935+
* @throws IllegalArgumentException
3936+
* if {@code onError} is null
3937+
* @throws IllegalArgumentException
3938+
* if {@code onComplete} is null
3939+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable#wiki-onnext-oncompleted-and-onerror">RxJava Wiki: onNext, onCompleted, and onError</a>
3940+
* */
3941+
public final void forEach(final Action1<? super T> onNext, final Action1<Throwable> onError) {
3942+
subscribe(onNext, onError);
3943+
}
3944+
3945+
/**
3946+
* Subscribes to the {@link Observable} and receives notifications for each element and the terminal events.
3947+
* <p>
3948+
* Alias to {@link #subscribe(Action1, Action1, Action0)}
3949+
*
3950+
* @param onNext
3951+
* {@link Action1} to execute for each item.
3952+
* @param onError
3953+
* {@link Action1} to execute when an error is emitted.
3954+
* @param onComplete
3955+
* {@link Action0} to execute when completion is signalled.
3956+
* @throws IllegalArgumentException
3957+
* if {@code onNext} is null
3958+
* @throws IllegalArgumentException
3959+
* if {@code onError} is null
3960+
* @throws IllegalArgumentException
3961+
* if {@code onComplete} is null
3962+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable#wiki-onnext-oncompleted-and-onerror">RxJava Wiki: onNext, onCompleted, and onError</a>
3963+
* */
3964+
public final void forEach(final Action1<? super T> onNext, final Action1<Throwable> onError, final Action0 onComplete) {
3965+
subscribe(onNext, onError, onComplete);
3966+
}
3967+
39053968
/**
39063969
* Groups the items emitted by an Observable according to a specified criterion, and emits these grouped
39073970
* items as {@link GroupedObservable}s, one {@code GroupedObservable} per group.

0 commit comments

Comments
 (0)