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

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

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

Flowable.firstElement介绍

[英]Returns a Maybe that emits only the very first item emitted by this Flowable or completes if this Flowable is empty.

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

代码示例

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

@Override
public Publisher<Integer> createPublisher(final long elements) {
  return
      Flowable.range(1, 10).firstElement().toFlowable()
    ;
}

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

@Test
public void testFirstOfNoneFlowable() {
  Flowable<Integer> flowable = Flowable.empty();
  flowable.firstElement().toFlowable().subscribe(w);
  verify(w, never()).onNext(anyInt());
  verify(w).onComplete();
  verify(w, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstWithEmpty() {
  Maybe<Integer> maybe = Flowable.<Integer> empty().firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm).onComplete();
  inOrder.verify(wm, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOfNone() {
  Flowable<Integer> flowable = Flowable.empty();
  flowable.firstElement().subscribe(wm);
  verify(wm, never()).onSuccess(anyInt());
  verify(wm).onComplete();
  verify(wm, never()).onError(isA(NoSuchElementException.class));
}

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

@Test
public void testFirst() {
  Maybe<Integer> maybe = Flowable.just(1, 2, 3).firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstWithPredicateOfNoneMatchingThePredicate() {
  Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
  flowable.filter(IS_EVEN).firstElement().subscribe(wm);
  verify(wm, never()).onSuccess(anyInt());
  verify(wm, times(1)).onComplete();
  verify(wm, never()).onError(isA(NoSuchElementException.class));
}

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

@Test
public void testFirstWithOneElement() {
  Maybe<Integer> maybe = Flowable.just(1).firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstWithPredicateOfNoneMatchingThePredicateFlowable() {
  Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
  flowable.filter(IS_EVEN).firstElement().toFlowable().subscribe(w);
  verify(w, never()).onNext(anyInt());
  verify(w).onComplete();
  verify(w, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstWithOneElementFlowable() {
  Flowable<Integer> flowable = Flowable.just(1).firstElement().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 testFirstWithEmptyFlowable() {
  Flowable<Integer> flowable = Flowable.<Integer> empty().firstElement().toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber).onComplete();
  inOrder.verify(subscriber, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstFlowable() {
  Flowable<Integer> flowable = Flowable.just(1, 2, 3).firstElement().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 testFirstWithPredicateAndEmpty() {
  Maybe<Integer> maybe = Flowable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm).onComplete();
  inOrder.verify(wm, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstWithPredicate() {
  Maybe<Integer> maybe = Flowable.just(1, 2, 3, 4, 5, 6)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstWithPredicateAndOneElement() {
  Maybe<Integer> maybe = Flowable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  maybe.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstWithPredicateAndOneElementFlowable() {
  Flowable<Integer> flowable = Flowable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement().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 testFirstWithPredicateAndEmptyFlowable() {
  Flowable<Integer> flowable = Flowable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement().toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber).onComplete();
  inOrder.verify(subscriber, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscription() {
  final int[] calls = { 0 };
  Maybe<Integer> source = Maybe.create(new MaybeOnSubscribe<Integer>() {
    @Override
    public void subscribe(MaybeEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onSuccess(1);
    }
  });
  Maybe.concat(Arrays.asList(source, source)).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

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

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscriptionDelayErrorIterable() {
  final int[] calls = { 0 };
  Flowable<Integer> source = Flowable.create(new FlowableOnSubscribe<Integer>() {
    @Override
    public void subscribe(FlowableEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onNext(1);
      s.onComplete();
    }
  }, BackpressureStrategy.MISSING);
  Flowable.concatDelayError(Arrays.asList(source, source)).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

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

@SuppressWarnings("unchecked")
  @Test
  public void noSubsequentSubscriptionDelayError() {
    final int[] calls = { 0 };

    Maybe<Integer> source = Maybe.create(new MaybeOnSubscribe<Integer>() {
      @Override
      public void subscribe(MaybeEmitter<Integer> s) throws Exception {
        calls[0]++;
        s.onSuccess(1);
      }
    });

    Maybe.concatArrayDelayError(source, source).firstElement()
    .test()
    .assertResult(1);

    assertEquals(1, calls[0]);
  }
}

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

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscription() {
  final int[] calls = { 0 };
  Single<Integer> source = Single.create(new SingleOnSubscribe<Integer>() {
    @Override
    public void subscribe(SingleEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onSuccess(1);
    }
  });
  Single.concatArray(source, source).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

相关文章

Flowable类方法