hu.akarnokd.rxjava3.operators.FlowableExpand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-extensions Show documentation
Show all versions of rxjava3-extensions Show documentation
RxJava 3.x extra sources, operators and components and ports of many 1.x companion libraries.
/*
* Copyright 2016-2019 David Karnok
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hu.akarnokd.rxjava3.operators;
import java.util.ArrayDeque;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
import io.reactivex.rxjava3.internal.fuseable.SimplePlainQueue;
import io.reactivex.rxjava3.internal.queue.SpscLinkedArrayQueue;
import io.reactivex.rxjava3.internal.subscriptions.*;
import io.reactivex.rxjava3.internal.util.*;
/**
* Emit and recursively expand elements from upstream.
* @param the value type
* @since 0.16.1
*/
final class FlowableExpand extends Flowable implements FlowableTransformer {
final Flowable source;
final Function super T, ? extends Publisher extends T>> expander;
final ExpandStrategy strategy;
final int capacityHint;
final boolean delayErrors;
FlowableExpand(Flowable source, Function super T, ? extends Publisher extends T>> expander,
ExpandStrategy strategy, int capacityHint, boolean delayErrors) {
this.source = source;
this.expander = expander;
this.strategy = strategy;
this.capacityHint = capacityHint;
this.delayErrors = delayErrors;
}
@Override
protected void subscribeActual(Subscriber super T> s) {
if (strategy != ExpandStrategy.DEPTH_FIRST) {
ExpandBreadthSubscriber parent = new ExpandBreadthSubscriber(s, expander, capacityHint, delayErrors);
parent.queue.offer(source);
s.onSubscribe(parent);
parent.drainQueue();
} else {
ExpandDepthSubscription parent = new ExpandDepthSubscription(s, expander, capacityHint, delayErrors);
parent.source = source;
s.onSubscribe(parent);
}
}
@Override
public Publisher apply(Flowable upstream) {
return new FlowableExpand(upstream, expander, strategy, capacityHint, delayErrors);
}
static final class ExpandBreadthSubscriber extends SubscriptionArbiter implements FlowableSubscriber {
private static final long serialVersionUID = -8200116117441115256L;
final Subscriber super T> downstream;
final Function super T, ? extends Publisher extends T>> expander;
final SimplePlainQueue> queue;
final AtomicInteger wip;
final boolean delayErrors;
final AtomicThrowable errors;
volatile boolean active;
long produced;
ExpandBreadthSubscriber(Subscriber super T> downstream,
Function super T, ? extends Publisher extends T>> expander, int capacityHint, boolean delayErrors) {
super(false);
this.downstream = downstream;
this.expander = expander;
this.wip = new AtomicInteger();
this.queue = new SpscLinkedArrayQueue>(capacityHint);
this.errors = new AtomicThrowable();
this.delayErrors = delayErrors;
}
@Override
public void onSubscribe(Subscription s) {
setSubscription(s);
}
@Override
public void onNext(T t) {
produced++;
downstream.onNext(t);
Publisher extends T> p;
try {
p = ObjectHelper.requireNonNull(expander.apply(t), "The expander returned a null Publisher");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
super.cancel();
downstream.onError(ex);
drainQueue();
return;
}
queue.offer(p);
}
@Override
public void onError(Throwable t) {
setSubscription(SubscriptionHelper.CANCELLED);
if (delayErrors) {
errors.tryAddThrowableOrReport(t);
active = false;
} else {
super.cancel();
downstream.onError(t);
}
drainQueue();
}
@Override
public void onComplete() {
active = false;
drainQueue();
}
@Override
public void cancel() {
super.cancel();
errors.tryTerminateAndReport();
drainQueue();
}
void drainQueue() {
if (wip.getAndIncrement() == 0) {
do {
SimplePlainQueue> q = queue;
if (isCancelled()) {
q.clear();
} else {
if (!active) {
if (q.isEmpty()) {
setSubscription(SubscriptionHelper.CANCELLED);
super.cancel();
errors.tryTerminateConsumer(downstream);
} else {
Publisher extends T> p = q.poll();
long c = produced;
if (c != 0L) {
produced = 0L;
produced(c);
}
active = true;
p.subscribe(this);
}
}
}
} while (wip.decrementAndGet() != 0);
}
}
}
static final class ExpandDepthSubscription
extends AtomicInteger
implements Subscription {
private static final long serialVersionUID = -2126738751597075165L;
final Subscriber super T> downstream;
final Function super T, ? extends Publisher extends T>> expander;
final AtomicThrowable error;
final AtomicInteger active;
final AtomicLong requested;
final AtomicReference