/** * Signals the events of this or the other Perhaps whichever * signals first. * @param other the other Perhaps instance * @return the new Perhaps instance */ @SuppressWarnings("unchecked") public final Perhaps<T> ambWith(Perhaps<? extends T> other) { return ambArray(this, other); }
/** * Blocks until this Perhaps terminates and calls the onNext with the success value. * @param onNext the callback to call when this Perhaps completes with a success value */ public final void blockingSubscribe(Consumer<? super T> onNext) { blockingSubscribe(onNext, Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION); }
/** * Delay the emission of the signals of this Perhaps by the * given amount of time. * @param delay the delay amount * @param unit the time unit of the delay * @param scheduler the scheduler to delay on * @return the new Perhaps instance */ public final Perhaps<T> delay(long delay, TimeUnit unit, Scheduler scheduler) { return delay(timer(delay, unit, scheduler)); }
/** * Switch to the fallback Perhaps if this Perhaps doesn't signal an * item (or terminates) within the specified time. * @param timeout the time to wait for an item * @param unit the unit of time * @param scheduler the scheduler to wait on * @param fallback the Perhaps to switch to if this Perhaps times out * @return the new Perhaps instance */ public final Perhaps<T> timeout(long timeout, TimeUnit unit, Scheduler scheduler, Perhaps<? extends T> fallback) { return timeout(timer(timeout, unit, scheduler), fallback); }
/** * Delay the subscription to this Perhaps by the given time amount. * @param delay the amount to delay the subscription * @param unit the delay time unit * @return the new Perhaps instance */ public final Perhaps<T> delaySubscription(long delay, TimeUnit unit) { return delaySubscription(timer(delay, unit)); }
/** * Subscribes to this Perhaps and calls the appropriate callback for the resulting signal. * @param onNext called when this Perhaps succeeds * @param onError called when this Perhaps fails * @return the Disposable that allows cancelling the subscription */ public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) { return subscribe(onNext, onError, Functions.EMPTY_ACTION); }
@SuppressWarnings("unchecked") @Test public void ambArray2Error() { PerhapsProcessor<Integer> sp1 = PerhapsProcessor.create(); PerhapsProcessor<Integer> sp2 = PerhapsProcessor.create(); TestSubscriber<Integer> ts = Perhaps.ambArray(sp1, sp2) .test(); assertTrue(sp1.hasSubscribers()); assertTrue(sp2.hasSubscribers()); ts.assertEmpty(); sp2.onError(new IOException()); ts.assertFailure(IOException.class); assertFalse(sp1.hasSubscribers()); assertFalse(sp2.hasSubscribers()); }
@Test public void delaySubscription() { Perhaps.just(1) .delaySubscription(100, TimeUnit.MILLISECONDS) .test() .awaitDone(5, TimeUnit.SECONDS) .assertResult(1); }
@SuppressWarnings("unchecked") @Test public void zipError1() { Perhaps.zip(Arrays.asList(Perhaps.error(new IOException()), Perhaps.just(2)), new Function<Object[], Object>() { @Override public Object apply(Object[] a) throws Exception { return "" + a[0] + a[1]; } }) .test() .assertFailure(IOException.class); }
@Test public void switchIfEmptyToError() { Perhaps.empty() .switchIfEmpty(Perhaps.error(new IOException())) .test() .assertFailure(IOException.class); }
@Test public void switchIfEmptyError() { Perhaps.error(new IOException()) .switchIfEmpty(Perhaps.just(2)) .test() .assertFailure(IOException.class); }