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

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

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

Flowable.count介绍

[英]Returns a Single that counts the total number of items emitted by the source Publisher and emits this count as a 64-bit Long.

Backpressure: The operator honors backpressure from downstream and consumes the source Publisher in an unbounded manner (i.e., without applying backpressure). Scheduler: count does not operate by default on a particular Scheduler.
[中]返回对源发布服务器发出的项目总数进行计数并以64位长度发出此计数的单个值。
背压:操作员接受来自下游的背压,并以无限制的方式(即不施加背压)消耗源发布服务器。计划程序:默认情况下,计数不会在特定计划程序上运行。

代码示例

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

  1. @Override
  2. public SingleSource<Long> apply(Flowable<Object> f) throws Exception {
  3. return f.count();
  4. }
  5. });

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

  1. @Override
  2. public Flowable<Long> apply(Flowable<Object> f) throws Exception {
  3. return f.count().toFlowable();
  4. }
  5. });

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

  1. @Test
  2. public void testCountError() {
  3. Flowable<String> f = Flowable.error(new Callable<Throwable>() {
  4. @Override
  5. public Throwable call() {
  6. return new RuntimeException();
  7. }
  8. });
  9. f.count().subscribe(wo);
  10. verify(wo, never()).onSuccess(anyInt());
  11. verify(wo, times(1)).onError(any(RuntimeException.class));
  12. }

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

  1. @Test
  2. public void testCountAFewItems() {
  3. Flowable<String> flowable = Flowable.just("a", "b", "c", "d");
  4. flowable.count().subscribe(wo);
  5. // we should be called only once
  6. verify(wo).onSuccess(4L);
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testCountZeroItems() {
  3. Flowable<String> flowable = Flowable.empty();
  4. flowable.count().subscribe(wo);
  5. // we should be called only once
  6. verify(wo).onSuccess(0L);
  7. verify(wo, never()).onError(any(Throwable.class));
  8. }

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

  1. @Test
  2. public void testCountZeroItemsFlowable() {
  3. Flowable<String> flowable = Flowable.empty();
  4. flowable.count().toFlowable().subscribe(w);
  5. // we should be called only once
  6. verify(w).onNext(0L);
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void testCountErrorFlowable() {
  3. Flowable<String> f = Flowable.error(new Callable<Throwable>() {
  4. @Override
  5. public Throwable call() {
  6. return new RuntimeException();
  7. }
  8. });
  9. f.count().toFlowable().subscribe(w);
  10. verify(w, never()).onNext(anyInt());
  11. verify(w, never()).onComplete();
  12. verify(w, times(1)).onError(any(RuntimeException.class));
  13. }

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

  1. @Test
  2. public void testCountAFewItemsFlowable() {
  3. Flowable<String> flowable = Flowable.just("a", "b", "c", "d");
  4. flowable.count().toFlowable().subscribe(w);
  5. // we should be called only once
  6. verify(w).onNext(4L);
  7. verify(w, never()).onError(any(Throwable.class));
  8. verify(w, times(1)).onComplete();
  9. }

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

  1. @Test
  2. public void simple() {
  3. Assert.assertEquals(0, Flowable.empty().count().blockingGet().intValue());
  4. Assert.assertEquals(1, Flowable.just(1).count().blockingGet().intValue());
  5. Assert.assertEquals(10, Flowable.range(1, 10).count().blockingGet().intValue());
  6. }

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

  1. @Test
  2. public void justUnsubscribed() throws Exception {
  3. o = new Object();
  4. WeakReference<Object> wr = new WeakReference<Object>(o);
  5. TestSubscriber<Object> ts = new TestSubscriber<Object>(0);
  6. Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts);
  7. ts.cancel();
  8. o = null;
  9. System.gc();
  10. Thread.sleep(200);
  11. Assert.assertNull("Object retained!", wr.get());
  12. }

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

  1. @Test
  2. public void simpleFlowable() {
  3. Assert.assertEquals(0, Flowable.empty().count().toFlowable().blockingLast().intValue());
  4. Assert.assertEquals(1, Flowable.just(1).count().toFlowable().blockingLast().intValue());
  5. Assert.assertEquals(10, Flowable.range(1, 10).count().toFlowable().blockingLast().intValue());
  6. }

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

  1. @Test
  2. public void dispose() {
  3. TestHelper.checkDisposed(Flowable.just(1).count());
  4. TestHelper.checkDisposed(Flowable.just(1).count().toFlowable());
  5. }

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

  1. @Test
  2. public void just() throws Exception {
  3. o = new Object();
  4. WeakReference<Object> wr = new WeakReference<Object>(o);
  5. TestSubscriber<Object> ts = new TestSubscriber<Object>();
  6. Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts);
  7. ts.assertValue(1L);
  8. ts.assertComplete();
  9. ts.assertNoErrors();
  10. o = null;
  11. System.gc();
  12. Thread.sleep(200);
  13. Assert.assertNull("Object retained!", wr.get());
  14. }

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

  1. @Test
  2. public void testTakeLastZeroProcessesAllItemsButIgnoresThem() {
  3. final AtomicInteger upstreamCount = new AtomicInteger();
  4. final int num = 10;
  5. long count = Flowable.range(1, num).doOnNext(new Consumer<Integer>() {
  6. @Override
  7. public void accept(Integer t) {
  8. upstreamCount.incrementAndGet();
  9. }})
  10. .takeLast(0).count().blockingGet();
  11. assertEquals(num, upstreamCount.get());
  12. assertEquals(0L, count);
  13. }

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

  1. @Test
  2. public void fromIterable() {
  3. ArrayList<String> items = new ArrayList<String>();
  4. items.add("one");
  5. items.add("two");
  6. items.add("three");
  7. assertEquals((Long)3L, Flowable.fromIterable(items).count().blockingGet());
  8. assertEquals("two", Flowable.fromIterable(items).skip(1).take(1).blockingSingle());
  9. assertEquals("three", Flowable.fromIterable(items).takeLast(1).blockingSingle());
  10. }

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

  1. @Test
  2. public void fromArityArgs1() {
  3. Flowable<String> items = Flowable.just("one");
  4. assertEquals((Long)1L, items.count().blockingGet());
  5. assertEquals("one", items.takeLast(1).blockingSingle());
  6. }

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

  1. @Test
  2. public void doubleShare() {
  3. Iterable<Integer> it = Flowable.range(1, 300).blockingIterable();
  4. Flowable.just(it, it)
  5. .flatMapIterable(Functions.<Iterable<Integer>>identity())
  6. .share()
  7. .share()
  8. .count()
  9. .test()
  10. .assertResult(600L);
  11. }

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

  1. @Test
  2. public void multiShare() {
  3. Iterable<Integer> it = Flowable.range(1, 300).blockingIterable();
  4. for (int i = 0; i < 5; i++) {
  5. Flowable<Integer> f = Flowable.just(it, it)
  6. .flatMapIterable(Functions.<Iterable<Integer>>identity());
  7. for (int j = 0; j < i; j++) {
  8. f = f.share();
  9. }
  10. f
  11. .count()
  12. .test()
  13. .withTag("Share: " + i)
  14. .assertResult(600L);
  15. }
  16. }

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

  1. @Test
  2. public void fromArityArgs3() {
  3. Flowable<String> items = Flowable.just("one", "two", "three");
  4. assertEquals((Long)3L, items.count().blockingGet());
  5. assertEquals("two", items.skip(1).take(1).blockingSingle());
  6. assertEquals("three", items.takeLast(1).blockingSingle());
  7. }

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

  1. @Test
  2. public void fromArray() {
  3. String[] items = new String[] { "one", "two", "three" };
  4. assertEquals((Long)3L, Flowable.fromArray(items).count().blockingGet());
  5. assertEquals("two", Flowable.fromArray(items).skip(1).take(1).blockingSingle());
  6. assertEquals("three", Flowable.fromArray(items).takeLast(1).blockingSingle());
  7. }

相关文章

Flowable类方法