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

reactor.adapter.JdkFlowAdapter 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

There is a newer version: 3.40.2
Show newest version
/*
 * Copyright (c) 2016-2021 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.adapter;

import java.util.concurrent.Flow;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Scannable;
import reactor.core.publisher.Flux;

/**
 * Convert a Java 9+ {@literal Flow.Publisher} to/from a Reactive Streams {@link Publisher}.
 *
 * @author Stephane Maldini
 * @author Sebastien Deleuze
 */
@SuppressWarnings("Since15")
public abstract class JdkFlowAdapter {

	/**
	 * Return a java {@code Flow.Publisher} from a {@link Flux}
	 * @param publisher the source Publisher to convert
	 * @param  the type of the publisher
	 * @return a java {@code Flow.Publisher} from the given {@link Publisher}
	 */
	public static  Flow.Publisher publisherToFlowPublisher(final Publisher
			publisher) {
		return new PublisherAsFlowPublisher<>(publisher);
	}

	/**
	 * Return a {@link Flux} from a java {@code Flow.Publisher}
	 *
	 * @param publisher the source Publisher to convert
	 * @param  the type of the publisher
	 * @return a {@link Flux} from a java {@code Flow.Publisher}
	 */
	public static  Flux flowPublisherToFlux(Flow.Publisher publisher) {
		return new FlowPublisherAsFlux<>(publisher);
	}

	private static class FlowPublisherAsFlux extends Flux implements Scannable {
        private final java.util.concurrent.Flow.Publisher pub;

        private FlowPublisherAsFlux(java.util.concurrent.Flow.Publisher pub) {
            this.pub = pub;
        }

        @Override
        public void subscribe(final CoreSubscriber actual) {
        	pub.subscribe(new SubscriberToRS<>(actual));
        }

		@Override
		public Object scanUnsafe(Attr key) {
			return null; //no particular key to be represented, still useful in hooks
		}
    }

    private static class PublisherAsFlowPublisher implements Flow.Publisher {
        private final Publisher pub;

        private PublisherAsFlowPublisher(Publisher pub) {
            this.pub = pub;
        }

        @Override
        public void subscribe(Flow.Subscriber subscriber) {
        	pub.subscribe(new FlowSubscriber<>(subscriber));
        }
    }

    private static class FlowSubscriber implements CoreSubscriber, Flow.Subscription {

		private final Flow.Subscriber subscriber;

		Subscription subscription;

		public FlowSubscriber(Flow.Subscriber subscriber) {
			this.subscriber = subscriber;
		}

		@Override
		public void onSubscribe(final Subscription s) {
		    this.subscription = s;
			subscriber.onSubscribe(this);
		}

		@Override
		public void onNext(T o) {
			subscriber.onNext(o);
		}

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

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

		@Override
		public void request(long n) {
		    subscription.request(n);
		}

		@Override
		public void cancel() {
		    subscription.cancel();
		}
	}

	private static class SubscriberToRS implements Flow.Subscriber, Subscription {

		private final Subscriber s;

		Flow.Subscription subscription;

		public SubscriberToRS(Subscriber s) {
			this.s = s;
		}

		@Override
		public void onSubscribe(final Flow.Subscription subscription) {
		    this.subscription = subscription;
			s.onSubscribe(this);
		}

		@Override
		public void onNext(T o) {
			s.onNext(o);
		}

		@Override
		public void onError(Throwable throwable) {
			s.onError(throwable);
		}

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

		@Override
		public void request(long n) {
		    subscription.request(n);
		}

		@Override
		public void cancel() {
		    subscription.cancel();
		}
	}

	JdkFlowAdapter(){}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy