io.reactivex.Observable.single()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(229)

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

Observable.single介绍

[英]Returns a Single that emits the single item emitted by this Observable, if this Observable emits only a single item, or a default item if the source ObservableSource emits no items. If the source ObservableSource emits more than one item, an IllegalArgumentException is signalled instead.

Scheduler: single does not operate by default on a particular Scheduler.
[中]如果此可观测对象仅发射单个项,则返回发射此可观测对象发射的单个项的单个项;如果源可观测源不发射任何项,则返回默认项。如果源ObservableSource发出多个项,则会发出IllegalArgumentException信号。
调度程序:默认情况下,single不会在特定调度程序上运行。

代码示例

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

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

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

/**
 * If this {@code Observable} completes after emitting a single item, return that item; if it emits
 * more than one item, throw an {@code IllegalArgumentException}; if it emits no items, return a default
 * value.
 * <p>
 * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/blockingSingleDefault.o.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code blockingSingle} does not operate by default on a particular {@link Scheduler}.</dd>
 *  <dt><b>Error handling:</b></dt>
 *  <dd>If the source signals an error, the operator wraps a checked {@link Exception}
 *  into {@link RuntimeException} and throws that. Otherwise, {@code RuntimeException}s and
 *  {@link Error}s are rethrown as they are.</dd>
 * </dl>
 *
 * @param defaultItem
 *            a default value to return if this {@code Observable} emits no items
 * @return the single item emitted by this {@code Observable}, or the default value if it emits no
 *         items
 * @see <a href="http://reactivex.io/documentation/operators/first.html">ReactiveX documentation: First</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final T blockingSingle(T defaultItem) {
  return single(defaultItem).blockingGet();
}

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

@Test
public void testSingleOrDefault() {
  Single<Integer> o = Observable.just(1).single(2);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultObservable() {
  Observable<Integer> o = Observable.just(1).single(2).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(1);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithTooManyElements() {
  Single<Integer> o = Observable.just(1, 2).single(3);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithEmpty() {
  Single<Integer> o = Observable.<Integer> empty()
      .single(1);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithTooManyElementsObservable() {
  Observable<Integer> o = Observable.just(1, 2).single(3).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithEmptyObservable() {
  Observable<Integer> o = Observable.<Integer> empty()
      .single(1).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(1);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithPredicateAndEmpty() {
  Single<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(2);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void singleDefault() {
  Observable.just(1).single(100).test().assertResult(1);
  Observable.empty().single(100).test().assertResult(100);
}

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

@Test
public void testSingleOrDefaultWithPredicate() {
  Single<Integer> o = Observable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(4);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

/**
 * If this {@code Observable} completes after emitting a single item, return that item; if it emits
 * more than one item, throw an {@code IllegalArgumentException}; if it emits no items, return a default
 * value.
 * <p>
 * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/blockingSingleDefault.o.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code blockingSingle} does not operate by default on a particular {@link Scheduler}.</dd>
 *  <dt><b>Error handling:</b></dt>
 *  <dd>If the source signals an error, the operator wraps a checked {@link Exception}
 *  into {@link RuntimeException} and throws that. Otherwise, {@code RuntimeException}s and
 *  {@link Error}s are rethrown as they are.</dd>
 * </dl>
 *
 * @param defaultItem
 *            a default value to return if this {@code Observable} emits no items
 * @return the single item emitted by this {@code Observable}, or the default value if it emits no
 *         items
 * @see <a href="http://reactivex.io/documentation/operators/first.html">ReactiveX documentation: First</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final T blockingSingle(T defaultItem) {
  return single(defaultItem).blockingGet();
}

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

@Test
public void testSingleOrDefaultWithPredicateObservable() {
  Observable<Integer> o = Observable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(4).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(2);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithPredicateAndEmptyObservable() {
  Observable<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(2).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(2);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithPredicateAndTooManyElements() {
  Single<Integer> o = Observable.just(1, 2, 3, 4)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(6);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleOrDefaultWithPredicateAndTooManyElementsObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3, 4)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .single(6).toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void singleDefaultObservable() {
  Observable.just(1).single(100).toObservable().test().assertResult(1);
  Observable.empty().single(100).toObservable().test().assertResult(100);
}

相关文章

Observable类方法