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

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

本文整理了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

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

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

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

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

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

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

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

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

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

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

@Test
public void testFirstOrDefaultWithEmpty() {
  Single<Integer> single = Flowable.<Integer> empty()
      .first(1);
  single.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@Test
public void testFirstOrDefaultWithPredicateAndEmpty() {
  Single<Integer> single = Flowable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(2);
  single.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefaultWithPredicateAndOneElement() {
  Single<Integer> single = Flowable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(4);
  single.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefaultWithEmptyFlowable() {
  Flowable<Integer> flowable = Flowable.<Integer> empty()
      .first(1).toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber, times(1)).onNext(1);
  inOrder.verify(subscriber, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

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

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

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

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

@Test
public void testFirstOrDefaultWithPredicateAndEmptyFlowable() {
  Flowable<Integer> flowable = Flowable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(2).toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber, times(1)).onNext(2);
  inOrder.verify(subscriber, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefaultWithPredicateAndOneElementFlowable() {
  Flowable<Integer> flowable = Flowable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(4).toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber, times(1)).onNext(2);
  inOrder.verify(subscriber, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

相关文章

Flowable类方法