| 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.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class FlowableElementAt<T> extends AbstractFlowableWithUpstream<T, T> { |
| 25 |
final long index; |
| 26 |
final T defaultValue; |
| 27 |
final boolean errorOnFewer; |
| 28 |
|
| 29 |
public FlowableElementAt(Flowable<T> source, long index, T defaultValue, boolean errorOnFewer) { |
| 30 |
super(source); |
| 31 |
this.index = index; |
| 32 |
this.defaultValue = defaultValue; |
| 33 |
this.errorOnFewer = errorOnFewer; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 38 |
source.subscribe(new ElementAtSubscriber<>(s, index, defaultValue, errorOnFewer)); |
| 39 |
} |
| 40 |
|
| 41 |
static final class ElementAtSubscriber<T> extends DeferredScalarSubscription<T> implements FlowableSubscriber<T> { |
| 42 |
|
| 43 |
private static final long serialVersionUID = 4066607327284737757L; |
| 44 |
|
| 45 |
final long index; |
| 46 |
final T defaultValue; |
| 47 |
final boolean errorOnFewer; |
| 48 |
|
| 49 |
Subscription upstream; |
| 50 |
|
| 51 |
long count; |
| 52 |
|
| 53 |
boolean done; |
| 54 |
|
| 55 |
ElementAtSubscriber(Subscriber<? super T> actual, long index, T defaultValue, boolean errorOnFewer) { |
| 56 |
super(actual); |
| 57 |
this.index = index; |
| 58 |
this.defaultValue = defaultValue; |
| 59 |
this.errorOnFewer = errorOnFewer; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onSubscribe(Subscription s) { |
| 64 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 65 |
this.upstream = s; |
| 66 |
downstream.onSubscribe(this); |
| 67 |
s.request(Long.MAX_VALUE); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onNext(T t) { |
| 73 |
if (done) { |
| 74 |
return; |
| 75 |
} |
| 76 |
long c = count; |
| 77 |
if (c == index) { |
| 78 |
done = true; |
| 79 |
upstream.cancel(); |
| 80 |
complete(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 |
downstream.onError(t); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onComplete() { |
| 98 |
if (!done) { |
| 99 |
done = true; |
| 100 |
T v = defaultValue; |
| 101 |
if (v == null) { |
| 102 |
if (errorOnFewer) { |
| 103 |
downstream.onError(new NoSuchElementException()); |
| 104 |
} else { |
| 105 |
downstream.onComplete(); |
| 106 |
} |
| 107 |
} else { |
| 108 |
complete(v); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void cancel() { |
| 115 |
super.cancel(); |
| 116 |
upstream.cancel(); |
| 117 |
} |
| 118 |
} |
| 119 |
} |