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

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

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

Observable.lastElement介绍

[英]Returns a Maybe that emits the last item emitted by this Observable or completes if this Observable is empty.

Scheduler: lastElement does not operate by default on a particular Scheduler.
[中]返回一个Maybe,该Maybe发出此可观察对象发出的最后一项,如果此可观察对象为空,则返回complete。
调度器:默认情况下,lastElement不会在特定的调度器上运行。

代码示例

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

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

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

@Override
  public ObservableSource<Object> apply(Observable<Object> o) throws Exception {
    return o.lastElement().toObservable();
  }
});

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

@Test
public void testLastViaObservable() {
  Observable.just(1, 2, 3).lastElement();
}

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

@Test
public void testLastWithElements() {
  Maybe<Integer> last = Observable.just(1, 2, 3).lastElement();
  assertEquals(3, last.blockingGet().intValue());
}

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

@Test
  public void testLast() {
    Maybe<Integer> o = Observable.just(1, 2, 3).lastElement();

    MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(3);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
  public void testLastWithOneElement() {
    Maybe<Integer> o = Observable.just(1).lastElement();

    MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(1);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
public void testLastMultiSubscribe() {
  Maybe<Integer> last = Observable.just(1, 2, 3).lastElement();
  assertEquals(3, last.blockingGet().intValue());
  assertEquals(3, last.blockingGet().intValue());
}

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

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

          @Override
          public boolean test(Integer t1) {
            return t1 % 2 == 0;
          }
        })
        .lastElement();

    MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(6);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
  public void testLastWithPredicateAndOneElement() {
    Maybe<Integer> o = Observable.just(1, 2)
      .filter(
        new Predicate<Integer>() {

          @Override
          public boolean test(Integer t1) {
            return t1 % 2 == 0;
          }
        })
      .lastElement();

    MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(2);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
public void testLastWithNoElements() {
  Maybe<?> last = Observable.empty().lastElement();
  assertNull(last.blockingGet());
}

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

/**
 * We expect an ArithmeticException exception here because last() emits a single value
 * but then we divide by 0.
 */
@Test(expected = ArithmeticException.class)
public void testMapWithErrorInFunc() {
  Observable.range(1, 1).lastElement().map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer i) {
      return i / 0;
    }
  }).blockingGet();
}

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

@Test
public void dispose() {
  TestHelper.checkDisposed(Observable.never().lastElement().toObservable());
  TestHelper.checkDisposed(Observable.never().lastElement());
  TestHelper.checkDisposed(Observable.just(1).lastOrError().toObservable());
  TestHelper.checkDisposed(Observable.just(1).lastOrError());
  TestHelper.checkDisposed(Observable.just(1).last(2).toObservable());
  TestHelper.checkDisposed(Observable.just(1).last(2));
}

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

/**
 * While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty.
 */
@Test
public void testErrorPassesThruMap() {
  assertNull(Observable.range(1, 0).lastElement().map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer i) {
      return i;
    }
  }).blockingGet());
}

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

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

代码示例来源:origin: radixdlt/radixdlt-java

public io.reactivex.Maybe<Collection<Consumable>> getUnconsumedConsumables() {
    return newValidTransactions.lastElement().map(lastTx -> unconsumedConsumables.values());
  }
}

代码示例来源:origin: florent37/RxGps

public Maybe<Location> lastLocation() {
  return checkPlayServicesAvailable()
      .flatMap(new Function<Boolean, ObservableSource<Boolean>>() {
        @Override
        public ObservableSource<Boolean> apply(@NonNull Boolean aBoolean) throws Exception {
          return request(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
      })
      .flatMapMaybe(new Function<Boolean, MaybeSource<Location>>() {
        @Override
        public MaybeSource<Location> apply(@NonNull Boolean aBoolean) throws Exception {
          return rxLocation.location().lastLocation();
        }
      })
      .lastElement();
}

代码示例来源:origin: florent37/RxGps

public Maybe<Location> lastLocationIfExists() {
  return checkPlayServicesAvailable()
    .flatMap(new Function<Boolean, ObservableSource<Boolean>>() {
      @Override
      public ObservableSource<Boolean> apply(@android.support.annotation.NonNull Boolean aBoolean) throws Exception {
        return request(Manifest.permission.ACCESS_COARSE_LOCATION);
      }
    })
    .flatMapSingle(new Function<Boolean, SingleSource<Boolean>>() {
      @Override
      public SingleSource<Boolean> apply(Boolean aBoolean) throws Exception {
        return rxLocation.location().isLocationAvailable();
      }
    })
    .flatMapMaybe(new Function<Boolean, MaybeSource<Location>>() {
      @Override
      public MaybeSource<Location> apply(@android.support.annotation.NonNull Boolean isExists) throws Exception {
        if (isExists) {
          return rxLocation.location().lastLocation();
        } else {
          throw new RxGps.LastLocationUnavailableException();
        }
      }
    })
    .lastElement();
}

相关文章

Observable类方法