| 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.concurrent.atomic.AtomicInteger; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter; |
| 22 |
|
| 23 |
public final class FlowableRepeat<T> extends AbstractFlowableWithUpstream<T, T> { |
| 24 |
final long count; |
| 25 |
public FlowableRepeat(Flowable<T> source, long count) { |
| 26 |
super(source); |
| 27 |
this.count = count; |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public void subscribeActual(Subscriber<? super T> s) { |
| 32 |
SubscriptionArbiter sa = new SubscriptionArbiter(false); |
| 33 |
s.onSubscribe(sa); |
| 34 |
|
| 35 |
RepeatSubscriber<T> rs = new RepeatSubscriber<>(s, count != Long.MAX_VALUE ? count - 1 : Long.MAX_VALUE, sa, source); |
| 36 |
rs.subscribeNext(); |
| 37 |
} |
| 38 |
|
| 39 |
static final class RepeatSubscriber<T> extends AtomicInteger implements FlowableSubscriber<T> { |
| 40 |
|
| 41 |
private static final long serialVersionUID = -7098360935104053232L; |
| 42 |
|
| 43 |
final Subscriber<? super T> downstream; |
| 44 |
final SubscriptionArbiter sa; |
| 45 |
final Publisher<? extends T> source; |
| 46 |
long remaining; |
| 47 |
|
| 48 |
long produced; |
| 49 |
|
| 50 |
RepeatSubscriber(Subscriber<? super T> actual, long count, SubscriptionArbiter sa, Publisher<? extends T> source) { |
| 51 |
this.downstream = actual; |
| 52 |
this.sa = sa; |
| 53 |
this.source = source; |
| 54 |
this.remaining = count; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onSubscribe(Subscription s) { |
| 59 |
sa.setSubscription(s); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onNext(T t) { |
| 64 |
produced++; |
| 65 |
downstream.onNext(t); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onError(Throwable t) { |
| 70 |
downstream.onError(t); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onComplete() { |
| 75 |
long r = remaining; |
| 76 |
if (r != Long.MAX_VALUE) { |
| 77 |
remaining = r - 1; |
| 78 |
} |
| 79 |
if (r != 0L) { |
| 80 |
subscribeNext(); |
| 81 |
} else { |
| 82 |
downstream.onComplete(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Subscribes to the source again via trampolining. |
| 88 |
*/ |
| 89 |
void subscribeNext() { |
| 90 |
if (getAndIncrement() == 0) { |
| 91 |
int missed = 1; |
| 92 |
for (;;) { |
| 93 |
if (sa.isCancelled()) { |
| 94 |
return; |
| 95 |
} |
| 96 |
long p = produced; |
| 97 |
if (p != 0L) { |
| 98 |
produced = 0L; |
| 99 |
sa.produced(p); |
| 100 |
} |
| 101 |
source.subscribe(this); |
| 102 |
|
| 103 |
missed = addAndGet(-missed); |
| 104 |
if (missed == 0) { |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |