| 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.*; |
| 17 |
import java.util.concurrent.atomic.AtomicInteger; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.CompositeException; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter; |
| 24 |
|
| 25 |
public final class FlowableConcatArray<T> extends Flowable<T> { |
| 26 |
|
| 27 |
final Publisher<? extends T>[] sources; |
| 28 |
|
| 29 |
final boolean delayError; |
| 30 |
|
| 31 |
public FlowableConcatArray(Publisher<? extends T>[] sources, boolean delayError) { |
| 32 |
this.sources = sources; |
| 33 |
this.delayError = delayError; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 38 |
ConcatArraySubscriber<T> parent = new ConcatArraySubscriber<>(sources, delayError, s); |
| 39 |
s.onSubscribe(parent); |
| 40 |
|
| 41 |
parent.onComplete(); |
| 42 |
} |
| 43 |
|
| 44 |
static final class ConcatArraySubscriber<T> extends SubscriptionArbiter implements FlowableSubscriber<T> { |
| 45 |
|
| 46 |
private static final long serialVersionUID = -8158322871608889516L; |
| 47 |
|
| 48 |
final Subscriber<? super T> downstream; |
| 49 |
|
| 50 |
final Publisher<? extends T>[] sources; |
| 51 |
|
| 52 |
final boolean delayError; |
| 53 |
|
| 54 |
final AtomicInteger wip; |
| 55 |
|
| 56 |
int index; |
| 57 |
|
| 58 |
List<Throwable> errors; |
| 59 |
|
| 60 |
long produced; |
| 61 |
|
| 62 |
ConcatArraySubscriber(Publisher<? extends T>[] sources, boolean delayError, Subscriber<? super T> downstream) { |
| 63 |
super(false); |
| 64 |
this.downstream = downstream; |
| 65 |
this.sources = sources; |
| 66 |
this.delayError = delayError; |
| 67 |
this.wip = new AtomicInteger(); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onSubscribe(Subscription s) { |
| 72 |
setSubscription(s); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onNext(T t) { |
| 77 |
produced++; |
| 78 |
downstream.onNext(t); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onError(Throwable t) { |
| 83 |
if (delayError) { |
| 84 |
List<Throwable> list = errors; |
| 85 |
if (list == null) { |
| 86 |
list = new ArrayList<>(sources.length - index + 1); |
| 87 |
errors = list; |
| 88 |
} |
| 89 |
list.add(t); |
| 90 |
onComplete(); |
| 91 |
} else { |
| 92 |
downstream.onError(t); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onComplete() { |
| 98 |
if (wip.getAndIncrement() == 0) { |
| 99 |
Publisher<? extends T>[] sources = this.sources; |
| 100 |
int n = sources.length; |
| 101 |
int i = index; |
| 102 |
for (;;) { |
| 103 |
|
| 104 |
if (i == n) { |
| 105 |
List<Throwable> list = errors; |
| 106 |
if (list != null) { |
| 107 |
if (list.size() == 1) { |
| 108 |
downstream.onError(list.get(0)); |
| 109 |
} else { |
| 110 |
downstream.onError(new CompositeException(list)); |
| 111 |
} |
| 112 |
} else { |
| 113 |
downstream.onComplete(); |
| 114 |
} |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
Publisher<? extends T> p = sources[i]; |
| 119 |
|
| 120 |
if (p == null) { |
| 121 |
Throwable ex = new NullPointerException("A Publisher entry is null"); |
| 122 |
if (delayError) { |
| 123 |
List<Throwable> list = errors; |
| 124 |
if (list == null) { |
| 125 |
list = new ArrayList<>(n - i + 1); |
| 126 |
errors = list; |
| 127 |
} |
| 128 |
list.add(ex); |
| 129 |
i++; |
| 130 |
continue; |
| 131 |
} else { |
| 132 |
downstream.onError(ex); |
| 133 |
return; |
| 134 |
} |
| 135 |
} else { |
| 136 |
long r = produced; |
| 137 |
if (r != 0L) { |
| 138 |
produced = 0L; |
| 139 |
produced(r); |
| 140 |
} |
| 141 |
p.subscribe(this); |
| 142 |
} |
| 143 |
|
| 144 |
index = ++i; |
| 145 |
|
| 146 |
if (wip.decrementAndGet() == 0) { |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |