Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/BoundedSubscriber.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.AtomicReference;
17
18 import org.reactivestreams.Subscription;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.*;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.functions.Functions;
25 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
26 import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28
29 public final class BoundedSubscriber<T> extends AtomicReference<Subscription>
30 implements FlowableSubscriber<T>, Subscription, Disposable, LambdaConsumerIntrospection {
31
32 private static final long serialVersionUID = -7251123623727029452L;
33 final Consumer<? super T> onNext;
34 final Consumer<? super Throwable> onError;
35 final Action onComplete;
36 final Consumer<? super Subscription> onSubscribe;
37
38 final int bufferSize;
39 int consumed;
40 final int limit;
41
42 public BoundedSubscriber(Consumer<? super T> onNext, Consumer<? super Throwable> onError,
43 Action onComplete, Consumer<? super Subscription> onSubscribe, int bufferSize) {
44 super();
45 this.onNext = onNext;
46 this.onError = onError;
47 this.onComplete = onComplete;
48 this.onSubscribe = onSubscribe;
49 this.bufferSize = bufferSize;
50 this.limit = bufferSize - (bufferSize >> 2);
51 }
52
53 @Override
54 public void onSubscribe(Subscription s) {
55 if (SubscriptionHelper.setOnce(this, s)) {
56 try {
57 onSubscribe.accept(this);
58 } catch (Throwable e) {
59 Exceptions.throwIfFatal(e);
60 s.cancel();
61 onError(e);
62 }
63 }
64 }
65
66 @Override
67 public void onNext(T t) {
68 if (!isDisposed()) {
69 try {
70 onNext.accept(t);
71
72 int c = consumed + 1;
73 if (c == limit) {
74 consumed = 0;
75 get().request(limit);
76 } else {
77 consumed = c;
78 }
79 } catch (Throwable e) {
80 Exceptions.throwIfFatal(e);
81 get().cancel();
82 onError(e);
83 }
84 }
85 }
86
87 @Override
88 public void onError(Throwable t) {
89 if (get() != SubscriptionHelper.CANCELLED) {
90 lazySet(SubscriptionHelper.CANCELLED);
91 try {
92 onError.accept(t);
93 } catch (Throwable e) {
94 Exceptions.throwIfFatal(e);
95 RxJavaPlugins.onError(new CompositeException(t, e));
96 }
97 } else {
98 RxJavaPlugins.onError(t);
99 }
100 }
101
102 @Override
103 public void onComplete() {
104 if (get() != SubscriptionHelper.CANCELLED) {
105 lazySet(SubscriptionHelper.CANCELLED);
106 try {
107 onComplete.run();
108 } catch (Throwable e) {
109 Exceptions.throwIfFatal(e);
110 RxJavaPlugins.onError(e);
111 }
112 }
113 }
114
115 @Override
116 public void dispose() {
117 cancel();
118 }
119
120 @Override
121 public boolean isDisposed() {
122 return get() == SubscriptionHelper.CANCELLED;
123 }
124
125 @Override
126 public void request(long n) {
127 get().request(n);
128 }
129
130 @Override
131 public void cancel() {
132 SubscriptionHelper.cancel(this);
133 }
134
135 @Override
136 public boolean hasCustomOnError() {
137 return onError != Functions.ON_ERROR_MISSING;
138 }
139 }