package reactor.core.publisher;
import java.util.Queue;
import java.util.function.Supplier;
import reactor.core.CoreSubscriber;
import reactor.core.Fuseable;
import reactor.core.Scannable;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Scheduler.Worker;
import reactor.util.annotation.Nullable;
final class ParallelRunOn<T> extends ParallelFlux<T> implements Scannable{
final ParallelFlux<? extends T> source;
final Scheduler scheduler;
final int prefetch;
final Supplier<Queue<T>> queueSupplier;
ParallelRunOn(ParallelFlux<? extends T> parent,
Scheduler scheduler, int prefetch, Supplier<Queue<T>> queueSupplier) {
if (prefetch <= 0) {
throw new IllegalArgumentException("prefetch > 0 required but it was " + prefetch);
}
this.source = parent;
this.scheduler = scheduler;
this.prefetch = prefetch;
this.queueSupplier = queueSupplier;
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return source;
if (key == Attr.PREFETCH) return getPrefetch();
return null;
}
@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T>[] subscribers) {
if (!validate(subscribers)) {
return;
}
int n = subscribers.length;
CoreSubscriber<T>[] parents = new CoreSubscriber[n];
boolean conditional = subscribers[0] instanceof Fuseable.ConditionalSubscriber;
for (int i = 0; i < n; i++) {
Worker w = scheduler.createWorker();
if (conditional) {
parents[i] = new FluxPublishOn.PublishOnConditionalSubscriber<>(
(Fuseable.ConditionalSubscriber<T>)subscribers[i],
scheduler, w, true,
prefetch, prefetch, queueSupplier);
}
else {
parents[i] = new FluxPublishOn.PublishOnSubscriber<>(subscribers[i],
scheduler, w, true,
prefetch, prefetch, queueSupplier);
}
}
source.subscribe(parents);
}
@Override
public int getPrefetch() {
return prefetch;
}
@Override
public int parallelism() {
return source.parallelism();
}
}