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

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

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

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

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

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

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

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

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

@Test
public void testDefaultIfEmptyWithEmpty() {
  Flowable<Integer> source = Flowable.empty();
  Flowable<Integer> flowable = source.defaultIfEmpty(10);
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  verify(subscriber).onNext(10);
  verify(subscriber).onComplete();
  verify(subscriber, never()).onError(any(Throwable.class));
}

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

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

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

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

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

@Test
@Ignore("Subscribers should not throw")
public void testEmptyButClientThrows() {
  final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  Flowable.<Integer>empty().defaultIfEmpty(1).subscribe(new DefaultSubscriber<Integer>() {
    @Override
    public void onNext(Integer t) {
      throw new TestException();
    }
    @Override
    public void onError(Throwable e) {
      subscriber.onError(e);
    }
    @Override
    public void onComplete() {
      subscriber.onComplete();
    }
  });
  verify(subscriber).onError(any(TestException.class));
  verify(subscriber, never()).onNext(any(Integer.class));
  verify(subscriber, never()).onComplete();
}

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

@Test
public void testBackpressureEmpty() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  Flowable.<Integer>empty().defaultIfEmpty(1).subscribe(ts);
  ts.assertNoValues();
  ts.assertNotTerminated();
  ts.request(1);
  ts.assertValue(1);
  ts.assertNoErrors();
  ts.assertComplete();
}

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

@Test
  public void testBackpressureNonEmpty() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
    Flowable.just(1, 2, 3).defaultIfEmpty(1).subscribe(ts);
    ts.assertNoValues();
    ts.assertNotTerminated();
    ts.request(2);
    ts.assertValues(1, 2);
    ts.request(1);
    ts.assertValues(1, 2, 3);
    ts.assertNoErrors();
    ts.assertComplete();
  }
}

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

.defaultIfEmpty(it);

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

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

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

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

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

/**
 * Finds the last usable snapshot. For a given maxPosition, finds a snapshot that's older than
 * that version number so a new one can be incrementally computed if possible.
 *
 * @param aggregate   The aggregate for which a snapshot is to be computed
 * @param maxPosition The maximum allowed version of the snapshot that is deemed usable
 *
 * @return An Flowable that returns at most one snapshot
 */
default Flowable<SnapshotT> getLastUsableSnapshot(
    final AggregateT aggregate, long maxPosition) {
  return fromPublisher(getSnapshot(maxPosition, aggregate))
      .defaultIfEmpty(createEmptySnapshot())
      .doOnNext(it -> {
        final String snapshotAsString =
            it.getLastEventPosition() == 0 ? "<none>" :
                it.getLastEventPosition() == 0 ? "<none>" :
                    it.toString();
        getLog().debug("  -> Last Usable Snapshot: {}", snapshotAsString);
        it.setAggregate(aggregate);
      });
}

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

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

相关文章

Flowable类方法