Skip to content

Commit 166c529

Browse files
JakeWhartonakarnokd
authored andcommitted
Migrate Disposables static factories to Disposable interface (#6781)
1 parent a249f4f commit 166c529

File tree

204 files changed

+1037
-1062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+1037
-1062
lines changed

src/main/java/io/reactivex/rxjava3/core/Scheduler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
* underlying task-execution scheme supports stopping and restarting itself.
8282
* <p>
8383
* If the {@code Scheduler} is shut down or a {@code Worker} is disposed, the {@code schedule} methods
84-
* should return the {@link io.reactivex.rxjava3.disposables.Disposables#disposed()} singleton instance indicating the shut down/disposed
84+
* should return the {@link Disposable#disposed()} singleton instance indicating the shut down/disposed
8585
* state to the caller. Since the shutdown or dispose can happen from any thread, the {@code schedule} implementations
8686
* should make best effort to cancel tasks immediately after those tasks have been submitted to the
8787
* underlying task-execution scheme if the shutdown/dispose was detected after this submission.
@@ -349,7 +349,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
349349
* re-adjust the absolute/relative time calculation accordingly.
350350
* <p>
351351
* If the {@code Worker} is disposed, the {@code schedule} methods
352-
* should return the {@link io.reactivex.rxjava3.disposables.Disposables#disposed()} singleton instance indicating the disposed
352+
* should return the {@link Disposable#disposed()} singleton instance indicating the disposed
353353
* state to the caller. Since the {@link #dispose()} call can happen on any thread, the {@code schedule} implementations
354354
* should make best effort to cancel tasks immediately after those tasks have been submitted to the
355355
* underlying task-execution scheme if the dispose was detected after this submission.

src/main/java/io/reactivex/rxjava3/disposables/Disposable.java

+123
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
*/
1313
package io.reactivex.rxjava3.disposables;
1414

15+
import io.reactivex.rxjava3.annotations.NonNull;
16+
import io.reactivex.rxjava3.functions.Action;
17+
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
18+
import io.reactivex.rxjava3.internal.functions.Functions;
19+
import org.reactivestreams.Subscription;
20+
21+
import java.util.Objects;
22+
import java.util.concurrent.Future;
23+
1524
/**
1625
* Represents a disposable resource.
1726
*/
@@ -26,4 +35,118 @@ public interface Disposable {
2635
* @return true if this resource has been disposed
2736
*/
2837
boolean isDisposed();
38+
39+
/**
40+
* Construct a {@code Disposable} by wrapping a {@link Runnable} that is
41+
* executed exactly once when the {@code Disposable} is disposed.
42+
* @param run the Runnable to wrap
43+
* @return the new Disposable instance
44+
* @since 3.0.0
45+
*/
46+
@NonNull
47+
static Disposable fromRunnable(@NonNull Runnable run) {
48+
Objects.requireNonNull(run, "run is null");
49+
return new RunnableDisposable(run);
50+
}
51+
52+
/**
53+
* Construct a {@code Disposable} by wrapping a {@link Action} that is
54+
* executed exactly once when the {@code Disposable} is disposed.
55+
* @param run the Action to wrap
56+
* @return the new Disposable instance
57+
* @since 3.0.0
58+
*/
59+
@NonNull
60+
static Disposable fromAction(@NonNull Action run) {
61+
Objects.requireNonNull(run, "run is null");
62+
return new ActionDisposable(run);
63+
}
64+
65+
/**
66+
* Construct a {@code Disposable} by wrapping a {@link Future} that is
67+
* cancelled exactly once when the {@code Disposable} is disposed.
68+
* <p>
69+
* The {@code Future} is cancelled with {@code mayInterruptIfRunning == true}.
70+
* @param future the Future to wrap
71+
* @return the new Disposable instance
72+
* @see #fromFuture(Future, boolean)
73+
* @since 3.0.0
74+
*/
75+
@NonNull
76+
static Disposable fromFuture(@NonNull Future<?> future) {
77+
Objects.requireNonNull(future, "future is null");
78+
return fromFuture(future, true);
79+
}
80+
81+
/**
82+
* Construct a {@code Disposable} by wrapping a {@link Future} that is
83+
* cancelled exactly once when the {@code Disposable} is disposed.
84+
* @param future the Future to wrap
85+
* @param allowInterrupt if true, the future cancel happens via {@code Future.cancel(true)}
86+
* @return the new Disposable instance
87+
* @since 3.0.0
88+
*/
89+
@NonNull
90+
static Disposable fromFuture(@NonNull Future<?> future, boolean allowInterrupt) {
91+
Objects.requireNonNull(future, "future is null");
92+
return new FutureDisposable(future, allowInterrupt);
93+
}
94+
95+
/**
96+
* Construct a {@code Disposable} by wrapping a {@link Subscription} that is
97+
* cancelled exactly once when the {@code Disposable} is disposed.
98+
* @param subscription the Runnable to wrap
99+
* @return the new Disposable instance
100+
* @since 3.0.0
101+
*/
102+
@NonNull
103+
static Disposable fromSubscription(@NonNull Subscription subscription) {
104+
Objects.requireNonNull(subscription, "subscription is null");
105+
return new SubscriptionDisposable(subscription);
106+
}
107+
108+
/**
109+
* Construct a {@code Disposable} by wrapping an {@link AutoCloseable} that is
110+
* closed exactly once when the {@code Disposable} is disposed.
111+
* @param autoCloseable the AutoCloseable to wrap
112+
* @return the new Disposable instance
113+
* @since 3.0.0
114+
*/
115+
@NonNull
116+
static Disposable fromAutoCloseable(@NonNull AutoCloseable autoCloseable) {
117+
Objects.requireNonNull(autoCloseable, "autoCloseable is null");
118+
return new AutoCloseableDisposable(autoCloseable);
119+
}
120+
121+
/**
122+
* Construct an {@link AutoCloseable} by wrapping a {@code Disposable} that is
123+
* disposed when the returned {@code AutoCloseable} is closed.
124+
* @param disposable the Disposable instance
125+
* @return the new AutoCloseable instance
126+
* @since 3.0.0
127+
*/
128+
@NonNull
129+
static AutoCloseable toAutoCloseable(@NonNull Disposable disposable) {
130+
return disposable::dispose;
131+
}
132+
133+
/**
134+
* Returns a new, non-disposed {@code Disposable} instance.
135+
* @return a new, non-disposed {@code Disposable} instance
136+
* @since 3.0.0
137+
*/
138+
@NonNull
139+
static Disposable empty() {
140+
return fromRunnable(Functions.EMPTY_RUNNABLE);
141+
}
142+
143+
/**
144+
* Returns a shared, disposed {@code Disposable} instance.
145+
* @return a shared, disposed {@code Disposable} instance
146+
* @since 3.0.0
147+
*/
148+
@NonNull
149+
static Disposable disposed() {
150+
return EmptyDisposable.INSTANCE;
151+
}
29152
}

src/main/java/io/reactivex/rxjava3/disposables/Disposables.java

-143
This file was deleted.

src/main/java/io/reactivex/rxjava3/disposables/SerialDisposable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public boolean replace(@Nullable Disposable next) {
7171
public Disposable get() {
7272
Disposable d = resource.get();
7373
if (d == DisposableHelper.DISPOSED) {
74-
return Disposables.disposed();
74+
return Disposable.disposed();
7575
}
7676
return d;
7777
}

src/main/java/io/reactivex/rxjava3/disposables/package-info.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
/**
1818
* Default implementations for {@link io.reactivex.rxjava3.disposables.Disposable Disposable}-based resource management
1919
* ({@code Disposable} container types) and utility classes to construct
20-
* {@link io.reactivex.rxjava3.disposables.Disposables Disposables} from callbacks and other types.
20+
* {@link io.reactivex.rxjava3.disposables.Disposable Disposables} from callbacks and other types.
2121
*/
2222
package io.reactivex.rxjava3.disposables;

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableFromAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public CompletableFromAction(Action run) {
2929

3030
@Override
3131
protected void subscribeActual(CompletableObserver observer) {
32-
Disposable d = Disposables.empty();
32+
Disposable d = Disposable.empty();
3333
observer.onSubscribe(d);
3434
try {
3535
run.run();

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableFromCallable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public CompletableFromCallable(Callable<?> callable) {
3030

3131
@Override
3232
protected void subscribeActual(CompletableObserver observer) {
33-
Disposable d = Disposables.empty();
33+
Disposable d = Disposable.empty();
3434
observer.onSubscribe(d);
3535
try {
3636
callable.call();

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableFromRunnable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public CompletableFromRunnable(Runnable runnable) {
2828

2929
@Override
3030
protected void subscribeActual(CompletableObserver observer) {
31-
Disposable d = Disposables.empty();
31+
Disposable d = Disposable.empty();
3232
observer.onSubscribe(d);
3333
try {
3434
runnable.run();

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableFromSupplier.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CompletableFromSupplier(Supplier<?> supplier) {
3333

3434
@Override
3535
protected void subscribeActual(CompletableObserver observer) {
36-
Disposable d = Disposables.empty();
36+
Disposable d = Disposable.empty();
3737
observer.onSubscribe(d);
3838
try {
3939
supplier.get();

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeError.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package io.reactivex.rxjava3.internal.operators.maybe;
1515

1616
import io.reactivex.rxjava3.core.*;
17-
import io.reactivex.rxjava3.disposables.Disposables;
17+
import io.reactivex.rxjava3.disposables.Disposable;
1818

1919
/**
2020
* Signals a constant Throwable.
@@ -31,7 +31,7 @@ public MaybeError(Throwable error) {
3131

3232
@Override
3333
protected void subscribeActual(MaybeObserver<? super T> observer) {
34-
observer.onSubscribe(Disposables.disposed());
34+
observer.onSubscribe(Disposable.disposed());
3535
observer.onError(error);
3636
}
3737
}

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeErrorCallable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package io.reactivex.rxjava3.internal.operators.maybe;
1515

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

3535
@Override
3636
protected void subscribeActual(MaybeObserver<? super T> observer) {
37-
observer.onSubscribe(Disposables.disposed());
37+
observer.onSubscribe(Disposable.disposed());
3838
Throwable ex;
3939

4040
try {

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MaybeFromAction(Action action) {
3434

3535
@Override
3636
protected void subscribeActual(MaybeObserver<? super T> observer) {
37-
Disposable d = Disposables.empty();
37+
Disposable d = Disposable.empty();
3838
observer.onSubscribe(d);
3939

4040
if (!d.isDisposed()) {

0 commit comments

Comments
 (0)