Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x: Migrate Disposables static factories to Disposable interface #6781

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/io/reactivex/rxjava3/core/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
* underlying task-execution scheme supports stopping and restarting itself.
* <p>
* If the {@code Scheduler} is shut down or a {@code Worker} is disposed, the {@code schedule} methods
* should return the {@link io.reactivex.rxjava3.disposables.Disposables#disposed()} singleton instance indicating the shut down/disposed
* should return the {@link Disposable#disposed()} singleton instance indicating the shut down/disposed
* state to the caller. Since the shutdown or dispose can happen from any thread, the {@code schedule} implementations
* should make best effort to cancel tasks immediately after those tasks have been submitted to the
* underlying task-execution scheme if the shutdown/dispose was detected after this submission.
Expand Down Expand Up @@ -349,7 +349,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
* re-adjust the absolute/relative time calculation accordingly.
* <p>
* If the {@code Worker} is disposed, the {@code schedule} methods
* should return the {@link io.reactivex.rxjava3.disposables.Disposables#disposed()} singleton instance indicating the disposed
* should return the {@link Disposable#disposed()} singleton instance indicating the disposed
* state to the caller. Since the {@link #dispose()} call can happen on any thread, the {@code schedule} implementations
* should make best effort to cancel tasks immediately after those tasks have been submitted to the
* underlying task-execution scheme if the dispose was detected after this submission.
Expand Down
123 changes: 123 additions & 0 deletions src/main/java/io/reactivex/rxjava3/disposables/Disposable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
*/
package io.reactivex.rxjava3.disposables;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
import io.reactivex.rxjava3.internal.functions.Functions;
import org.reactivestreams.Subscription;

import java.util.Objects;
import java.util.concurrent.Future;

/**
* Represents a disposable resource.
*/
Expand All @@ -26,4 +35,118 @@ public interface Disposable {
* @return true if this resource has been disposed
*/
boolean isDisposed();

/**
* Construct a {@code Disposable} by wrapping a {@link Runnable} that is
* executed exactly once when the {@code Disposable} is disposed.
* @param run the Runnable to wrap
* @return the new Disposable instance
* @since 3.0.0
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these additions would count as @since 3.0.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Done.

@NonNull
static Disposable fromRunnable(@NonNull Runnable run) {
Objects.requireNonNull(run, "run is null");
return new RunnableDisposable(run);
}

/**
* Construct a {@code Disposable} by wrapping a {@link Action} that is
* executed exactly once when the {@code Disposable} is disposed.
* @param run the Action to wrap
* @return the new Disposable instance
* @since 3.0.0
*/
@NonNull
static Disposable fromAction(@NonNull Action run) {
Objects.requireNonNull(run, "run is null");
return new ActionDisposable(run);
}

/**
* Construct a {@code Disposable} by wrapping a {@link Future} that is
* cancelled exactly once when the {@code Disposable} is disposed.
* <p>
* The {@code Future} is cancelled with {@code mayInterruptIfRunning == true}.
* @param future the Future to wrap
* @return the new Disposable instance
* @see #fromFuture(Future, boolean)
* @since 3.0.0
*/
@NonNull
static Disposable fromFuture(@NonNull Future<?> future) {
Objects.requireNonNull(future, "future is null");
return fromFuture(future, true);
}

/**
* Construct a {@code Disposable} by wrapping a {@link Future} that is
* cancelled exactly once when the {@code Disposable} is disposed.
* @param future the Future to wrap
* @param allowInterrupt if true, the future cancel happens via {@code Future.cancel(true)}
* @return the new Disposable instance
* @since 3.0.0
*/
@NonNull
static Disposable fromFuture(@NonNull Future<?> future, boolean allowInterrupt) {
Objects.requireNonNull(future, "future is null");
return new FutureDisposable(future, allowInterrupt);
}

/**
* Construct a {@code Disposable} by wrapping a {@link Subscription} that is
* cancelled exactly once when the {@code Disposable} is disposed.
* @param subscription the Runnable to wrap
* @return the new Disposable instance
* @since 3.0.0
*/
@NonNull
static Disposable fromSubscription(@NonNull Subscription subscription) {
Objects.requireNonNull(subscription, "subscription is null");
return new SubscriptionDisposable(subscription);
}

/**
* Construct a {@code Disposable} by wrapping an {@link AutoCloseable} that is
* closed exactly once when the {@code Disposable} is disposed.
* @param autoCloseable the AutoCloseable to wrap
* @return the new Disposable instance
* @since 3.0.0
*/
@NonNull
static Disposable fromAutoCloseable(@NonNull AutoCloseable autoCloseable) {
Objects.requireNonNull(autoCloseable, "autoCloseable is null");
return new AutoCloseableDisposable(autoCloseable);
}

/**
* Construct an {@link AutoCloseable} by wrapping a {@code Disposable} that is
* disposed when the returned {@code AutoCloseable} is closed.
* @param disposable the Disposable instance
* @return the new AutoCloseable instance
* @since 3.0.0
*/
@NonNull
static AutoCloseable toAutoCloseable(@NonNull Disposable disposable) {
return disposable::dispose;
}

/**
* Returns a new, non-disposed {@code Disposable} instance.
* @return a new, non-disposed {@code Disposable} instance
* @since 3.0.0
*/
@NonNull
static Disposable empty() {
return fromRunnable(Functions.EMPTY_RUNNABLE);
}

/**
* Returns a shared, disposed {@code Disposable} instance.
* @return a shared, disposed {@code Disposable} instance
* @since 3.0.0
*/
@NonNull
static Disposable disposed() {
return EmptyDisposable.INSTANCE;
}
}
143 changes: 0 additions & 143 deletions src/main/java/io/reactivex/rxjava3/disposables/Disposables.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public boolean replace(@Nullable Disposable next) {
public Disposable get() {
Disposable d = resource.get();
if (d == DisposableHelper.DISPOSED) {
return Disposables.disposed();
return Disposable.disposed();
}
return d;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
/**
* Default implementations for {@link io.reactivex.rxjava3.disposables.Disposable Disposable}-based resource management
* ({@code Disposable} container types) and utility classes to construct
* {@link io.reactivex.rxjava3.disposables.Disposables Disposables} from callbacks and other types.
* {@link io.reactivex.rxjava3.disposables.Disposable Disposables} from callbacks and other types.
*/
package io.reactivex.rxjava3.disposables;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CompletableFromAction(Action run) {

@Override
protected void subscribeActual(CompletableObserver observer) {
Disposable d = Disposables.empty();
Disposable d = Disposable.empty();
observer.onSubscribe(d);
try {
run.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CompletableFromCallable(Callable<?> callable) {

@Override
protected void subscribeActual(CompletableObserver observer) {
Disposable d = Disposables.empty();
Disposable d = Disposable.empty();
observer.onSubscribe(d);
try {
callable.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CompletableFromRunnable(Runnable runnable) {

@Override
protected void subscribeActual(CompletableObserver observer) {
Disposable d = Disposables.empty();
Disposable d = Disposable.empty();
observer.onSubscribe(d);
try {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CompletableFromSupplier(Supplier<?> supplier) {

@Override
protected void subscribeActual(CompletableObserver observer) {
Disposable d = Disposables.empty();
Disposable d = Disposable.empty();
observer.onSubscribe(d);
try {
supplier.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package io.reactivex.rxjava3.internal.operators.maybe;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposables;
import io.reactivex.rxjava3.disposables.Disposable;

/**
* Signals a constant Throwable.
Expand All @@ -31,7 +31,7 @@ public MaybeError(Throwable error) {

@Override
protected void subscribeActual(MaybeObserver<? super T> observer) {
observer.onSubscribe(Disposables.disposed());
observer.onSubscribe(Disposable.disposed());
observer.onError(error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package io.reactivex.rxjava3.internal.operators.maybe;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposables;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Supplier;
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
Expand All @@ -34,7 +34,7 @@ public MaybeErrorCallable(Supplier<? extends Throwable> errorSupplier) {

@Override
protected void subscribeActual(MaybeObserver<? super T> observer) {
observer.onSubscribe(Disposables.disposed());
observer.onSubscribe(Disposable.disposed());
Throwable ex;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MaybeFromAction(Action action) {

@Override
protected void subscribeActual(MaybeObserver<? super T> observer) {
Disposable d = Disposables.empty();
Disposable d = Disposable.empty();
observer.onSubscribe(d);

if (!d.isDisposed()) {
Expand Down
Loading