/** * Subscribes to this Solo and calls the onNext if this Solo succeeds. * @param onNext called when this Solo succeeds * @return the Disposable that allows cancelling the subscription */ public final Disposable subscribe(Consumer<? super T> onNext) { return subscribe(onNext, Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION); }
/** * Subscribes to this Solo and calls the appropriate callback for the resulting signal. * @param onNext called when this Solo succeeds * @param onError called when this Solo 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); }
/** * Subscribe with a Subscriber (subclass) and return it as is. * @param <E> the Subscriber subclass type * @param s the soubscriber, not null * @return the {@code s} as is */ public final <E extends Subscriber<? super T>> E subscribeWith(E s) { subscribe(s); return s; }
/** * Converts this Solo into a Future and signals its single * value. * @return the new Future instance */ public final Future<T> toFuture() { FuturePerhapsSubscriber<T> fs = new FuturePerhapsSubscriber<T>(); subscribe(fs); return fs; } }
/** * Subscribe to this Solo and ignore any signal it produces. * @return the Disposable that allows cancelling the subscription */ public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION); }
/** * Subscribes to this Solo and calls the appropriate callback for the resulting signal. * @param onNext called when this Solo succeeds * @param onError called when this Solo fails * @param onComplete called when this Solo succeeds after the call to onNext * @return the Disposable that allows cancelling the subscription */ public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete) { LambdaSubscriber<T> s = new LambdaSubscriber<T>(onNext, onError, onComplete, Functions.REQUEST_MAX); subscribe(s); return s; }
@Override protected void subscribeActual(Subscriber<? super T> s) { AmbSubscriber<T> parent = new AmbSubscriber<T>(s); s.onSubscribe(parent); for (Solo<? extends T> source : sources) { if (source == null) { parent.onError(new NullPointerException("One of the sources is null")); break; } source.subscribe(parent); } }
void otherComplete() { SubscriptionHelper.cancel(upstream); if (once.compareAndSet(false, true)) { Solo<T> f = fallback; if (f != null) { f.subscribe(fallbackSubscriber); } else { downstream.onError(new TimeoutException()); } } }
@Override protected void subscribeActual(Subscriber<? super T> s) { TimeoutSubscriber<T> parent = new TimeoutSubscriber<T>(s, fallback); s.onSubscribe(parent); other.subscribe(parent.other); source.subscribe(parent); }
/** * Blockingly awaits indefinitely the success value of this Solo or rethrows * its error (wrapped into a RuntimeException if necessary). * @return the success value of this Solo */ public final T blockingGet() { BlockingGetSubscriber<T> s = new BlockingGetSubscriber<T>(); subscribe(s); return s.blockingGet(); }
@Override protected void subscribeActual(Subscriber<? super T> s) { Solo<T> sp; try { sp = ObjectHelper.requireNonNull(supplier.call(), "The supplier returned a null Solo"); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); EmptySubscription.error(ex, s); return; } sp.subscribe(s); }
@Override public void onError(Throwable t) { Solo<? extends R> sp; try { sp = ObjectHelper.requireNonNull(onErrorMapper.apply(t), "The onErrorMapper returned a null Solo"); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); downstream.onError(ex); return; } sp.subscribe(nextSubscriber); }
@Override public void onNext(T t) { Solo<? extends R> sp; try { sp = ObjectHelper.requireNonNull(onSuccessMapper.apply(t), "The onSuccessMapper returned a null Solo"); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); downstream.onError(ex); return; } sp.subscribe(nextSubscriber); }
@Override public void onNext(T t) { Solo<? extends R> sp; try { sp = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null Solo"); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); downstream.onError(ex); return; } sp.subscribe(nextSubscriber); }
@Override public void onError(Throwable t) { Solo<T> sp; try { sp = ObjectHelper.requireNonNull(errorHandler.apply(t), "The errorHandler returned a null Solo"); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); downstream.onError(new CompositeException(t, ex)); return; } sp.subscribe(nextSubscriber); }
@Test public void subscribeThreeArgError() { Solo.error(new IOException()).subscribe(this, this, this); assertEquals(1, count); }
@Test public void subscribeOneArg() { Solo.just(1).subscribe(this); assertEquals(1, count); }
@Test public void subscribeTwoArg() { Solo.just(1).subscribe(this, this); assertEquals(1, count); }
@Test public void subscribeThreeArg() { Solo.just(1).subscribe(this, this, this); assertEquals(2, count); }
@Test public void subscribeZeroArg() { Solo.just(1) .doOnNext(this) .subscribe(); assertEquals(1, count); }