<commit_before>/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * 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 reactor.core.publisher;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.flow.Loopback;
import reactor.core.flow.Producer;
import reactor.core.util.BackpressureUtils;
import reactor.core.util.DeferredSubscription;
import reactor.core.util.EmptySubscription;
import reactor.core.util.Exceptions;
import reactor.fn.Consumer;
import reactor.fn.Supplier;

/**
 * Subscribes to the source Publisher asynchronously through a scheduler function or
 * ExecutorService.
 * 
 * @param <T> the value type
 */

/**
 * {@see <a href='https://github.com/reactor/reactive-streams-commons'>https://github.com/reactor/reactive-streams-commons</a>}
 * @since 2.5
 */
final class FluxPublishOn<T> extends FluxSource<T, T> implements Loopback {

	final Callable<? extends Consumer<Runnable>> schedulerFactory;
	
	public FluxPublishOn(
			Publisher<? extends T> source, 
			Callable<? extends Consumer<Runnable>> schedulerFactory) {
		super(source);
		this.schedulerFactory = Objects.requireNonNull(schedulerFactory, "schedulerFactory");
	}

	@Override
	public void subscribe(Subscriber<? super T> s) {
		Consumer<Runnable> scheduler;
		
		try {
			scheduler = schedulerFactory.call();
		} catch (Throwable e) {
			Exceptions.throwIfFatal(e);
			EmptySubscription.error(s, e);
			return;
		}
		
		if (scheduler == null) {
			EmptySubscription.error(s, new NullPointerException("The schedulerFactory returned a null Function"));
			return;
		}
		
		if (source instanceof Supplier) {
			
			@SuppressWarnings("unchecked")
			Supplier<T> supplier = (Supplier<T>) source;
			
			T v = supplier.get();
			
			if (v == null) {
				ScheduledEmptySubscriptionEager parent = new ScheduledEmptySubscriptionEager(s, scheduler);
				s.onSubscribe(parent);
				scheduler.accept(parent);
			} else {
				s.onSubscribe(new ScheduledSubscriptionEagerCancel<>(s, v, scheduler));
			}
			return;
		}
		
		PublishOnClassic<T> parent = new PublishOnClassic<>(s, scheduler);
		s.onSubscribe(parent);
		
		scheduler.accept(new SourceSubscribeTask<>(parent, source));
	}

	@Override
	public Object connectedInput() {
		return schedulerFactory;
	}

	@Override
	public Object connectedOutput() {
		return null;
	}

	static final class PublishOnClassic<T>
	extends DeferredSubscription implements Subscriber<T>, Producer, Loopback {
		final Subscriber<? super T> actual;
		
		final Consumer<Runnable> scheduler;

		public PublishOnClassic(Subscriber<? super T> actual, Consumer<Runnable> scheduler) {
			this.actual = actual;
			this.scheduler = scheduler;
		}
		
		@Override
		public void onSubscribe(Subscription s) {
			set(s);
		}
		
		@Override
		public void onNext(T t) {
			actual.onNext(t);
		}
		
		@Override
		public void onError(Throwable t) {
			scheduler.accept(null);
			actual.onError(t);
		}
		
		@Override
		public void onComplete() {
			scheduler.accept(null);
			actual.onComplete();
		}
		
		@Override
		public void request(long n) {
			if (BackpressureUtils.validate(n)) {
				scheduler.accept(new RequestTask(n, this));
			}
		}

		@Override
		public void cancel() {
			super.cancel();
			scheduler.accept(null);
		}
		
		void requestInner(long n) {
			super.request(n);
		}

		@Override
		public Object downstream() {
			return actual;
		}

		@Override
		public Object connectedOutput() {
			return scheduler;
		}

		@Override
		public Object connectedInput() {
			return null;
		}
	}

	static final class RequestTask implements Runnable {

		final long n;
		final PublishOnClassic<?> parent;

		public RequestTask(long n, PublishOnClassic<?> parent) {
			this.n = n;
			this.parent = parent;
		}

		@Override
		public void run() {
			parent.requestInner(n);
		}
	}
	
	static final class ScheduledSubscriptionEagerCancel<T><commit_msg>add a pipelining subscribeOn/publishOn<commit_after>/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * 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 reactor.core.publisher;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.flow.Loopback;
import reactor.core.flow.Producer;
import reactor.core.util.BackpressureUtils;
import reactor.core.util.DeferredSubscription;
import reactor.core.util.EmptySubscription;
import reactor.core.util.Exceptions;
import reactor.fn.Consumer;
import reactor.fn.Supplier;

/**
 * Subscribes to the source Publisher asynchronously through a scheduler function or
 * ExecutorService.
 * 
 * @param <T> the value type
 */

/**
 * {@see <a href='https://github.com/reactor/reactive-streams-commons'>https://github.com/reactor/reactive-streams-commons</a>}
 * @since 2.5
 */
final class FluxPublishOn<T> extends FluxSource<T, T> implements Loopback {

	final Callable<? extends Consumer<Runnable>> schedulerFactory;
	
	public FluxPublishOn(
			Publisher<? extends T> source, 
			Callable<? extends Consumer<Runnable>> schedulerFactory) {
		super(source);
		this.schedulerFactory = Objects.requireNonNull(schedulerFactory, "schedulerFactory");
	}

	@Override
	public void subscribe(Subscriber<? super T> s) {
		Consumer<Runnable> scheduler;
		
		try {
			scheduler = schedulerFactory.call();
		} catch (Throwable e) {
			Exceptions.throwIfFatal(e);
			EmptySubscription.error(s, e);
			return;
		}
		
		if (scheduler == null) {
			EmptySubscription.error(s, new NullPointerException("The schedulerFactory returned a null Function"));
			return;
		}
		
		if (source instanceof Supplier) {
			
			@SuppressWarnings("unchecked")
			Supplier<T> supplier = (Supplier<T>) source;
			
			T v = supplier.get();
			
			if (v == null) {
				ScheduledEmptySubscriptionEager parent = new ScheduledEmptySubscriptionEager(s, scheduler);
				s.onSubscribe(parent);
				scheduler.accept(parent);
			} else {
				s.onSubscribe(new ScheduledSubscriptionEagerCancel<>(s, v, scheduler));
			}
			return;
		}

		PublishOnPipeline<T> parent = new PublishOnPipeline<>(s, scheduler);
		//FluxPublishOnPipeline<T> parent = new FluxPublishOnPipeline<>(s, scheduler);
		s.onSubscribe(parent);
		
		scheduler.accept(new SourceSubscribeTask<>(parent, source));
	}

	@Override
	public Object connectedInput() {
		return schedulerFactory;
	}

	@Override
	public Object connectedOutput() {
		return null;
	}

	static final class PublishOnPipeline<T>
			extends DeferredSubscription implements Subscriber<T>, Producer, Loopback, Runnable {
		final Subscriber<? super T> actual;

		final Consumer<Runnable> scheduler;

		volatile long requested;

		static final AtomicLongFieldUpdater<PublishOnPipeline> REQUESTED =
			AtomicLongFieldUpdater.newUpdater(PublishOnPipeline.class, "requested");

		public PublishOnPipeline(Subscriber<? super T> actual, Consumer<Runnable> scheduler) {
			this.actual = actual;
			this.scheduler = scheduler;
		}

		@Override
		public void onSubscribe(Subscription s) {
			set(s);
		}

		@Override
		public void onNext(T t) {
			actual.onNext(t);
		}

		@Override
		public void onError(Throwable t) {
			scheduler.accept(null);
			actual.onError(t);
		}

		@Override
		public void onComplete() {
			scheduler.accept(null);
			actual.onComplete();
		}

		@Override
		public void request(long n) {
			if (BackpressureUtils.validate(n)) {
				if(BackpressureUtils.addAndGet(REQUESTED, this, n) == 0L){
					scheduler.accept(this);
				}
			}
		}

		@Override
		public void run() {
			long r = requested;
			for(;;){
				if(r != 0) {
					super.request(r);
				}

				r = REQUESTED.getAndSet(this, 0L);
				if(r == 0) {
					break;
				}
			}
		}

		@Override
		public void cancel() {
			super.cancel();
			scheduler.accept(null);
		}

		@Override
		public Object downstream() {
			return actual;
		}

		@Override
		public Object connectedOutput() {
			return scheduler;
		}

		@Override
		public Object connectedInput() {
			return null;
		}
	}

	static final class ScheduledSubscriptionEagerCancel<T>
{'fraction_non_alphanumeric': 0.1075633817757505, 'fraction_numerical': 0.00339450514479686, 'mean_word_length': 4.07427341227126, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1458611', 'n_tokens_mistral': 3345, 'n_tokens_neox': 2868, 'n_words': 746}