io.reactivex.Observable.forEachWhile()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(201)

本文整理了Java中io.reactivex.Observable.forEachWhile()方法的一些代码示例,展示了Observable.forEachWhile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.forEachWhile()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:forEachWhile

Observable.forEachWhile介绍

[英]Subscribes to the ObservableSource and receives notifications for each element until the onNext Predicate returns false.

If the Observable emits an error, it is wrapped into an io.reactivex.exceptions.OnErrorNotImplementedExceptionand routed to the RxJavaPlugins.onError handler. Scheduler: forEachWhile does not operate by default on a particular Scheduler.
[中]订阅ObservableSource并接收每个元素的通知,直到onNext谓词返回false。
如果可观测对象发出错误,它将被包装到io中。reactivex。例外。OnErrorNotImplementedException并路由到RxJavaPlugins。一个错误处理程序。调度器:forEachWhile默认情况下不会在特定的调度器上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

  1. /**
  2. * Subscribes to the {@link ObservableSource} and receives notifications for each element and error events until the
  3. * onNext Predicate returns false.
  4. * <dl>
  5. * <dt><b>Scheduler:</b></dt>
  6. * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd>
  7. * </dl>
  8. *
  9. * @param onNext
  10. * {@link Predicate} to execute for each item.
  11. * @param onError
  12. * {@link Consumer} to execute when an error is emitted.
  13. * @return
  14. * a Disposable that allows disposing of an asynchronous sequence
  15. * @throws NullPointerException
  16. * if {@code onNext} is null, or
  17. * if {@code onError} is null
  18. * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
  19. */
  20. @CheckReturnValue
  21. @SchedulerSupport(SchedulerSupport.NONE)
  22. public final Disposable forEachWhile(Predicate<? super T> onNext, Consumer<? super Throwable> onError) {
  23. return forEachWhile(onNext, onError, Functions.EMPTY_ACTION);
  24. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Override
  2. public Object apply(Observable<Integer> f) throws Exception {
  3. return f.forEachWhile(Functions.alwaysTrue());
  4. }
  5. }, false, 1, 1, (Object[])null);

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void forEachWhileOnErrorNull() {
  3. just1.forEachWhile(new Predicate<Integer>() {
  4. @Override
  5. public boolean test(Integer v) {
  6. return true;
  7. }
  8. }, null);
  9. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void forEachWhileOnCompleteNull() {
  3. just1.forEachWhile(new Predicate<Integer>() {
  4. @Override
  5. public boolean test(Integer v) {
  6. return true;
  7. }
  8. }, new Consumer<Throwable>() {
  9. @Override
  10. public void accept(Throwable e) { }
  11. }, null);
  12. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void forEachWhileNull() {
  3. just1.forEachWhile(null);
  4. }

代码示例来源:origin: ReactiveX/RxJava

  1. /**
  2. * Subscribes to the {@link ObservableSource} and receives notifications for each element until the
  3. * onNext Predicate returns false.
  4. * <p>
  5. * <img width="640" height="272" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/forEachWhile.o.png" alt="">
  6. * <p>
  7. * If the Observable emits an error, it is wrapped into an
  8. * {@link io.reactivex.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException}
  9. * and routed to the RxJavaPlugins.onError handler.
  10. * <dl>
  11. * <dt><b>Scheduler:</b></dt>
  12. * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd>
  13. * </dl>
  14. *
  15. * @param onNext
  16. * {@link Predicate} to execute for each item.
  17. * @return
  18. * a Disposable that allows disposing of an asynchronous sequence
  19. * @throws NullPointerException
  20. * if {@code onNext} is null
  21. * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
  22. */
  23. @CheckReturnValue
  24. @SchedulerSupport(SchedulerSupport.NONE)
  25. public final Disposable forEachWhile(Predicate<? super T> onNext) {
  26. return forEachWhile(onNext, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION);
  27. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Subscribes to the {@link ObservableSource} and receives notifications for each element and error events until the
  3. * onNext Predicate returns false.
  4. * <dl>
  5. * <dt><b>Scheduler:</b></dt>
  6. * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd>
  7. * </dl>
  8. *
  9. * @param onNext
  10. * {@link Predicate} to execute for each item.
  11. * @param onError
  12. * {@link Consumer} to execute when an error is emitted.
  13. * @return
  14. * a Disposable that allows cancelling an asynchronous sequence
  15. * @throws NullPointerException
  16. * if {@code onNext} is null, or
  17. * if {@code onError} is null
  18. * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
  19. */
  20. @CheckReturnValue
  21. @SchedulerSupport(SchedulerSupport.NONE)
  22. public final Disposable forEachWhile(Predicate<? super T> onNext, Consumer<? super Throwable> onError) {
  23. return forEachWhile(onNext, onError, Functions.EMPTY_ACTION);
  24. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Subscribes to the {@link ObservableSource} and receives notifications for each element until the
  3. * onNext Predicate returns false.
  4. * <p>
  5. * <img width="640" height="272" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/forEachWhile.o.png" alt="">
  6. * <p>
  7. * If the Observable emits an error, it is wrapped into an
  8. * {@link io.reactivex.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException}
  9. * and routed to the RxJavaPlugins.onError handler.
  10. * <dl>
  11. * <dt><b>Scheduler:</b></dt>
  12. * <dd>{@code forEachWhile} does not operate by default on a particular {@link Scheduler}.</dd>
  13. * </dl>
  14. *
  15. * @param onNext
  16. * {@link Predicate} to execute for each item.
  17. * @return
  18. * a Disposable that allows cancelling an asynchronous sequence
  19. * @throws NullPointerException
  20. * if {@code onNext} is null
  21. * @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
  22. */
  23. @CheckReturnValue
  24. @SchedulerSupport(SchedulerSupport.NONE)
  25. public final Disposable forEachWhile(Predicate<? super T> onNext) {
  26. return forEachWhile(onNext, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION);
  27. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void forEachWile() {
  3. final List<Object> list = new ArrayList<Object>();
  4. Observable.range(1, 5)
  5. .doOnNext(new Consumer<Integer>() {
  6. @Override
  7. public void accept(Integer v) throws Exception {
  8. list.add(v);
  9. }
  10. })
  11. .forEachWhile(new Predicate<Integer>() {
  12. @Override
  13. public boolean test(Integer v) throws Exception {
  14. return v < 3;
  15. }
  16. });
  17. assertEquals(Arrays.asList(1, 2, 3), list);
  18. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void whilePredicateThrows() {
  3. List<Throwable> errors = TestHelper.trackPluginErrors();
  4. try {
  5. Observable.just(1).forEachWhile(new Predicate<Integer>() {
  6. @Override
  7. public boolean test(Integer v) throws Exception {
  8. throw new TestException();
  9. }
  10. });
  11. TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
  12. Throwable c = errors.get(0).getCause();
  13. assertTrue("" + c, c instanceof TestException);
  14. } finally {
  15. RxJavaPlugins.reset();
  16. }
  17. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void observableForEachWhile() {
  3. Observable.error(new TestException())
  4. .forEachWhile(Functions.alwaysTrue());
  5. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void forEachWileWithError() {
  3. final List<Object> list = new ArrayList<Object>();
  4. Observable.range(1, 5).concatWith(Observable.<Integer>error(new TestException()))
  5. .doOnNext(new Consumer<Integer>() {
  6. @Override
  7. public void accept(Integer v) throws Exception {
  8. list.add(v);
  9. }
  10. })
  11. .forEachWhile(new Predicate<Integer>() {
  12. @Override
  13. public boolean test(Integer v) throws Exception {
  14. return true;
  15. }
  16. }, new Consumer<Throwable>() {
  17. @Override
  18. public void accept(Throwable e) throws Exception {
  19. list.add(100);
  20. }
  21. });
  22. assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list);
  23. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void whileCompleteThrows() {
  3. List<Throwable> errors = TestHelper.trackPluginErrors();
  4. try {
  5. Observable.just(1).forEachWhile(Functions.alwaysTrue(), Functions.emptyConsumer(),
  6. new Action() {
  7. @Override
  8. public void run() throws Exception {
  9. throw new TestException();
  10. }
  11. });
  12. TestHelper.assertUndeliverable(errors, 0, TestException.class);
  13. } finally {
  14. RxJavaPlugins.reset();
  15. }
  16. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void whileErrorThrows() {
  3. List<Throwable> errors = TestHelper.trackPluginErrors();
  4. try {
  5. Observable.<Integer>error(new TestException("Outer"))
  6. .forEachWhile(Functions.alwaysTrue(), new Consumer<Throwable>() {
  7. @Override
  8. public void accept(Throwable v) throws Exception {
  9. throw new TestException("Inner");
  10. }
  11. });
  12. TestHelper.assertError(errors, 0, CompositeException.class);
  13. List<Throwable> ce = TestHelper.compositeList(errors.get(0));
  14. TestHelper.assertError(ce, 0, TestException.class, "Outer");
  15. TestHelper.assertError(ce, 1, TestException.class, "Inner");
  16. } finally {
  17. RxJavaPlugins.reset();
  18. }
  19. }

相关文章

Observable类方法