@Override public Disposable call() { return Disposables.fromRunnable(unsubscribe); } };
/** * Returns the currently contained Disposable or null if this container is empty. * @return the current Disposable, may be null */ @Nullable public Disposable get() { Disposable d = resource.get(); if (d == DisposableHelper.DISPOSED) { return Disposables.disposed(); } return d; }
@Override public void subscribe(Observer<? super String> observer) { observer.onSubscribe(Disposables.empty()); push(observer, "one", 10); push(observer, "two", 60); push(observer, "three", 110); push(observer, "four", 160); push(observer, "five", 210); complete(observer, 500); } });
@Test public void disposeThrowsCheckedException() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new IOException(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (RuntimeException ex) { // expected if (!(ex.getCause() instanceof IOException)) { fail(ex.toString() + " should have thrown RuntimeException(IOException)"); } } assertTrue(d1.isDisposed()); }
/** * Construct a Disposable by wrapping a Future that is * cancelled exactly once when the Disposable is disposed. * @param future the Future to wrap * @return the new Disposable instance */ @NonNull public static Disposable fromFuture(@NonNull Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromFuture(future, true); }
@Test public void fromActionThrows() { try { Disposables.fromAction(new Action() { @Override public void run() throws Exception { Disposables.fromAction(new Action() { @Override public void run() throws Exception { Disposables.fromAction(new Action() { @Override public void run() throws Exception {
@Test public void disposeThrowsIAE() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new IllegalArgumentException(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (IllegalArgumentException ex) { // expected } assertTrue(d1.isDisposed()); }
/** * Construct a Disposable by wrapping a Future that is * cancelled exactly once when the Disposable is disposed. * @param future the Future to wrap * @return the new Disposable instance */ @NonNull public static Disposable fromFuture(@NonNull Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromFuture(future, true); }
@Test public void fromAction() { class AtomicAction extends AtomicBoolean implements Action { private static final long serialVersionUID = -1517510584253657229L; @Override public void run() throws Exception { set(true); } } AtomicAction aa = new AtomicAction(); Disposables.fromAction(aa).dispose(); assertTrue(aa.get()); }
@Override public void subscribe(Observer<Object> observer) { observer.onSubscribe(Disposables.empty()); push(observer, new Object(), 50); push(observer, new Object(), 200); complete(observer, 250); } });
/** * Returns the currently contained Disposable or null if this container is empty. * @return the current Disposable, may be null */ @Nullable public Disposable get() { Disposable d = resource.get(); if (d == DisposableHelper.DISPOSED) { return Disposables.disposed(); } return d; }
/** * Returns a new, non-disposed Disposable instance. * @return a new, non-disposed Disposable instance */ @NonNull public static Disposable empty() { return fromRunnable(Functions.EMPTY_RUNNABLE); }
@Test public void disposeThrowsError() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new AssertionError(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (AssertionError ex) { // expected } assertTrue(d1.isDisposed()); }
/** * Construct a Disposable by wrapping a Future that is * cancelled exactly once when the Disposable is disposed. * @param future the Future to wrap * @return the new Disposable instance */ @NonNull public static Disposable fromFuture(@NonNull Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromFuture(future, true); }
@Override public void subscribe(Observer<? super Integer> t) { t.onSubscribe(Disposables.empty()); calls.getAndIncrement(); } });
/** * Returns the currently contained Disposable or null if this container is empty. * @return the current Disposable, may be null */ @Nullable public Disposable get() { Disposable d = resource.get(); if (d == DisposableHelper.DISPOSED) { return Disposables.disposed(); } return d; }
@Override public Disposable call() { return Disposables.fromRunnable(unsubscribe); } };
@Test public void normal() { FutureTask<Object> ft = new FutureTask<Object>(Functions.EMPTY_RUNNABLE, null); Disposable d = Disposables.fromFuture(ft); assertFalse(d.isDisposed()); d.dispose(); assertTrue(d.isDisposed()); d.dispose(); assertTrue(d.isDisposed()); assertTrue(ft.isCancelled()); }
@Override public void run() { acd.replaceResource(0, Disposables.empty()); } };
@Override protected void subscribeActual(SingleObserver<? super T> s) { s.onSubscribe(Disposables.disposed()); s.onSuccess(value); }