| 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.subscribers; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
|
| 25 |
public final class SubscriberResourceWrapper<T> extends AtomicReference<Disposable> implements FlowableSubscriber<T>, Disposable, Subscription { |
| 26 |
|
| 27 |
private static final long serialVersionUID = -8612022020200669122L; |
| 28 |
|
| 29 |
final Subscriber<? super T> downstream; |
| 30 |
|
| 31 |
final AtomicReference<Subscription> upstream = new AtomicReference<>(); |
| 32 |
|
| 33 |
public SubscriberResourceWrapper(Subscriber<? super T> downstream) { |
| 34 |
this.downstream = downstream; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void onSubscribe(Subscription s) { |
| 39 |
if (SubscriptionHelper.setOnce(upstream, s)) { |
| 40 |
downstream.onSubscribe(this); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void onNext(T t) { |
| 46 |
downstream.onNext(t); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void onError(Throwable t) { |
| 51 |
DisposableHelper.dispose(this); |
| 52 |
downstream.onError(t); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void onComplete() { |
| 57 |
DisposableHelper.dispose(this); |
| 58 |
downstream.onComplete(); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void request(long n) { |
| 63 |
if (SubscriptionHelper.validate(n)) { |
| 64 |
upstream.get().request(n); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void dispose() { |
| 70 |
SubscriptionHelper.cancel(upstream); |
| 71 |
|
| 72 |
DisposableHelper.dispose(this); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isDisposed() { |
| 77 |
return upstream.get() == SubscriptionHelper.CANCELLED; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void cancel() { |
| 82 |
dispose(); |
| 83 |
} |
| 84 |
|
| 85 |
public void setResource(Disposable resource) { |
| 86 |
DisposableHelper.set(this, resource); |
| 87 |
} |
| 88 |
} |