| 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.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.internal.util.*; |
| 23 |
|
| 24 |
/** |
| 25 |
* Ensures that the event flow between the upstream and downstream follow |
| 26 |
* the Reactive-Streams 1.0 specification by honoring the 3 additional rules |
| 27 |
* (which are omitted in standard operators due to performance reasons). |
| 28 |
* <ul> |
| 29 |
* <li>§1.3: onNext should not be called concurrently until onSubscribe returns</li> |
| 30 |
* <li>§2.3: onError or onComplete must not call cancel</li> |
| 31 |
* <li>§3.9: negative requests should emit an onError(IllegalArgumentException)</li> |
| 32 |
* </ul> |
| 33 |
* In addition, if rule §2.12 (onSubscribe must be called at most once) is violated, |
| 34 |
* the sequence is cancelled an onError(IllegalStateException) is emitted. |
| 35 |
* @param <T> the value type |
| 36 |
* @since 2.0.7 |
| 37 |
*/ |
| 38 |
public class StrictSubscriber<T> |
| 39 |
extends AtomicInteger |
| 40 |
implements FlowableSubscriber<T>, Subscription { |
| 41 |
|
| 42 |
private static final long serialVersionUID = -4945028590049415624L; |
| 43 |
|
| 44 |
final Subscriber<? super T> downstream; |
| 45 |
|
| 46 |
final AtomicThrowable error; |
| 47 |
|
| 48 |
final AtomicLong requested; |
| 49 |
|
| 50 |
final AtomicReference<Subscription> upstream; |
| 51 |
|
| 52 |
final AtomicBoolean once; |
| 53 |
|
| 54 |
volatile boolean done; |
| 55 |
|
| 56 |
public StrictSubscriber(Subscriber<? super T> downstream) { |
| 57 |
this.downstream = downstream; |
| 58 |
this.error = new AtomicThrowable(); |
| 59 |
this.requested = new AtomicLong(); |
| 60 |
this.upstream = new AtomicReference<>(); |
| 61 |
this.once = new AtomicBoolean(); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void request(long n) { |
| 66 |
if (n <= 0) { |
| 67 |
cancel(); |
| 68 |
onError(new IllegalArgumentException("§3.9 violated: positive request amount required but it was " + n)); |
| 69 |
} else { |
| 70 |
SubscriptionHelper.deferredRequest(upstream, requested, n); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void cancel() { |
| 76 |
if (!done) { |
| 77 |
SubscriptionHelper.cancel(upstream); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onSubscribe(Subscription s) { |
| 83 |
if (once.compareAndSet(false, true)) { |
| 84 |
|
| 85 |
downstream.onSubscribe(this); |
| 86 |
|
| 87 |
SubscriptionHelper.deferredSetOnce(this.upstream, requested, s); |
| 88 |
} else { |
| 89 |
s.cancel(); |
| 90 |
cancel(); |
| 91 |
onError(new IllegalStateException("§2.12 violated: onSubscribe must be called at most once")); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onNext(T t) { |
| 97 |
HalfSerializer.onNext(downstream, t, this, error); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onError(Throwable t) { |
| 102 |
done = true; |
| 103 |
HalfSerializer.onError(downstream, t, this, error); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onComplete() { |
| 108 |
done = true; |
| 109 |
HalfSerializer.onComplete(downstream, this, error); |
| 110 |
} |
| 111 |
} |