Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTake.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.atomic.AtomicLong;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.internal.subscriptions.*;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class FlowableTake<T> extends AbstractFlowableWithUpstream<T, T> {
25
26 final long n;
27
28 public FlowableTake(Flowable<T> source, long n) {
29 super(source);
30 this.n = n;
31 }
32
33 @Override
34 protected void subscribeActual(Subscriber<? super T> s) {
35 source.subscribe(new TakeSubscriber<>(s, n));
36 }
37
38 static final class TakeSubscriber<T>
39 extends AtomicLong
40 implements FlowableSubscriber<T>, Subscription {
41
42 private static final long serialVersionUID = 2288246011222124525L;
43
44 final Subscriber<? super T> downstream;
45
46 long remaining;
47
48 Subscription upstream;
49
50 TakeSubscriber(Subscriber<? super T> actual, long remaining) {
51 this.downstream = actual;
52 this.remaining = remaining;
53 lazySet(remaining);
54 }
55
56 @Override
57 public void onSubscribe(Subscription s) {
58 if (SubscriptionHelper.validate(this.upstream, s)) {
59 if (remaining == 0L) {
60 s.cancel();
61 EmptySubscription.complete(downstream);
62 } else {
63 this.upstream = s;
64 downstream.onSubscribe(this);
65 }
66 }
67 }
68
69 @Override
70 public void onNext(T t) {
71 long r = remaining;
72 if (r > 0L) {
73 remaining = --r;
74 downstream.onNext(t);
75 if (r == 0L) {
76 upstream.cancel();
77 downstream.onComplete();
78 }
79 }
80 }
81
82 @Override
83 public void onError(Throwable t) {
84 if (remaining > 0L) {
85 remaining = 0L;
86 downstream.onError(t);
87 } else {
88 RxJavaPlugins.onError(t);
89 }
90 }
91
92 @Override
93 public void onComplete() {
94 if (remaining > 0L) {
95 remaining = 0L;
96 downstream.onComplete();
97 }
98 }
99
100 @Override
101 public void request(long n) {
102 if (SubscriptionHelper.validate(n)) {
103 for (;;) {
104 long r = get();
105 if (r == 0L) {
106 break;
107 }
108 long toRequest = Math.min(r, n);
109 long u = r - toRequest;
110 if (compareAndSet(r, u)) {
111 upstream.request(toRequest);
112 break;
113 }
114 }
115 }
116 }
117
118 @Override
119 public void cancel() {
120 upstream.cancel();
121 }
122
123 }
124 }