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

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

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

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

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

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

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

@Test
public void testCountError() {
  Flowable<String> f = Flowable.error(new Callable<Throwable>() {
    @Override
    public Throwable call() {
      return new RuntimeException();
    }
  });
  f.count().subscribe(wo);
  verify(wo, never()).onSuccess(anyInt());
  verify(wo, times(1)).onError(any(RuntimeException.class));
}

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

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

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

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

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

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

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

@Test
public void testCountErrorFlowable() {
  Flowable<String> f = Flowable.error(new Callable<Throwable>() {
    @Override
    public Throwable call() {
      return new RuntimeException();
    }
  });
  f.count().toFlowable().subscribe(w);
  verify(w, never()).onNext(anyInt());
  verify(w, never()).onComplete();
  verify(w, times(1)).onError(any(RuntimeException.class));
}

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

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

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

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

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

@Test
public void justUnsubscribed() throws Exception {
  o = new Object();
  WeakReference<Object> wr = new WeakReference<Object>(o);
  TestSubscriber<Object> ts = new TestSubscriber<Object>(0);
  Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts);
  ts.cancel();
  o = null;
  System.gc();
  Thread.sleep(200);
  Assert.assertNull("Object retained!", wr.get());
}

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

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

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

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

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

@Test
public void just() throws Exception {
  o = new Object();
  WeakReference<Object> wr = new WeakReference<Object>(o);
  TestSubscriber<Object> ts = new TestSubscriber<Object>();
  Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts);
  ts.assertValue(1L);
  ts.assertComplete();
  ts.assertNoErrors();
  o = null;
  System.gc();
  Thread.sleep(200);
  Assert.assertNull("Object retained!", wr.get());
}

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

@Test
public void testTakeLastZeroProcessesAllItemsButIgnoresThem() {
  final AtomicInteger upstreamCount = new AtomicInteger();
  final int num = 10;
  long count = Flowable.range(1, num).doOnNext(new Consumer<Integer>() {
    @Override
    public void accept(Integer t) {
      upstreamCount.incrementAndGet();
    }})
    .takeLast(0).count().blockingGet();
  assertEquals(num, upstreamCount.get());
  assertEquals(0L, count);
}

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

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

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

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

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

@Test
public void doubleShare() {
  Iterable<Integer> it = Flowable.range(1, 300).blockingIterable();
    Flowable.just(it, it)
    .flatMapIterable(Functions.<Iterable<Integer>>identity())
    .share()
    .share()
    .count()
    .test()
    .assertResult(600L);
}

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

@Test
public void multiShare() {
  Iterable<Integer> it = Flowable.range(1, 300).blockingIterable();
  for (int i = 0; i < 5; i++) {
    Flowable<Integer> f = Flowable.just(it, it)
    .flatMapIterable(Functions.<Iterable<Integer>>identity());
    for (int j = 0; j < i; j++) {
      f = f.share();
    }
    f
    .count()
    .test()
    .withTag("Share: " + i)
    .assertResult(600L);
  }
}

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

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

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

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

相关文章

Flowable类方法