/** * Returns a {@link Future} representing the single value emitted by this {@code Observable}. * <p> * If the {@link Observable} emits more than one item, {@link java.util.concurrent.Future} will receive an * {@link java.lang.IllegalArgumentException}. If the {@link Observable} is empty, {@link java.util.concurrent.Future} * will receive an {@link java.util.NoSuchElementException}. * <p> * If the {@code Observable} may emit more than one item, use {@code Observable.toList().toBlocking().toFuture()}. * <p> * <img width="640" height="395" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.toFuture.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code toFuture} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @return a {@link Future} that expects a single item to be emitted by this {@code Observable} * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX documentation: To</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Future<T> toFuture() { return subscribeWith(new FutureObserver<T>()); }
/** * Returns a {@link Future} representing the single value emitted by this {@code Observable}. * <p> * If the {@link Observable} emits more than one item, {@link java.util.concurrent.Future} will receive an * {@link java.lang.IllegalArgumentException}. If the {@link Observable} is empty, {@link java.util.concurrent.Future} * will receive an {@link java.util.NoSuchElementException}. * <p> * If the {@code Observable} may emit more than one item, use {@code Observable.toList().toBlocking().toFuture()}. * <p> * <img width="640" height="395" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.toFuture.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code toFuture} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @return a {@link Future} that expects a single item to be emitted by this {@code Observable} * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX documentation: To</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Future<T> toFuture() { return subscribeWith(new FutureObserver<T>()); }
@Test public void range() { Observable.range(1, 5) .doAfterNext(afterNext) .subscribeWith(ts) .assertResult(1, 2, 3, 4, 5); assertEquals(Arrays.asList(1, -1, 2, -2, 3, -3, 4, -4, 5, -5), values); }
@Test public void just() { Observable.just(1) .doAfterNext(afterNext) .subscribeWith(ts) .assertResult(1); assertEquals(Arrays.asList(1, -1), values); }
@Test public void empty() { Observable.<Integer>empty() .doAfterNext(afterNext) .subscribeWith(ts) .assertResult(); assertTrue(values.isEmpty()); }
@Test public void withObservable() { Observable.range(1, 10) .subscribeWith(new TestObserver<Integer>()) .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); }
@Test public void error() { Observable.<Integer>error(new TestException()) .doAfterNext(afterNext) .subscribeWith(ts) .assertFailure(TestException.class); assertTrue(values.isEmpty()); }
@Test public void rangeConditional() { Observable.range(1, 5) .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertResult(1, 2, 3, 4, 5); assertEquals(Arrays.asList(1, -1, 2, -2, 3, -3, 4, -4, 5, -5), values); }
@Test public void justConditional() { Observable.just(1) .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertResult(1); assertEquals(Arrays.asList(1, -1), values); }
@Test public void emptyConditional() { Observable.<Integer>empty() .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertResult(); assertTrue(values.isEmpty()); }
@Test public void errorConditional() { Observable.<Integer>error(new TestException()) .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertFailure(TestException.class); assertTrue(values.isEmpty()); }