io.reactivex.Flowable.first()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(291)

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

Flowable.first介绍

[英]Returns a Single that emits only the very first item emitted by this Flowable, or a default item if this Flowable completes without emitting anything.

Backpressure: The operator honors backpressure from downstream and consumes the source Publisher in an unbounded manner (i.e., without applying backpressure). Scheduler: first does not operate by default on a particular Scheduler.
[中]返回一个仅发出此可流动项发出的第一个项的单个项,或者如果此可流动项完成而不发出任何内容,则返回一个默认项。
背压:操作员接受来自下游的背压,并以无限制的方式(即不施加背压)消耗源发布服务器。调度程序:默认情况下,first不会在特定调度程序上运行。

代码示例

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

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

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

  1. @Test
  2. public void testFirstOrElseOfNone() {
  3. Flowable<String> src = Flowable.empty();
  4. src.first("default").subscribe(wo);
  5. verify(wo, times(1)).onSuccess(anyString());
  6. verify(wo, times(1)).onSuccess("default");
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testFirstOrElseOfSome() {
  3. Flowable<String> src = Flowable.just("a", "b", "c");
  4. src.first("default").subscribe(wo);
  5. verify(wo, times(1)).onSuccess(anyString());
  6. verify(wo, times(1)).onSuccess("a");
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() {
  3. Flowable<String> src = Flowable.just("a", "b", "c");
  4. src.filter(IS_D).first("default").subscribe(wo);
  5. verify(wo, times(1)).onSuccess(anyString());
  6. verify(wo, times(1)).onSuccess("default");
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testFirstOrElseWithPredicateOfSome() {
  3. Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f");
  4. src.filter(IS_D).first("default").subscribe(wo);
  5. verify(wo, times(1)).onSuccess(anyString());
  6. verify(wo, times(1)).onSuccess("d");
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testFirstOrDefaultWithEmpty() {
  3. Single<Integer> single = Flowable.<Integer> empty()
  4. .first(1);
  5. single.subscribe(wo);
  6. InOrder inOrder = inOrder(wo);
  7. inOrder.verify(wo, times(1)).onSuccess(1);
  8. inOrder.verifyNoMoreInteractions();
  9. }

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

  1. @Test
  2. public void testFirstOrDefault() {
  3. Single<Integer> single = Flowable.just(1, 2, 3)
  4. .first(4);
  5. single.subscribe(wo);
  6. InOrder inOrder = inOrder(wo);
  7. inOrder.verify(wo, times(1)).onSuccess(1);
  8. inOrder.verifyNoMoreInteractions();
  9. }

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

  1. @Test
  2. public void testFirstOrDefaultWithOneElement() {
  3. Single<Integer> single = Flowable.just(1).first(2);
  4. single.subscribe(wo);
  5. InOrder inOrder = inOrder(wo);
  6. inOrder.verify(wo, times(1)).onSuccess(1);
  7. inOrder.verifyNoMoreInteractions();
  8. }

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

  1. @Test
  2. public void testFirstOrElseOfSomeFlowable() {
  3. Flowable<String> src = Flowable.just("a", "b", "c");
  4. src.first("default").toFlowable().subscribe(w);
  5. verify(w, times(1)).onNext(anyString());
  6. verify(w, times(1)).onNext("a");
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void testFirstOrElseOfNoneFlowable() {
  3. Flowable<String> src = Flowable.empty();
  4. src.first("default").toFlowable().subscribe(w);
  5. verify(w, times(1)).onNext(anyString());
  6. verify(w, times(1)).onNext("default");
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void testFirstOrElseWithPredicateOfSomeFlowable() {
  3. Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f");
  4. src.filter(IS_D).first("default").toFlowable().subscribe(w);
  5. verify(w, times(1)).onNext(anyString());
  6. verify(w, times(1)).onNext("d");
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void testFirstOrElseWithPredicateOfNoneMatchingThePredicateFlowable() {
  3. Flowable<String> src = Flowable.just("a", "b", "c");
  4. src.filter(IS_D).first("default").toFlowable().subscribe(w);
  5. verify(w, times(1)).onNext(anyString());
  6. verify(w, times(1)).onNext("default");
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void testFirstOrDefaultWithPredicate() {
  3. Single<Integer> single = Flowable.just(1, 2, 3, 4, 5, 6)
  4. .filter(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return t1 % 2 == 0;
  8. }
  9. })
  10. .first(8);
  11. single.subscribe(wo);
  12. InOrder inOrder = inOrder(wo);
  13. inOrder.verify(wo, times(1)).onSuccess(2);
  14. inOrder.verifyNoMoreInteractions();
  15. }

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

  1. @Test
  2. public void testFirstOrDefaultWithPredicateAndEmpty() {
  3. Single<Integer> single = Flowable.just(1)
  4. .filter(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return t1 % 2 == 0;
  8. }
  9. })
  10. .first(2);
  11. single.subscribe(wo);
  12. InOrder inOrder = inOrder(wo);
  13. inOrder.verify(wo, times(1)).onSuccess(2);
  14. inOrder.verifyNoMoreInteractions();
  15. }

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

  1. @Test
  2. public void testFirstOrDefaultWithPredicateAndOneElement() {
  3. Single<Integer> single = Flowable.just(1, 2)
  4. .filter(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return t1 % 2 == 0;
  8. }
  9. })
  10. .first(4);
  11. single.subscribe(wo);
  12. InOrder inOrder = inOrder(wo);
  13. inOrder.verify(wo, times(1)).onSuccess(2);
  14. inOrder.verifyNoMoreInteractions();
  15. }

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

  1. @Test
  2. public void testFirstOrDefaultWithEmptyFlowable() {
  3. Flowable<Integer> flowable = Flowable.<Integer> empty()
  4. .first(1).toFlowable();
  5. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  6. flowable.subscribe(subscriber);
  7. InOrder inOrder = inOrder(subscriber);
  8. inOrder.verify(subscriber, times(1)).onNext(1);
  9. inOrder.verify(subscriber, times(1)).onComplete();
  10. inOrder.verifyNoMoreInteractions();
  11. }

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

  1. @Test
  2. public void testFirstOrDefaultFlowable() {
  3. Flowable<Integer> flowable = Flowable.just(1, 2, 3)
  4. .first(4).toFlowable();
  5. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  6. flowable.subscribe(subscriber);
  7. InOrder inOrder = inOrder(subscriber);
  8. inOrder.verify(subscriber, times(1)).onNext(1);
  9. inOrder.verify(subscriber, times(1)).onComplete();
  10. inOrder.verifyNoMoreInteractions();
  11. }

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

  1. @Test
  2. public void testFirstOrDefaultWithOneElementFlowable() {
  3. Flowable<Integer> flowable = Flowable.just(1).first(2).toFlowable();
  4. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  5. flowable.subscribe(subscriber);
  6. InOrder inOrder = inOrder(subscriber);
  7. inOrder.verify(subscriber, times(1)).onNext(1);
  8. inOrder.verify(subscriber, times(1)).onComplete();
  9. inOrder.verifyNoMoreInteractions();
  10. }

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

  1. @Test
  2. public void testFirstOrDefaultWithPredicateAndEmptyFlowable() {
  3. Flowable<Integer> flowable = Flowable.just(1)
  4. .filter(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return t1 % 2 == 0;
  8. }
  9. })
  10. .first(2).toFlowable();
  11. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  12. flowable.subscribe(subscriber);
  13. InOrder inOrder = inOrder(subscriber);
  14. inOrder.verify(subscriber, times(1)).onNext(2);
  15. inOrder.verify(subscriber, times(1)).onComplete();
  16. inOrder.verifyNoMoreInteractions();
  17. }

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

  1. @Test
  2. public void testFirstOrDefaultWithPredicateAndOneElementFlowable() {
  3. Flowable<Integer> flowable = Flowable.just(1, 2)
  4. .filter(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return t1 % 2 == 0;
  8. }
  9. })
  10. .first(4).toFlowable();
  11. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  12. flowable.subscribe(subscriber);
  13. InOrder inOrder = inOrder(subscriber);
  14. inOrder.verify(subscriber, times(1)).onNext(2);
  15. inOrder.verify(subscriber, times(1)).onComplete();
  16. inOrder.verifyNoMoreInteractions();
  17. }

相关文章

Flowable类方法