Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/SinglePostCompleteSubscriber.java

raw
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.AtomicLong;
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.BackpressureHelper;
23
24 /**
25 * Relays signals from upstream according to downstream requests and allows
26 * signalling a final value followed by onComplete in a backpressure-aware manner.
27 *
28 * @param <T> the input value type
29 * @param <R> the output value type
30 */
31 public abstract class SinglePostCompleteSubscriber<T, R> extends AtomicLong implements FlowableSubscriber<T>, Subscription {
32 private static final long serialVersionUID = 7917814472626990048L;
33
34 /** The downstream consumer. */
35 protected final Subscriber<? super R> downstream;
36
37 /** The upstream subscription. */
38 protected Subscription upstream;
39
40 /** The last value stored in case there is no request for it. */
41 protected R value;
42
43 /** Number of values emitted so far. */
44 protected long produced;
45
46 /** Masks out the 2^63 bit indicating a completed state. */
47 static final long COMPLETE_MASK = Long.MIN_VALUE;
48 /** Masks out the lower 63 bit holding the current request amount. */
49 static final long REQUEST_MASK = Long.MAX_VALUE;
50
51 public SinglePostCompleteSubscriber(Subscriber<? super R> downstream) {
52 this.downstream = downstream;
53 }
54
55 @Override
56 public void onSubscribe(Subscription s) {
57 if (SubscriptionHelper.validate(this.upstream, s)) {
58 this.upstream = s;
59 downstream.onSubscribe(this);
60 }
61 }
62
63 /**
64 * Signals the given value and an onComplete if the downstream is ready to receive the final value.
65 * @param n the value to emit
66 */
67 protected final void complete(R n) {
68 long p = produced;
69 if (p != 0) {
70 BackpressureHelper.produced(this, p);
71 }
72
73 for (;;) {
74 long r = get();
75 if ((r & COMPLETE_MASK) != 0) {
76 onDrop(n);
77 return;
78 }
79 if ((r & REQUEST_MASK) != 0) {
80 lazySet(COMPLETE_MASK + 1);
81 downstream.onNext(n);
82 downstream.onComplete();
83 return;
84 }
85 value = n;
86 if (compareAndSet(0, COMPLETE_MASK)) {
87 return;
88 }
89 value = null;
90 }
91 }
92
93 /**
94 * Called in case of multiple calls to complete.
95 * @param n the value dropped
96 */
97 protected void onDrop(R n) {
98 // default is no-op
99 }
100
101 @Override
102 public final void request(long n) {
103 if (SubscriptionHelper.validate(n)) {
104 for (;;) {
105 long r = get();
106 if ((r & COMPLETE_MASK) != 0) {
107 if (compareAndSet(COMPLETE_MASK, COMPLETE_MASK + 1)) {
108 downstream.onNext(value);
109 downstream.onComplete();
110 }
111 break;
112 }
113 long u = BackpressureHelper.addCap(r, n);
114 if (compareAndSet(r, u)) {
115 upstream.request(n);
116 break;
117 }
118 }
119 }
120 }
121
122 @Override
123 public void cancel() {
124 upstream.cancel();
125 }
126 }