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

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

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

Observable.singleElement介绍

[英]Returns a Maybe that completes if this Observable is empty or emits the single item emitted by this Observable, or signals an IllegalArgumentException if this Observable emits more than one item.

Scheduler: singleElement does not operate by default on a particular Scheduler.
[中]如果此可观测项为空或发出此可观测项发出的单个项,则返回一个可能完成的值;如果此可观测项发出多个项,则返回一个IllegalArgumentException信号。
调度程序:singleElement默认情况下不会在特定调度程序上运行。

代码示例

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

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

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

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

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

@Test
public void testSingleObservable() {
  Observable<Integer> o = Observable.just(1).singleElement().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 testSingle() {
  Maybe<Integer> o = Observable.just(1).singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void fromObservableJust() {
  Observable.just(1)
  .singleElement()
  .test()
  .assertResult(1);
}

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

@Test
public void testSingleWithEmptyObservable() {
  Observable<Integer> o = Observable.<Integer> empty().singleElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.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 testSingleWithTooManyElementsObservable() {
  Observable<Integer> o = Observable.just(1, 2).singleElement().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 testSingleWithTooManyElements() {
  Maybe<Integer> o = Observable.just(1, 2).singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleWithEmpty() {
  Maybe<Integer> o = Observable.<Integer> empty().singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  o.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 testSingleWithPredicateAndEmptyObservable() {
  Observable<Integer> o = Observable.just(1)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.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 testSingleWithPredicateObservable() {
  Observable<Integer> o = Observable.just(1, 2)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement().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 obervableMaybeobervable() {
  Observable.just(1).singleElement().toObservable().test().assertResult(1);
}

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

@Test
public void testSingleWithPredicateAndEmpty() {
  Maybe<Integer> o = Observable.just(1)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  o.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 testSingleWithPredicate() {
  Maybe<Integer> o = Observable.just(1, 2)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement();
  MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSingleWithPredicateAndTooManyElementsObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3, 4)
      .filter(
      new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .singleElement().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 testSingleWithPredicateAndTooManyElements() {
  Maybe<Integer> o = Observable.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();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onError(
      isA(IllegalArgumentException.class));
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void fromObservableEmpty() {
  Observable.empty()
  .singleElement()
  .test()
  .assertResult();
}

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

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

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

@Test
public void singleElementOperatorDoNotSwallowExceptionWhenDone() {
  final Throwable exception = new RuntimeException("some error");
  final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
  try {
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
      @Override public void accept(final Throwable throwable) throws Exception {
        error.set(throwable);
      }
    });
    Observable.unsafeCreate(new ObservableSource<Integer>() {
      @Override public void subscribe(final Observer<? super Integer> observer) {
        observer.onComplete();
        observer.onError(exception);
      }
    }).singleElement().test().assertComplete();
    assertSame(exception, error.get().getCause());
  } finally {
    RxJavaPlugins.reset();
  }
}

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

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

相关文章

Observable类方法