Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableElementAtMaybe.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 org.reactivestreams.Subscription;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class FlowableElementAtMaybe<T> extends Maybe<T> implements FuseToFlowable<T> {
25 final Flowable<T> source;
26
27 final long index;
28
29 public FlowableElementAtMaybe(Flowable<T> source, long index) {
30 this.source = source;
31 this.index = index;
32 }
33
34 @Override
35 protected void subscribeActual(MaybeObserver<? super T> observer) {
36 source.subscribe(new ElementAtSubscriber<>(observer, index));
37 }
38
39 @Override
40 public Flowable<T> fuseToFlowable() {
41 return RxJavaPlugins.onAssembly(new FlowableElementAt<>(source, index, null, false));
42 }
43
44 static final class ElementAtSubscriber<T> implements FlowableSubscriber<T>, Disposable {
45
46 final MaybeObserver<? super T> downstream;
47
48 final long index;
49
50 Subscription upstream;
51
52 long count;
53
54 boolean done;
55
56 ElementAtSubscriber(MaybeObserver<? super T> actual, long index) {
57 this.downstream = actual;
58 this.index = index;
59 }
60
61 @Override
62 public void onSubscribe(Subscription s) {
63 if (SubscriptionHelper.validate(this.upstream, s)) {
64 this.upstream = s;
65 downstream.onSubscribe(this);
66 s.request(index + 1);
67 }
68 }
69
70 @Override
71 public void onNext(T t) {
72 if (done) {
73 return;
74 }
75 long c = count;
76 if (c == index) {
77 done = true;
78 upstream.cancel();
79 upstream = SubscriptionHelper.CANCELLED;
80 downstream.onSuccess(t);
81 return;
82 }
83 count = c + 1;
84 }
85
86 @Override
87 public void onError(Throwable t) {
88 if (done) {
89 RxJavaPlugins.onError(t);
90 return;
91 }
92 done = true;
93 upstream = SubscriptionHelper.CANCELLED;
94 downstream.onError(t);
95 }
96
97 @Override
98 public void onComplete() {
99 upstream = SubscriptionHelper.CANCELLED;
100 if (!done) {
101 done = true;
102 downstream.onComplete();
103 }
104 }
105
106 @Override
107 public void dispose() {
108 upstream.cancel();
109 upstream = SubscriptionHelper.CANCELLED;
110 }
111
112 @Override
113 public boolean isDisposed() {
114 return upstream == SubscriptionHelper.CANCELLED;
115 }
116
117 }
118 }