| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.disposables; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 17 |
import io.reactivex.rxjava3.functions.Action; |
| 18 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 19 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 20 |
import org.reactivestreams.Subscription; |
| 21 |
|
| 22 |
import java.util.Objects; |
| 23 |
import java.util.concurrent.Future; |
| 24 |
|
| 25 |
/** |
| 26 |
* Represents a disposable resource. |
| 27 |
*/ |
| 28 |
public interface Disposable { |
| 29 |
/** |
| 30 |
* Dispose the resource, the operation should be idempotent. |
| 31 |
*/ |
| 32 |
void dispose(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns true if this resource has been disposed. |
| 36 |
* @return true if this resource has been disposed |
| 37 |
*/ |
| 38 |
boolean isDisposed(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Construct a {@code Disposable} by wrapping a {@link Runnable} that is |
| 42 |
* executed exactly once when the {@code Disposable} is disposed. |
| 43 |
* @param run the Runnable to wrap |
| 44 |
* @return the new Disposable instance |
| 45 |
* @throws NullPointerException if {@code run} is {@code null} |
| 46 |
* @since 3.0.0 |
| 47 |
*/ |
| 48 |
@NonNull |
| 49 |
static Disposable fromRunnable(@NonNull Runnable run) { |
| 50 |
Objects.requireNonNull(run, "run is null"); |
| 51 |
return new RunnableDisposable(run); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Construct a {@code Disposable} by wrapping a {@link Action} that is |
| 56 |
* executed exactly once when the {@code Disposable} is disposed. |
| 57 |
* @param action the Action to wrap |
| 58 |
* @return the new Disposable instance |
| 59 |
* @throws NullPointerException if {@code action} is {@code null} |
| 60 |
* @since 3.0.0 |
| 61 |
*/ |
| 62 |
@NonNull |
| 63 |
static Disposable fromAction(@NonNull Action action) { |
| 64 |
Objects.requireNonNull(action, "action is null"); |
| 65 |
return new ActionDisposable(action); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Construct a {@code Disposable} by wrapping a {@link Future} that is |
| 70 |
* cancelled exactly once when the {@code Disposable} is disposed. |
| 71 |
* <p> |
| 72 |
* The {@code Future} is cancelled with {@code mayInterruptIfRunning == true}. |
| 73 |
* @param future the Future to wrap |
| 74 |
* @return the new Disposable instance |
| 75 |
* @throws NullPointerException if {@code future} is {@code null} |
| 76 |
* @see #fromFuture(Future, boolean) |
| 77 |
* @since 3.0.0 |
| 78 |
*/ |
| 79 |
@NonNull |
| 80 |
static Disposable fromFuture(@NonNull Future<?> future) { |
| 81 |
Objects.requireNonNull(future, "future is null"); |
| 82 |
return fromFuture(future, true); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Construct a {@code Disposable} by wrapping a {@link Future} that is |
| 87 |
* cancelled exactly once when the {@code Disposable} is disposed. |
| 88 |
* @param future the Future to wrap |
| 89 |
* @param allowInterrupt if true, the future cancel happens via {@code Future.cancel(true)} |
| 90 |
* @return the new Disposable instance |
| 91 |
* @throws NullPointerException if {@code future} is {@code null} |
| 92 |
* @since 3.0.0 |
| 93 |
*/ |
| 94 |
@NonNull |
| 95 |
static Disposable fromFuture(@NonNull Future<?> future, boolean allowInterrupt) { |
| 96 |
Objects.requireNonNull(future, "future is null"); |
| 97 |
return new FutureDisposable(future, allowInterrupt); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Construct a {@code Disposable} by wrapping a {@link Subscription} that is |
| 102 |
* cancelled exactly once when the {@code Disposable} is disposed. |
| 103 |
* @param subscription the Runnable to wrap |
| 104 |
* @return the new Disposable instance |
| 105 |
* @throws NullPointerException if {@code subscription} is {@code null} |
| 106 |
* @since 3.0.0 |
| 107 |
*/ |
| 108 |
@NonNull |
| 109 |
static Disposable fromSubscription(@NonNull Subscription subscription) { |
| 110 |
Objects.requireNonNull(subscription, "subscription is null"); |
| 111 |
return new SubscriptionDisposable(subscription); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Construct a {@code Disposable} by wrapping an {@link AutoCloseable} that is |
| 116 |
* closed exactly once when the {@code Disposable} is disposed. |
| 117 |
* @param autoCloseable the AutoCloseable to wrap |
| 118 |
* @return the new Disposable instance |
| 119 |
* @throws NullPointerException if {@code autoCloseable} is {@code null} |
| 120 |
* @since 3.0.0 |
| 121 |
*/ |
| 122 |
@NonNull |
| 123 |
static Disposable fromAutoCloseable(@NonNull AutoCloseable autoCloseable) { |
| 124 |
Objects.requireNonNull(autoCloseable, "autoCloseable is null"); |
| 125 |
return new AutoCloseableDisposable(autoCloseable); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Construct an {@link AutoCloseable} by wrapping a {@code Disposable} that is |
| 130 |
* disposed when the returned {@code AutoCloseable} is closed. |
| 131 |
* @param disposable the Disposable instance |
| 132 |
* @return the new AutoCloseable instance |
| 133 |
* @throws NullPointerException if {@code disposable} is {@code null} |
| 134 |
* @since 3.0.0 |
| 135 |
*/ |
| 136 |
@NonNull |
| 137 |
static AutoCloseable toAutoCloseable(@NonNull Disposable disposable) { |
| 138 |
Objects.requireNonNull(disposable, "disposable is null"); |
| 139 |
return disposable::dispose; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns a new, non-disposed {@code Disposable} instance. |
| 144 |
* @return a new, non-disposed {@code Disposable} instance |
| 145 |
* @since 3.0.0 |
| 146 |
*/ |
| 147 |
@NonNull |
| 148 |
static Disposable empty() { |
| 149 |
return fromRunnable(Functions.EMPTY_RUNNABLE); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns a shared, disposed {@code Disposable} instance. |
| 154 |
* @return a shared, disposed {@code Disposable} instance |
| 155 |
* @since 3.0.0 |
| 156 |
*/ |
| 157 |
@NonNull |
| 158 |
static Disposable disposed() { |
| 159 |
return EmptyDisposable.INSTANCE; |
| 160 |
} |
| 161 |
} |