| 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.internal.disposables; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 19 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.functions.Cancellable; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* A disposable container that wraps a Cancellable instance. |
| 25 |
* <p> |
| 26 |
* Watch out for the AtomicReference API leak! |
| 27 |
*/ |
| 28 |
public final class CancellableDisposable extends AtomicReference<Cancellable> |
| 29 |
implements Disposable { |
| 30 |
|
| 31 |
private static final long serialVersionUID = 5718521705281392066L; |
| 32 |
|
| 33 |
public CancellableDisposable(Cancellable cancellable) { |
| 34 |
super(cancellable); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public boolean isDisposed() { |
| 39 |
return get() == null; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void dispose() { |
| 44 |
if (get() != null) { |
| 45 |
Cancellable c = getAndSet(null); |
| 46 |
if (c != null) { |
| 47 |
try { |
| 48 |
c.cancel(); |
| 49 |
} catch (Throwable ex) { |
| 50 |
Exceptions.throwIfFatal(ex); |
| 51 |
RxJavaPlugins.onError(ex); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |