| 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.subscriptions; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscription; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 22 |
|
| 23 |
/** |
| 24 |
* A subscription implementation that arbitrates exactly one other Subscription and can |
| 25 |
* hold a single disposable resource. |
| 26 |
* |
| 27 |
* <p>All methods are thread-safe. |
| 28 |
*/ |
| 29 |
public final class AsyncSubscription extends AtomicLong implements Subscription, Disposable { |
| 30 |
|
| 31 |
private static final long serialVersionUID = 7028635084060361255L; |
| 32 |
|
| 33 |
final AtomicReference<Subscription> actual; |
| 34 |
|
| 35 |
final AtomicReference<Disposable> resource; |
| 36 |
|
| 37 |
public AsyncSubscription() { |
| 38 |
resource = new AtomicReference<>(); |
| 39 |
actual = new AtomicReference<>(); |
| 40 |
} |
| 41 |
|
| 42 |
public AsyncSubscription(Disposable resource) { |
| 43 |
this(); |
| 44 |
this.resource.lazySet(resource); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void request(long n) { |
| 49 |
SubscriptionHelper.deferredRequest(actual, this, n); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void cancel() { |
| 54 |
dispose(); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void dispose() { |
| 59 |
SubscriptionHelper.cancel(actual); |
| 60 |
DisposableHelper.dispose(resource); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public boolean isDisposed() { |
| 65 |
return actual.get() == SubscriptionHelper.CANCELLED; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sets a new resource and disposes the currently held resource. |
| 70 |
* @param r the new resource to set |
| 71 |
* @return false if this AsyncSubscription has been cancelled/disposed |
| 72 |
* @see #replaceResource(Disposable) |
| 73 |
*/ |
| 74 |
public boolean setResource(Disposable r) { |
| 75 |
return DisposableHelper.set(resource, r); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Replaces the currently held resource with the given new one without disposing the old. |
| 80 |
* @param r the new resource to set |
| 81 |
* @return false if this AsyncSubscription has been cancelled/disposed |
| 82 |
*/ |
| 83 |
public boolean replaceResource(Disposable r) { |
| 84 |
return DisposableHelper.replace(resource, r); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Sets the given subscription if there isn't any subscription held. |
| 89 |
* @param s the first and only subscription to set |
| 90 |
*/ |
| 91 |
public void setSubscription(Subscription s) { |
| 92 |
SubscriptionHelper.deferredSetOnce(actual, this, s); |
| 93 |
} |
| 94 |
} |