| 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.maybe; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.*; |
| 25 |
|
| 26 |
/** |
| 27 |
* Concatenate values of each MaybeSource provided in an array. |
| 28 |
* |
| 29 |
* @param <T> the value type |
| 30 |
*/ |
| 31 |
public final class MaybeConcatArray<T> extends Flowable<T> { |
| 32 |
|
| 33 |
final MaybeSource<? extends T>[] sources; |
| 34 |
|
| 35 |
public MaybeConcatArray(MaybeSource<? extends T>[] sources) { |
| 36 |
this.sources = sources; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 41 |
ConcatMaybeObserver<T> parent = new ConcatMaybeObserver<>(s, sources); |
| 42 |
s.onSubscribe(parent); |
| 43 |
parent.drain(); |
| 44 |
} |
| 45 |
|
| 46 |
static final class ConcatMaybeObserver<T> |
| 47 |
extends AtomicInteger |
| 48 |
implements MaybeObserver<T>, Subscription { |
| 49 |
|
| 50 |
private static final long serialVersionUID = 3520831347801429610L; |
| 51 |
|
| 52 |
final Subscriber<? super T> downstream; |
| 53 |
|
| 54 |
final AtomicLong requested; |
| 55 |
|
| 56 |
final AtomicReference<Object> current; |
| 57 |
|
| 58 |
final SequentialDisposable disposables; |
| 59 |
|
| 60 |
final MaybeSource<? extends T>[] sources; |
| 61 |
|
| 62 |
int index; |
| 63 |
|
| 64 |
long produced; |
| 65 |
|
| 66 |
ConcatMaybeObserver(Subscriber<? super T> actual, MaybeSource<? extends T>[] sources) { |
| 67 |
this.downstream = actual; |
| 68 |
this.sources = sources; |
| 69 |
this.requested = new AtomicLong(); |
| 70 |
this.disposables = new SequentialDisposable(); |
| 71 |
this.current = new AtomicReference<>(NotificationLite.COMPLETE); // as if a previous completed |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void request(long n) { |
| 76 |
if (SubscriptionHelper.validate(n)) { |
| 77 |
BackpressureHelper.add(requested, n); |
| 78 |
drain(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void cancel() { |
| 84 |
disposables.dispose(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onSubscribe(Disposable d) { |
| 89 |
disposables.replace(d); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onSuccess(T value) { |
| 94 |
current.lazySet(value); |
| 95 |
drain(); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void onError(Throwable e) { |
| 100 |
downstream.onError(e); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void onComplete() { |
| 105 |
current.lazySet(NotificationLite.COMPLETE); |
| 106 |
drain(); |
| 107 |
} |
| 108 |
|
| 109 |
@SuppressWarnings("unchecked") |
| 110 |
void drain() { |
| 111 |
if (getAndIncrement() != 0) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
AtomicReference<Object> c = current; |
| 116 |
Subscriber<? super T> a = downstream; |
| 117 |
Disposable cancelled = disposables; |
| 118 |
|
| 119 |
for (;;) { |
| 120 |
if (cancelled.isDisposed()) { |
| 121 |
c.lazySet(null); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
Object o = c.get(); |
| 126 |
|
| 127 |
if (o != null) { |
| 128 |
boolean goNextSource; |
| 129 |
if (o != NotificationLite.COMPLETE) { |
| 130 |
long p = produced; |
| 131 |
if (p != requested.get()) { |
| 132 |
produced = p + 1; |
| 133 |
c.lazySet(null); |
| 134 |
goNextSource = true; |
| 135 |
|
| 136 |
a.onNext((T)o); |
| 137 |
} else { |
| 138 |
goNextSource = false; |
| 139 |
} |
| 140 |
} else { |
| 141 |
goNextSource = true; |
| 142 |
c.lazySet(null); |
| 143 |
} |
| 144 |
|
| 145 |
if (goNextSource && !cancelled.isDisposed()) { |
| 146 |
int i = index; |
| 147 |
if (i == sources.length) { |
| 148 |
a.onComplete(); |
| 149 |
return; |
| 150 |
} |
| 151 |
index = i + 1; |
| 152 |
|
| 153 |
sources[i].subscribe(this); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (decrementAndGet() == 0) { |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |