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

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

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

Observable.first介绍

[英]Returns a Single that emits only the very first item emitted by the source ObservableSource, or a default item if the source ObservableSource completes without emitting any items.

Scheduler: first does not operate by default on a particular Scheduler.
[中]返回仅发出源ObservableSource发出的第一个项的单个项,或者如果源ObservableSource完成而不发出任何项,则返回默认项。
调度器:第一个默认情况下不会在特定的调度器上运行。

代码示例

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

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

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

@Test
public void testFirstOrDefaultWithOneElement() {
  Single<Integer> o = Observable.just(1).first(2);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefault() {
  Single<Integer> o = Observable.just(1, 2, 3)
      .first(4);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrElseOfSome() {
  Observable<String> src = Observable.just("a", "b", "c");
  src.first("default").subscribe(wo);
  verify(wo, times(1)).onSuccess(anyString());
  verify(wo, times(1)).onSuccess("a");
  verify(wo, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() {
  Observable<String> src = Observable.just("a", "b", "c");
  src.filter(IS_D).first("default").subscribe(wo);
  verify(wo, times(1)).onSuccess(anyString());
  verify(wo, times(1)).onSuccess("default");
  verify(wo, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstOrElseWithPredicateOfSome() {
  Observable<String> src = Observable.just("a", "b", "c", "d", "e", "f");
  src.filter(IS_D).first("default").subscribe(wo);
  verify(wo, times(1)).onSuccess(anyString());
  verify(wo, times(1)).onSuccess("d");
  verify(wo, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstOrElseWithPredicateOfNoneMatchingThePredicateObservable() {
  Observable<String> src = Observable.just("a", "b", "c");
  src.filter(IS_D).first("default").toObservable().subscribe(w);
  verify(w, times(1)).onNext(anyString());
  verify(w, times(1)).onNext("default");
  verify(w, never()).onError(any(Throwable.class));
  verify(w, times(1)).onComplete();
}

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

@Test
public void testFirstOrElseOfNone() {
  Observable<String> src = Observable.empty();
  src.first("default").subscribe(wo);
  verify(wo, times(1)).onSuccess(anyString());
  verify(wo, times(1)).onSuccess("default");
  verify(wo, never()).onError(any(Throwable.class));
}

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

@Test
public void testFirstOrElseOfSomeObservable() {
  Observable<String> src = Observable.just("a", "b", "c");
  src.first("default").toObservable().subscribe(w);
  verify(w, times(1)).onNext(anyString());
  verify(w, times(1)).onNext("a");
  verify(w, never()).onError(any(Throwable.class));
  verify(w, times(1)).onComplete();
}

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

@Test
public void testFirstOrElseOfNoneObservable() {
  Observable<String> src = Observable.empty();
  src.first("default").toObservable().subscribe(w);
  verify(w, times(1)).onNext(anyString());
  verify(w, times(1)).onNext("default");
  verify(w, never()).onError(any(Throwable.class));
  verify(w, times(1)).onComplete();
}

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

@Test
public void testFirstOrElseWithPredicateOfSomeObservable() {
  Observable<String> src = Observable.just("a", "b", "c", "d", "e", "f");
  src.filter(IS_D).first("default").toObservable().subscribe(w);
  verify(w, times(1)).onNext(anyString());
  verify(w, times(1)).onNext("d");
  verify(w, never()).onError(any(Throwable.class));
  verify(w, times(1)).onComplete();
}

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

@Test
public void testFirstOrDefaultWithEmpty() {
  Single<Integer> o = Observable.<Integer> empty()
      .first(1);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefaultWithOneElementObservable() {
  Observable<Integer> o = Observable.just(1).first(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 testFirstOrDefaultWithPredicate() {
  Single<Integer> o = Observable.just(1, 2, 3, 4, 5, 6)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(8);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

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

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

@Test
public void testFirstOrDefaultObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3)
      .first(4).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 testFirstOrDefaultWithPredicateAndEmpty() {
  Single<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(2);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testFirstOrDefaultWithEmptyObservable() {
  Observable<Integer> o = Observable.<Integer> empty()
      .first(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 testFirstOrDefaultWithPredicateObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3, 4, 5, 6)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(8).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 testFirstOrDefaultWithPredicateAndEmptyObservable() {
  Observable<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .first(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();
}

相关文章

Observable类方法