/** * Subscribes to the {@link ObservableSource} and receives notifications for each element until the * onNext Predicate returns false. * <p> * If the Observable emits an error, it is wrapped into an * {@link io.reactivex.common.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the RxJavaCommonPlugins.onError handler. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param onNext * {@link Predicate} to execute for each item. * @return * a Disposable that allows cancelling an asynchronous sequence * @throws NullPointerException * if {@code onNext} is null * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate<? super T> onNext) { return forEachWhile(onNext, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION); }
@Test(expected = NullPointerException.class) public void forEachWhileOnCompleteNull() { just1.forEachWhile(new Predicate<Integer>() { @Override public boolean test(Integer v) { return true; } }, new Consumer<Throwable>() { @Override public void accept(Throwable e) { } }, null); }
@Test(expected = NullPointerException.class) public void forEachWhileOnErrorNull() { just1.forEachWhile(new Predicate<Integer>() { @Override public boolean test(Integer v) { return true; } }, null); }
/** * Subscribes to the {@link ObservableSource} and receives notifications for each element until the * onNext Predicate returns false. * <p> * If the Observable emits an error, it is wrapped into an * {@link io.reactivex.common.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the RxJavaCommonPlugins.onError handler. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param onNext * {@link Predicate} to execute for each item. * @return * a Disposable that allows cancelling an asynchronous sequence * @throws NullPointerException * if {@code onNext} is null * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate<? super T> onNext) { return forEachWhile(onNext, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION); }
/** * Subscribes to the {@link ObservableSource} and receives notifications for each element and error events until the * onNext Predicate returns false. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param onNext * {@link Predicate} to execute for each item. * @param onError * {@link Consumer} to execute when an error is emitted. * @return * a Disposable that allows cancelling an asynchronous sequence * @throws NullPointerException * if {@code onNext} is null, or * if {@code onError} is null * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate<? super T> onNext, Consumer<? super Throwable> onError) { return forEachWhile(onNext, onError, Functions.EMPTY_ACTION); }
@Test(expected = NullPointerException.class) public void forEachWhileNull() { just1.forEachWhile(null); }
@Override public Object apply(Observable<Integer> f) throws Exception { return f.forEachWhile(Functions.alwaysTrue()); } }, false, 1, 1, (Object[])null);
/** * Subscribes to the {@link ObservableSource} and receives notifications for each element and error events until the * onNext Predicate returns false. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param onNext * {@link Predicate} to execute for each item. * @param onError * {@link Consumer} to execute when an error is emitted. * @return * a Disposable that allows cancelling an asynchronous sequence * @throws NullPointerException * if {@code onNext} is null, or * if {@code onError} is null * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate<? super T> onNext, Consumer<? super Throwable> onError) { return forEachWhile(onNext, onError, Functions.EMPTY_ACTION); }
@Test public void forEachWile() { final List<Object> list = new ArrayList<Object>(); Observable.range(1, 5) .doOnNext(new Consumer<Integer>() { @Override public void accept(Integer v) throws Exception { list.add(v); } }) .forEachWhile(new Predicate<Integer>() { @Override public boolean test(Integer v) throws Exception { return v < 3; } }); assertEquals(Arrays.asList(1, 2, 3), list); }
@Test public void whilePredicateThrows() { List<Throwable> errors = TestCommonHelper.trackPluginErrors(); try { Observable.just(1).forEachWhile(new Predicate<Integer>() { @Override public boolean test(Integer v) throws Exception { throw new TestException(); } }); TestCommonHelper.assertError(errors, 0, OnErrorNotImplementedException.class); Throwable c = errors.get(0).getCause(); assertTrue("" + c, c instanceof TestException); } finally { RxJavaCommonPlugins.reset(); } }
@Test public void observableForEachWhile() { Observable.error(new TestException()) .forEachWhile(Functions.alwaysTrue()); }
@Test public void forEachWileWithError() { final List<Object> list = new ArrayList<Object>(); Observable.range(1, 5).concatWith(Observable.<Integer>error(new TestException())) .doOnNext(new Consumer<Integer>() { @Override public void accept(Integer v) throws Exception { list.add(v); } }) .forEachWhile(new Predicate<Integer>() { @Override public boolean test(Integer v) throws Exception { return true; } }, new Consumer<Throwable>() { @Override public void accept(Throwable e) throws Exception { list.add(100); } }); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); }
@Test public void whileCompleteThrows() { List<Throwable> errors = TestCommonHelper.trackPluginErrors(); try { Observable.just(1).forEachWhile(Functions.alwaysTrue(), Functions.emptyConsumer(), new Action() { @Override public void run() throws Exception { throw new TestException(); } }); TestCommonHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaCommonPlugins.reset(); } }
@Test public void whileErrorThrows() { List<Throwable> errors = TestCommonHelper.trackPluginErrors(); try { Observable.<Integer>error(new TestException("Outer")) .forEachWhile(Functions.alwaysTrue(), new Consumer<Throwable>() { @Override public void accept(Throwable v) throws Exception { throw new TestException("Inner"); } }); TestCommonHelper.assertError(errors, 0, CompositeException.class); List<Throwable> ce = TestCommonHelper.compositeList(errors.get(0)); TestCommonHelper.assertError(ce, 0, TestException.class, "Outer"); TestCommonHelper.assertError(ce, 1, TestException.class, "Inner"); } finally { RxJavaCommonPlugins.reset(); } }