Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTakeLast.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.ArrayDeque;
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
25 public final class FlowableTakeLast<T> extends AbstractFlowableWithUpstream<T, T> {
26 final int count;
27
28 public FlowableTakeLast(Flowable<T> source, int count) {
29 super(source);
30 this.count = count;
31 }
32
33 @Override
34 protected void subscribeActual(Subscriber<? super T> s) {
35 source.subscribe(new TakeLastSubscriber<>(s, count));
36 }
37
38 static final class TakeLastSubscriber<T> extends ArrayDeque<T> implements FlowableSubscriber<T>, Subscription {
39
40 private static final long serialVersionUID = 7240042530241604978L;
41 final Subscriber<? super T> downstream;
42 final int count;
43
44 Subscription upstream;
45 volatile boolean done;
46 volatile boolean cancelled;
47
48 final AtomicLong requested = new AtomicLong();
49
50 final AtomicInteger wip = new AtomicInteger();
51
52 TakeLastSubscriber(Subscriber<? super T> actual, int count) {
53 this.downstream = actual;
54 this.count = count;
55 }
56
57 @Override
58 public void onSubscribe(Subscription s) {
59 if (SubscriptionHelper.validate(this.upstream, s)) {
60 this.upstream = s;
61 downstream.onSubscribe(this);
62 s.request(Long.MAX_VALUE);
63 }
64 }
65
66 @Override
67 public void onNext(T t) {
68 if (count == size()) {
69 poll();
70 }
71 offer(t);
72 }
73
74 @Override
75 public void onError(Throwable t) {
76 downstream.onError(t);
77 }
78
79 @Override
80 public void onComplete() {
81 done = true;
82 drain();
83 }
84
85 @Override
86 public void request(long n) {
87 if (SubscriptionHelper.validate(n)) {
88 BackpressureHelper.add(requested, n);
89 drain();
90 }
91 }
92
93 @Override
94 public void cancel() {
95 cancelled = true;
96 upstream.cancel();
97 }
98
99 void drain() {
100 if (wip.getAndIncrement() == 0) {
101 Subscriber<? super T> a = downstream;
102 long r = requested.get();
103 do {
104 if (cancelled) {
105 return;
106 }
107 if (done) {
108 long e = 0L;
109
110 while (e != r) {
111 if (cancelled) {
112 return;
113 }
114 T v = poll();
115 if (v == null) {
116 a.onComplete();
117 return;
118 }
119 a.onNext(v);
120 e++;
121 }
122 if (isEmpty()) {
123 a.onComplete();
124 return;
125 }
126 if (e != 0L) {
127 r = BackpressureHelper.produced(requested, e);
128 }
129 }
130 } while (wip.decrementAndGet() != 0);
131 }
132 }
133 }
134 }