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

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

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

Flowable.singleElement介绍

[英]Returns a Maybe that completes if this Flowable is empty, signals one item if this Flowable signals exactly one item or signals an IllegalArgumentException if this Flowable signals more than one item.

Backpressure: The operator honors backpressure from downstream and consumes the source Publisher in an unbounded manner (i.e., without applying backpressure). Scheduler: singleElement does not operate by default on a particular Scheduler.
[中]如果此可流文件为空,则返回一个可能已完成的消息;如果此可流文件正好表示一个项目,则表示一个项目;如果此可流文件表示多个项目,则表示IllegalArgumentException。
背压:操作员接受来自下游的背压,并以无限制的方式(即不施加背压)消耗源发布服务器。调度程序:singleElement默认情况下不会在特定调度程序上运行。

代码示例

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

@Override
  public Object apply(Flowable<Object> f) throws Exception {
    return f.singleElement();
  }
}, false, 1, 1, 1);

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

@Override
  public MaybeSource<Object> apply(Flowable<Object> f) throws Exception {
    return f.singleElement();
  }
});

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

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

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

/**
 * Retries at most times or until the predicate returns false, whichever happens first.
 *
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code retry} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * @param times the number of times to repeat
 * @param predicate the predicate called with the failure Throwable and should return true to trigger a retry.
 * @return the new Maybe instance
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Maybe<T> retry(long times, Predicate<? super Throwable> predicate) {
  return toFlowable().retry(times, predicate).singleElement();
}

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

@Override
public Publisher<Integer> createPublisher(final long elements) {
  return
      Flowable.just(1).singleElement().toFlowable()
    ;
}

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

@Test
public void testSingleWithEmpty() {
  Maybe<Integer> maybe = Flowable.<Integer> empty().singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  maybe.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer).onComplete();
  inOrder.verify(observer, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleWithEmptyFlowable() {
  Flowable<Integer> flowable = Flowable.<Integer> empty().singleElement().toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber).onComplete();
  inOrder.verify(subscriber, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingle() {
  Maybe<Integer> maybe = Flowable.just(1).singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  maybe.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleFlowable() {
  Flowable<Integer> flowable = Flowable.just(1).singleElement().toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber, times(1)).onNext(1);
  inOrder.verify(subscriber, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleWithTooManyElements() {
  Maybe<Integer> maybe = Flowable.just(1, 2).singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  maybe.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleWithTooManyElementsFlowable() {
  Flowable<Integer> flowable = Flowable.just(1, 2).singleElement().toFlowable();
  Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
  flowable.subscribe(subscriber);
  InOrder inOrder = inOrder(subscriber);
  inOrder.verify(subscriber, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void fromFlowableJust() {
  Flowable.just(1)
  .singleElement()
  .test()
  .assertResult(1);
}

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

@Test
public void fromFlowableEmpty() {
  Flowable.empty()
  .singleElement()
  .test()
  .assertResult();
}

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

@Test
public void testSingleWithPredicate() {
  Maybe<Integer> maybe = Flowable.just(1, 2)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  maybe.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void fromFlowableMany() {
  Flowable.range(1, 2)
  .singleElement()
  .test()
  .assertFailure(IllegalArgumentException.class);
}

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

@Test
public void testSingleWithPredicateAndTooManyElements() {
  Maybe<Integer> maybe = Flowable.just(1, 2, 3, 4)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  maybe.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void fromFlowableError() {
  Flowable.error(new TestException())
  .singleElement()
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void flowableMaybeFlowable() {
  Flowable.just(1).singleElement().toFlowable().test().assertResult(1);
}

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

@Test
public void fromObservableValueAndError() {
  Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
  .singleElement()
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void fromFlowableValueAndError() {
  Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
  .singleElement()
  .test()
  .assertFailure(TestException.class);
}

相关文章

Flowable类方法