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

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

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

Flowable.switchIfEmpty介绍

[英]Returns a Flowable that emits the items emitted by the source Publisher or the items of an alternate Publisher if the source Publisher is empty.

Backpressure: If the source Publisher is empty, the alternate Publisher is expected to honor backpressure. If the source Publisher is non-empty, it is expected to honor backpressure as instead. In either case, if violated, a MissingBackpressureException may get signaled somewhere downstream. Scheduler: switchIfEmpty does not operate by default on a particular Scheduler.
[中]返回一个可流动项,该可流动项发出源发布服务器发出的项目,或者如果源发布服务器为空,则发出备用发布服务器的项目。
背压:如果源发布服务器为空,则备用发布服务器应接受背压。如果源发布服务器为非空,则应将背压作为默认值。在任何一种情况下,如果违反,可能会在下游某处发出缺失背压异常*的信号。Scheduler:switchIfEmpty默认情况下不会在特定计划程序上运行。

代码示例

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

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

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

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

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

@Test
public void testSwitchWhenEmpty() throws Exception {
  final Flowable<Integer> flowable = Flowable.<Integer>empty()
      .switchIfEmpty(Flowable.fromIterable(Arrays.asList(42)));
  assertEquals(42, flowable.blockingSingle().intValue());
}

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

@Test
public void testSwitchWhenNotEmpty() throws Exception {
  final AtomicBoolean subscribed = new AtomicBoolean(false);
  final Flowable<Integer> flowable = Flowable.just(4)
      .switchIfEmpty(Flowable.just(2)
      .doOnSubscribe(new Consumer<Subscription>() {
        @Override
        public void accept(Subscription s) {
          subscribed.set(true);
        }
      }));
  assertEquals(4, flowable.blockingSingle().intValue());
  assertFalse(subscribed.get());
}

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

/**
 * Returns a Flowable that emits the items emitted by the source Publisher or a specified default item
 * if the source Publisher is empty.
 * <p>
 * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt="">
 * <dl>
 *  <dt><b>Backpressure:</b></dt>
 *  <dd>If the source {@code Publisher} is empty, this operator is guaranteed to honor backpressure from downstream.
 *  If the source {@code Publisher} is non-empty, it is expected to honor backpressure as well; if the rule is violated,
 *  a {@code MissingBackpressureException} <em>may</em> get signaled somewhere downstream.
 *  </dd>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param defaultItem
 *            the item to emit if the source Publisher emits no items
 * @return a Flowable that emits either the specified default item if the source Publisher emits no
 *         items, or the items emitted by the source Publisher
 * @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a>
 */
@CheckReturnValue
@NonNull
@BackpressureSupport(BackpressureKind.FULL)
@SchedulerSupport(SchedulerSupport.NONE)
public final Flowable<T> defaultIfEmpty(T defaultItem) {
  ObjectHelper.requireNonNull(defaultItem, "item is null");
  return switchIfEmpty(just(defaultItem));
}

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

@Test
public void testSwitchWithProducer() throws Exception {
  final AtomicBoolean emitted = new AtomicBoolean(false);
  Flowable<Long> withProducer = Flowable.unsafeCreate(new Publisher<Long>() {
    @Override
    public void subscribe(final Subscriber<? super Long> subscriber) {
      subscriber.onSubscribe(new Subscription() {
        @Override
        public void request(long n) {
          if (n > 0 && emitted.compareAndSet(false, true)) {
            emitted.set(true);
            subscriber.onNext(42L);
            subscriber.onComplete();
          }
        }
        @Override
        public void cancel() {
        }
      });
    }
  });
  final Flowable<Long> flowable = Flowable.<Long>empty().switchIfEmpty(withProducer);
  assertEquals(42, flowable.blockingSingle().intValue());
}

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

/**
 * Returns a Flowable that emits the items emitted by the source Publisher or a specified default item
 * if the source Publisher is empty.
 * <p>
 * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt="">
 * <dl>
 *  <dt><b>Backpressure:</b></dt>
 *  <dd>If the source {@code Publisher} is empty, this operator is guaranteed to honor backpressure from downstream.
 *  If the source {@code Publisher} is non-empty, it is expected to honor backpressure as well; if the rule is violated,
 *  a {@code MissingBackpressureException} <em>may</em> get signaled somewhere downstream.
 *  </dd>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param defaultItem
 *            the item to emit if the source Publisher emits no items
 * @return a Flowable that emits either the specified default item if the source Publisher emits no
 *         items, or the items emitted by the source Publisher
 * @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a>
 */
@CheckReturnValue
@BackpressureSupport(BackpressureKind.FULL)
@SchedulerSupport(SchedulerSupport.NONE)
public final Flowable<T> defaultIfEmpty(T defaultItem) {
  ObjectHelper.requireNonNull(defaultItem, "item is null");
  return switchIfEmpty(just(defaultItem));
}

代码示例来源:origin: micronaut-projects/micronaut-core

).switchIfEmpty(Flowable.error(new FunctionNotFoundException(functionName)));
return serviceInstanceLocator.map(instance -> {
  Optional<String> uri = instance.getMetadata().get(LocalFunctionRegistry.FUNCTION_PREFIX + functionName, String.class);

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

@Test
public void testBackpressureNoRequest() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  Flowable.<Integer>empty().switchIfEmpty(Flowable.just(1, 2, 3)).subscribe(ts);
  ts.assertNoValues();
  ts.assertNoErrors();
}

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

@Test
public void testSwitchShouldNotTriggerUnsubscribe() {
  final BooleanSubscription bs = new BooleanSubscription();
  Flowable.unsafeCreate(new Publisher<Long>() {
    @Override
    public void subscribe(final Subscriber<? super Long> subscriber) {
      subscriber.onSubscribe(bs);
      subscriber.onComplete();
    }
  }).switchIfEmpty(Flowable.<Long>never()).subscribe();
  assertFalse(bs.isCancelled());
}

代码示例来源:origin: micronaut-projects/micronaut-core

routePublisher = routePublisher.switchIfEmpty(Flowable.create((emitter) -> {
  HttpRequest<?> httpRequest = requestReference.get();
  MutableHttpResponse<?> response;

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

@Test
public void testSwitchRequestAlternativeObservableWithBackpressure() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L);
  Flowable.<Integer>empty().switchIfEmpty(Flowable.just(1, 2, 3)).subscribe(ts);
  assertEquals(Arrays.asList(1), ts.values());
  ts.assertNoErrors();
  ts.request(1);
  ts.assertValueCount(2);
  ts.request(1);
  ts.assertValueCount(3);
}

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

@Test
public void testBackpressureOnFirstObservable() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  Flowable.just(1, 2, 3).switchIfEmpty(Flowable.just(4, 5, 6)).subscribe(ts);
  ts.assertNotComplete();
  ts.assertNoErrors();
  ts.assertNoValues();
}

代码示例来源:origin: micronaut-projects/micronaut-core

setBodyContent(response, bodyContent)
);
return bodyToResponse.switchIfEmpty(Flowable.just(response));

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

.switchIfEmpty(Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)))
.subscribeOn(Schedulers.computation())
.subscribe(ts);

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

.switchIfEmpty(withProducer)
  .lift(new FlowableOperator<Long, Long>() {
@Override

代码示例来源:origin: fengzhizi715/RxCache

@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type) {
  Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type);
  Flowable<Record<T>> remote = source
      .map(new Function<T, Record<T>>() {
        @Override
        public Record<T> apply(@NonNull T t) throws Exception {
          rxCache.save(key, t);
          return new Record<>(Source.CLOUD, key, t);
        }
      });
  return cache.switchIfEmpty(remote);
}

代码示例来源:origin: fengzhizi715/RxCache

@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type) {
  Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type);
  Flowable<Record<T>> remote = source
      .map(new Function<T, Record<T>>() {
        @Override
        public Record<T> apply(@NonNull T t) throws Exception {
          rxCache.save(key, t);
          return new Record<>(Source.CLOUD, key, t);
        }
      });
  return remote.switchIfEmpty(cache);
}

代码示例来源:origin: fengzhizi715/RxCache

@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type) {
  Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type)
      .filter(new Predicate<Record<T>>() {
        @Override
        public boolean test(Record<T> record) throws Exception {
          return System.currentTimeMillis() - record.getCreateTime() <= timestamp;
        }
      });
  Flowable<Record<T>> remote = source
      .map(new Function<T, Record<T>>() {
        @Override
        public Record<T> apply(@NonNull T t) throws Exception {
          rxCache.save(key, t);
          return new Record<>(Source.CLOUD, key, t);
        }
      });
  return cache.switchIfEmpty(remote);
}

相关文章

Flowable类方法