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