Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableElementAtSingle.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.observable;
15
16 import java.util.NoSuchElementException;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.fuseable.FuseToObservable;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class ObservableElementAtSingle<T> extends Single<T> implements FuseToObservable<T> {
25 final ObservableSource<T> source;
26 final long index;
27 final T defaultValue;
28
29 public ObservableElementAtSingle(ObservableSource<T> source, long index, T defaultValue) {
30 this.source = source;
31 this.index = index;
32 this.defaultValue = defaultValue;
33 }
34
35 @Override
36 public void subscribeActual(SingleObserver<? super T> t) {
37 source.subscribe(new ElementAtObserver<>(t, index, defaultValue));
38 }
39
40 @Override
41 public Observable<T> fuseToObservable() {
42 return RxJavaPlugins.onAssembly(new ObservableElementAt<>(source, index, defaultValue, true));
43 }
44
45 static final class ElementAtObserver<T> implements Observer<T>, Disposable {
46 final SingleObserver<? super T> downstream;
47 final long index;
48 final T defaultValue;
49
50 Disposable upstream;
51
52 long count;
53
54 boolean done;
55
56 ElementAtObserver(SingleObserver<? super T> actual, long index, T defaultValue) {
57 this.downstream = actual;
58 this.index = index;
59 this.defaultValue = defaultValue;
60 }
61
62 @Override
63 public void onSubscribe(Disposable d) {
64 if (DisposableHelper.validate(this.upstream, d)) {
65 this.upstream = d;
66 downstream.onSubscribe(this);
67 }
68 }
69
70 @Override
71 public void dispose() {
72 upstream.dispose();
73 }
74
75 @Override
76 public boolean isDisposed() {
77 return upstream.isDisposed();
78 }
79
80 @Override
81 public void onNext(T t) {
82 if (done) {
83 return;
84 }
85 long c = count;
86 if (c == index) {
87 done = true;
88 upstream.dispose();
89 downstream.onSuccess(t);
90 return;
91 }
92 count = c + 1;
93 }
94
95 @Override
96 public void onError(Throwable t) {
97 if (done) {
98 RxJavaPlugins.onError(t);
99 return;
100 }
101 done = true;
102 downstream.onError(t);
103 }
104
105 @Override
106 public void onComplete() {
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 }