Debian Codegraph @

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