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

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

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

Flowable.defaultIfEmpty介绍

[英]Returns a Flowable that emits the items emitted by the source Publisher or a specified default item if the source Publisher is empty.

Backpressure: If the source Publisher is empty, this operator is guaranteed to honor backpressure from downstream. If the source Publisher is non-empty, it is expected to honor backpressure as well; if the rule is violated, a MissingBackpressureException may get signaled somewhere downstream. Scheduler: defaultIfEmpty does not operate by default on a particular Scheduler.
[中]

代码示例

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

  1. @Override
  2. public Publisher<Integer> createPublisher(long elements) {
  3. return
  4. Flowable.range(1, (int)elements).defaultIfEmpty(0)
  5. ;
  6. }
  7. }

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

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

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

  1. @Override
  2. public Publisher<T> apply(final T v) throws Exception {
  3. Publisher<U> p = ObjectHelper.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null Publisher");
  4. return new FlowableTakePublisher<U>(p, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
  5. }
  6. }

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

  1. @Test
  2. public void testDefaultIfEmptyWithEmpty() {
  3. Flowable<Integer> source = Flowable.empty();
  4. Flowable<Integer> flowable = source.defaultIfEmpty(10);
  5. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  6. flowable.subscribe(subscriber);
  7. verify(subscriber).onNext(10);
  8. verify(subscriber).onComplete();
  9. verify(subscriber, never()).onError(any(Throwable.class));
  10. }

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

  1. @Test
  2. public void testDefaultIfEmpty() {
  3. Flowable<Integer> source = Flowable.just(1, 2, 3);
  4. Flowable<Integer> flowable = source.defaultIfEmpty(10);
  5. Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  6. flowable.subscribe(subscriber);
  7. verify(subscriber, never()).onNext(10);
  8. verify(subscriber).onNext(1);
  9. verify(subscriber).onNext(2);
  10. verify(subscriber).onNext(3);
  11. verify(subscriber).onComplete();
  12. verify(subscriber, never()).onError(any(Throwable.class));
  13. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public Publisher<T> apply(final T v) throws Exception {
  3. Publisher<U> p = ObjectHelper.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null Publisher");
  4. return new FlowableTakePublisher<U>(p, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
  5. }
  6. }

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

  1. @Test
  2. @Ignore("Subscribers should not throw")
  3. public void testEmptyButClientThrows() {
  4. final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  5. Flowable.<Integer>empty().defaultIfEmpty(1).subscribe(new DefaultSubscriber<Integer>() {
  6. @Override
  7. public void onNext(Integer t) {
  8. throw new TestException();
  9. }
  10. @Override
  11. public void onError(Throwable e) {
  12. subscriber.onError(e);
  13. }
  14. @Override
  15. public void onComplete() {
  16. subscriber.onComplete();
  17. }
  18. });
  19. verify(subscriber).onError(any(TestException.class));
  20. verify(subscriber, never()).onNext(any(Integer.class));
  21. verify(subscriber, never()).onComplete();
  22. }

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

  1. @Test
  2. public void testBackpressureEmpty() {
  3. TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  4. Flowable.<Integer>empty().defaultIfEmpty(1).subscribe(ts);
  5. ts.assertNoValues();
  6. ts.assertNotTerminated();
  7. ts.request(1);
  8. ts.assertValue(1);
  9. ts.assertNoErrors();
  10. ts.assertComplete();
  11. }

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

  1. @Test
  2. public void testBackpressureNonEmpty() {
  3. TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  4. Flowable.just(1, 2, 3).defaultIfEmpty(1).subscribe(ts);
  5. ts.assertNoValues();
  6. ts.assertNotTerminated();
  7. ts.request(2);
  8. ts.assertValues(1, 2);
  9. ts.request(1);
  10. ts.assertValues(1, 2, 3);
  11. ts.assertNoErrors();
  12. ts.assertComplete();
  13. }
  14. }

代码示例来源:origin: com.github.rahulsom/grooves-api

  1. .defaultIfEmpty(it);

代码示例来源:origin: com.github.rahulsom/grooves-api

  1. /**
  2. * Finds the last usable snapshot. For a given maxTimestamp, finds a snapshot whose last event
  3. * is older than timestamp so a new one can be incrementally computed if possible.
  4. *
  5. * @param aggregate The aggregate for which the latest snapshot is desired
  6. * @param maxTimestamp The max last event timestamp allowed for the snapshot
  7. *
  8. * @return An Flowable that returns at most one snapshot
  9. */
  10. @NotNull default Flowable<SnapshotT> getLastUsableSnapshot(
  11. @NotNull final AggregateT aggregate, @NotNull Date maxTimestamp) {
  12. return fromPublisher(getSnapshot(maxTimestamp, aggregate))
  13. .defaultIfEmpty(createEmptySnapshot())
  14. .doOnNext(it -> {
  15. getLog().debug(" -> Last Usable Snapshot: {}",
  16. it.getLastEventTimestamp() == null ? "<none>" : it.toString());
  17. it.setAggregate(aggregate);
  18. });
  19. }

代码示例来源:origin: com.github.rahulsom/grooves-api

  1. empty())
  2. .map(Flowable::just)
  3. .defaultIfEmpty(computeSnapshotAndEvents(
  4. aggregate, moment, redirect, events, snapshot))
  5. .flatMap(it -> it)

代码示例来源:origin: com.github.rahulsom/grooves-api

  1. /**
  2. * Finds the last usable snapshot. For a given maxPosition, finds a snapshot that's older than
  3. * that version number so a new one can be incrementally computed if possible.
  4. *
  5. * @param aggregate The aggregate for which a snapshot is to be computed
  6. * @param maxPosition The maximum allowed version of the snapshot that is deemed usable
  7. *
  8. * @return An Flowable that returns at most one snapshot
  9. */
  10. default Flowable<SnapshotT> getLastUsableSnapshot(
  11. final AggregateT aggregate, long maxPosition) {
  12. return fromPublisher(getSnapshot(maxPosition, aggregate))
  13. .defaultIfEmpty(createEmptySnapshot())
  14. .doOnNext(it -> {
  15. final String snapshotAsString =
  16. it.getLastEventPosition() == 0 ? "<none>" :
  17. it.getLastEventPosition() == 0 ? "<none>" :
  18. it.toString();
  19. getLog().debug(" -> Last Usable Snapshot: {}", snapshotAsString);
  20. it.setAggregate(aggregate);
  21. });
  22. }

代码示例来源:origin: com.github.rahulsom/grooves-api

  1. empty())
  2. .map(Flowable::just)
  3. .defaultIfEmpty(computeSnapshotAndEvents(
  4. aggregate, version, redirect, events, lastUsableSnapshot))
  5. .flatMap(it -> it);

相关文章

Flowable类方法