Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSkipLastTimed.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.operators.flowable;
15
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
24 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
25
26 public final class FlowableSkipLastTimed<T> extends AbstractFlowableWithUpstream<T, T> {
27 final long time;
28 final TimeUnit unit;
29 final Scheduler scheduler;
30 final int bufferSize;
31 final boolean delayError;
32
33 public FlowableSkipLastTimed(Flowable<T> source, long time, TimeUnit unit, Scheduler scheduler, int bufferSize, boolean delayError) {
34 super(source);
35 this.time = time;
36 this.unit = unit;
37 this.scheduler = scheduler;
38 this.bufferSize = bufferSize;
39 this.delayError = delayError;
40 }
41
42 @Override
43 protected void subscribeActual(Subscriber<? super T> s) {
44 source.subscribe(new SkipLastTimedSubscriber<>(s, time, unit, scheduler, bufferSize, delayError));
45 }
46
47 static final class SkipLastTimedSubscriber<T> extends AtomicInteger implements FlowableSubscriber<T>, Subscription {
48
49 private static final long serialVersionUID = -5677354903406201275L;
50 final Subscriber<? super T> downstream;
51 final long time;
52 final TimeUnit unit;
53 final Scheduler scheduler;
54 final SpscLinkedArrayQueue<Object> queue;
55 final boolean delayError;
56
57 Subscription upstream;
58
59 final AtomicLong requested = new AtomicLong();
60
61 volatile boolean cancelled;
62
63 volatile boolean done;
64 Throwable error;
65
66 SkipLastTimedSubscriber(Subscriber<? super T> actual, long time, TimeUnit unit, Scheduler scheduler, int bufferSize, boolean delayError) {
67 this.downstream = actual;
68 this.time = time;
69 this.unit = unit;
70 this.scheduler = scheduler;
71 this.queue = new SpscLinkedArrayQueue<>(bufferSize);
72 this.delayError = delayError;
73 }
74
75 @Override
76 public void onSubscribe(Subscription s) {
77 if (SubscriptionHelper.validate(this.upstream, s)) {
78 this.upstream = s;
79 downstream.onSubscribe(this);
80 s.request(Long.MAX_VALUE);
81 }
82 }
83
84 @Override
85 public void onNext(T t) {
86 long now = scheduler.now(unit);
87
88 queue.offer(now, t);
89
90 drain();
91 }
92
93 @Override
94 public void onError(Throwable t) {
95 error = t;
96 done = true;
97 drain();
98 }
99
100 @Override
101 public void onComplete() {
102 done = true;
103 drain();
104 }
105
106 @Override
107 public void request(long n) {
108 if (SubscriptionHelper.validate(n)) {
109 BackpressureHelper.add(requested, n);
110 drain();
111 }
112 }
113
114 @Override
115 public void cancel() {
116 if (!cancelled) {
117 cancelled = true;
118 upstream.cancel();
119
120 if (getAndIncrement() == 0) {
121 queue.clear();
122 }
123 }
124 }
125
126 void drain() {
127 if (getAndIncrement() != 0) {
128 return;
129 }
130
131 int missed = 1;
132
133 final Subscriber<? super T> a = downstream;
134 final SpscLinkedArrayQueue<Object> q = queue;
135 final boolean delayError = this.delayError;
136 final TimeUnit unit = this.unit;
137 final Scheduler scheduler = this.scheduler;
138 final long time = this.time;
139
140 for (;;) {
141
142 long r = requested.get();
143 long e = 0L;
144
145 while (e != r) {
146 boolean d = done;
147
148 Long ts = (Long)q.peek();
149
150 boolean empty = ts == null;
151
152 long now = scheduler.now(unit);
153
154 if (!empty && ts > now - time) {
155 empty = true;
156 }
157
158 if (checkTerminated(d, empty, a, delayError)) {
159 return;
160 }
161
162 if (empty) {
163 break;
164 }
165
166 q.poll();
167 @SuppressWarnings("unchecked")
168 T v = (T)q.poll();
169
170 a.onNext(v);
171
172 e++;
173 }
174
175 if (e != 0L) {
176 BackpressureHelper.produced(requested, e);
177 }
178
179 missed = addAndGet(-missed);
180 if (missed == 0) {
181 break;
182 }
183 }
184 }
185
186 boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> a, boolean delayError) {
187 if (cancelled) {
188 queue.clear();
189 return true;
190 }
191 if (d) {
192 if (delayError) {
193 if (empty) {
194 Throwable e = error;
195 if (e != null) {
196 a.onError(e);
197 } else {
198 a.onComplete();
199 }
200 return true;
201 }
202 } else {
203 Throwable e = error;
204 if (e != null) {
205 queue.clear();
206 a.onError(e);
207 return true;
208 } else
209 if (empty) {
210 a.onComplete();
211 return true;
212 }
213 }
214 }
215 return false;
216 }
217 }
218 }