All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.core.publisher.MonoFlatMapMany Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

The newest version!
/*
 * Copyright (c) 2016-2023 VMware Inc. or its affiliates, 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
 *
 *   https://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.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Function;
import java.util.stream.Stream;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Scannable;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;

final class MonoFlatMapMany extends FluxFromMonoOperator {


	final Function> mapper;

	MonoFlatMapMany(Mono source,
			Function> mapper) {
		super(source);
		this.mapper = mapper;
	}

	@Override
	public CoreSubscriber subscribeOrReturn(CoreSubscriber actual) {
		//for now Mono in general doesn't support onErrorContinue, so the scalar version shouldn't either
		//even if the result is a Flux. once the mapper is applied, onErrorContinue will be taken care of by
		//the mapped Flux if relevant.
		if (FluxFlatMap.trySubscribeScalarMap(source, actual, mapper, false, false)) {
			return null;
		}
		return new FlatMapManyMain(actual, mapper);
	}

	@Override
	public Object scanUnsafe(Attr key) {
		if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
		return super.scanUnsafe(key);
	}

	static final class FlatMapManyMain implements InnerOperator {

		final CoreSubscriber actual;

		final Function> mapper;

		Subscription main;

		volatile Subscription inner;
		@SuppressWarnings("rawtypes")
		static final AtomicReferenceFieldUpdater INNER =
				AtomicReferenceFieldUpdater.newUpdater(FlatMapManyMain.class,
						Subscription.class,
						"inner");

		volatile long requested;
		@SuppressWarnings("rawtypes")
		static final AtomicLongFieldUpdater REQUESTED =
				AtomicLongFieldUpdater.newUpdater(FlatMapManyMain.class, "requested");

		boolean hasValue;

		FlatMapManyMain(CoreSubscriber actual,
				Function> mapper) {
			this.actual = actual;
			this.mapper = mapper;
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.PARENT) return main;
			if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;

			return InnerOperator.super.scanUnsafe(key);
		}

		@Override
		public Stream inners() {
			return Stream.of(Scannable.from(inner));
		}

		@Override
		public CoreSubscriber actual() {
			return actual;
		}

		@Override
		public void request(long n) {
			Subscription a = inner;
			if (a != null) {
				a.request(n);
			}
			else {
				if (Operators.validate(n)) {
					Operators.addCap(REQUESTED, this, n);
					a = inner;
					if (a != null) {
						n = REQUESTED.getAndSet(this, 0L);
						if (n != 0L) {
							a.request(n);
						}
					}
				}
			}
		}

		@Override
		public void cancel() {
			main.cancel();
			Operators.terminate(INNER, this);
		}

		@Override
		public void onSubscribe(Subscription s) {
			if (Operators.validate(this.main, s)) {
				this.main = s;

				actual.onSubscribe(this);

				s.request(Long.MAX_VALUE);
			}
		}

		void onSubscribeInner(Subscription s) {
			if (Operators.setOnce(INNER, this, s)) {

				long r = REQUESTED.getAndSet(this, 0L);
				if (r != 0) {
					s.request(r);
				}
			}
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onNext(T t) {
			hasValue = true;

			Publisher p;

			try {
				p = Objects.requireNonNull(mapper.apply(t),
						"The mapper returned a null Publisher.");
			}
			catch (Throwable ex) {
				//if the mapping fails, then there is nothing to be continued, since the source is a Mono
				actual.onError(Operators.onOperatorError(this, ex, t,
						actual.currentContext()));
				return;
			}

			if (p instanceof Callable) {
				R v;

				try {
					v = ((Callable) p).call();
				}
				catch (Throwable ex) {
					actual.onError(Operators.onOperatorError(this, ex, t,
							actual.currentContext()));
					return;
				}

				if (v == null) {
					actual.onComplete();
				}
				else {
					onSubscribeInner(Operators.scalarSubscription(actual, v));
				}

				return;
			}

			p = Operators.toFluxOrMono(p);
			p.subscribe(new FlatMapManyInner<>(this, actual));
		}

		@Override
		public void onError(Throwable t) {
			if (hasValue) {
				Operators.onErrorDropped(t, actual.currentContext());
				return;
			}
			actual.onError(t);
		}

		@Override
		public void onComplete() {
			if (!hasValue) {
				actual.onComplete();
			}
		}
	}

	static final class FlatMapManyInner implements InnerConsumer {

		final FlatMapManyMain parent;

		final CoreSubscriber actual;

		FlatMapManyInner(FlatMapManyMain parent,
				CoreSubscriber actual) {
			this.parent = parent;
			this.actual = actual;
		}

		@Override
		public Context currentContext() {
			return actual.currentContext();
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.PARENT) return parent.inner;
			if (key == Attr.ACTUAL) return parent;
			if (key == Attr.REQUESTED_FROM_DOWNSTREAM) return parent.requested;
			if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;

			return null;
		}

		@Override
		public void onSubscribe(Subscription s) {
			parent.onSubscribeInner(s);
		}

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

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

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

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy