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

reactor.rx.StreamUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8.RELEASE
Show newest version
/*
 * Copyright (c) 2011-2014 Pivotal Software, Inc.
 *
 *  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.rx;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.reactivestreams.SerializedSubscriber;
import reactor.fn.Consumer;
import reactor.rx.action.Action;
import reactor.rx.action.CompositeAction;
import reactor.rx.action.aggregation.WindowAction;
import reactor.rx.action.combination.DynamicMergeAction;
import reactor.rx.action.combination.FanInAction;
import reactor.rx.action.combination.FanInSubscription;
import reactor.rx.action.combination.SwitchAction;
import reactor.rx.action.error.RetryWhenAction;
import reactor.rx.action.transformation.GroupByAction;
import reactor.rx.stream.GroupedStream;
import reactor.rx.subscription.FanOutSubscription;
import reactor.rx.subscription.PushSubscription;

import java.io.Serializable;
import java.util.*;

/**
 * A simple collection of utils to assist in various tasks such as Debugging
 *
 * @author Stephane Maldini
 * @since 1.1
 */
public abstract class StreamUtils {

	public static  StreamVisitor browse(Stream composable) {
		return browse(composable, new DebugVisitor());
	}

	public static  StreamVisitor browse(Stream composable, DebugVisitor visitor) {
		StreamVisitor explorer = new StreamVisitor(visitor);
		explorer.accept(composable);
		return explorer;
	}

	static class DebugVisitor implements Consumer> {

		final private StringBuilder   appender = new StringBuilder();
		final private List errors   = new ArrayList();
		int d = 0;

		@Override
		public void accept(Stream composable) {
			newLine(d);

			appender.append(composable.getClass().getSimpleName().isEmpty() ? composable.getClass().getName() + "" +
					composable :
					composable
							.getClass()
							.getSimpleName().replaceAll("Action", "") + "[" + composable + "]");
		}

		@Override
		public String toString() {
			return appender.toString();
		}

		public void newMulticastLine(int d) {
			appender.append("\n");
			for (int i = 0; i < d+1; i++)
				appender.append("|   ");
		}

		private void newLine(int d) {
			newLine(d, true);
		}

		private void newLine(int d, boolean prefix) {
			appender.append("\n");
			for (int i = 0; i < d; i++)
				appender.append("|   ");
			if (prefix) appender.append("|____");
		}
	}

	public static class StreamVisitor implements Consumer> {

		final private Set         references = new HashSet();
		final private Map streamTree = new HashMap();
		final private DebugVisitor debugVisitor;

		public StreamVisitor(DebugVisitor debugVisitor) {
			this.debugVisitor = debugVisitor;
		}

		@Override
		public void accept(Stream composable) {
			List downstreamTree = null;
			if(Action.class.isAssignableFrom(composable.getClass())){
				parseUpstream((Action) composable, downstreamTree);
			}
			parseComposable(composable, downstreamTree);
		}

		@SuppressWarnings("unchecked")
		private void parseUpstream(Action action, List downstreamTree){
			if (action.getSubscription() != null && PushSubscription.class.isAssignableFrom(action.getSubscription().getClass())) {
				Publisher publisher = ((PushSubscription) action.getSubscription()).getPublisher();
				if (publisher != null && Stream.class.isAssignableFrom(publisher.getClass())) {
					parseComposable((Stream) publisher, downstreamTree);
				}
			}
		}

		@SuppressWarnings("unchecked")
		private  void parseComposable(Stream composable, final List streamTree) {
			if (composable == null) return;

			Map freshNestedStreams = new HashMap();
			freshNestedStreams.put("id", new StreamKey(composable).toString());

			if (streamTree != null) {
				streamTree.add(freshNestedStreams);
			}

			if (references.contains(composable)) {
				return;
			}

			freshNestedStreams.put("info", composable.toString());
			references.add(composable);

			if (debugVisitor != null) {
				debugVisitor.accept(composable);
				debugVisitor.d += 2;
			}

			List nextLevelNestedStreams = new ArrayList();

			boolean hasRenderedSpecial =
							renderWindow(composable, nextLevelNestedStreams)
							|| renderGroupBy(composable, nextLevelNestedStreams)
							|| renderSwitch(composable, nextLevelNestedStreams)
							|| renderDynamicMerge(composable, nextLevelNestedStreams)
							|| renderMerge(composable, nextLevelNestedStreams)
							|| renderRetryWhen(composable, nextLevelNestedStreams)
							|| renderCombine(composable, nextLevelNestedStreams);

			if (!nextLevelNestedStreams.isEmpty()) {
				freshNestedStreams.put("boundTo", nextLevelNestedStreams);
			}


			if (debugVisitor != null) {
				debugVisitor.d -= 2;
			}

			nextLevelNestedStreams = new ArrayList();
			loopSubscriptions(
					composable.downstreamSubscription(),
					nextLevelNestedStreams
			);

			if (!nextLevelNestedStreams.isEmpty()) {
				freshNestedStreams.put("to", nextLevelNestedStreams);
			}

			if (streamTree == null) {
				this.streamTree.putAll(freshNestedStreams);
			}

		}

		@SuppressWarnings("unchecked")
		private  void loopSubscriptions(E operation, final List streamTree) {
			if (operation == null) return;

			final boolean multicast = FanOutSubscription.class.isAssignableFrom(operation.getClass());

			Consumer procedure = new Consumer() {
				@Override
				public void accept(E registration) {
					Subscriber subscriber = null;
					if (PushSubscription.class.isAssignableFrom(registration.getClass())) {
						subscriber = ((PushSubscription) registration).getSubscriber();
					}
					if(subscriber != null && SerializedSubscriber.class.isAssignableFrom(subscriber.getClass())){
						subscriber =  ((SerializedSubscriber)subscriber).delegate();
					}

					if (subscriber != null) {
						if (debugVisitor != null && multicast) {
							debugVisitor.d ++;
							debugVisitor.newMulticastLine(debugVisitor.d);
						}

						if (Stream.class.isAssignableFrom(subscriber.getClass())) {
							parseComposable((Stream) subscriber, streamTree);
						} else {
							Map wrappedSubscriber = new HashMap();
							if (debugVisitor != null) {
								debugVisitor.newLine(debugVisitor.d);
								debugVisitor.appender.append(subscriber).append(registration);
							}
							if (Promise.class.isAssignableFrom(subscriber.getClass())) {
								List wrappedStream = new ArrayList();
								wrappedSubscriber.put("info", subscriber.toString());

								if(((Promise) subscriber).finalState != null)
									wrappedSubscriber.put("state", ((Promise) subscriber).finalState);

								parseComposable(((Promise) subscriber).outboundStream, wrappedStream);
							} else {
								wrappedSubscriber.put("info", subscriber.toString());
							}
							streamTree.add(wrappedSubscriber);
						}
						if (debugVisitor != null && multicast) {
							debugVisitor.d--;
							debugVisitor.newLine(debugVisitor.d, false);
						}
					}
				}
			};

			if (multicast) {
				((FanOutSubscription) operation).forEach(procedure);
			} else {
				procedure.accept(operation);
			}
		}


		private  boolean renderSwitch(Stream consumer, final List streamTree) {
			if (SwitchAction.class.isAssignableFrom(consumer.getClass())) {
				SwitchAction operation = (SwitchAction) consumer;
				SwitchAction.SwitchSubscriber switchSubscriber = operation.getSwitchSubscriber();
				if (switchSubscriber != null && switchSubscriber.getSubscription() != null) {
						loopSubscriptions(switchSubscriber.getSubscription(), streamTree);
				}
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		private  boolean renderWindow(Stream consumer, final List streamTree) {
			if (WindowAction.class.isAssignableFrom(consumer.getClass())) {
				WindowAction operation = (WindowAction) consumer;

				if (operation.currentWindow() != null) {
					loopSubscriptions(operation.currentWindow(), streamTree);
				}
				return true;
			}
			return false;
		}


		@SuppressWarnings("unchecked")
		private  boolean renderCombine(Stream consumer, final List streamTree) {
			if (CompositeAction.class.isAssignableFrom(consumer.getClass())) {
				CompositeAction operation = (CompositeAction) consumer;
				parseComposable(operation.input(), streamTree);
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		private  boolean renderDynamicMerge(Stream consumer, final List streamTree) {
			if (DynamicMergeAction.class.isAssignableFrom(consumer.getClass())) {
				DynamicMergeAction operation = (DynamicMergeAction) consumer;
				parseComposable(operation.mergedStream(), streamTree);
				return true;
			}
			return false;
		}

@SuppressWarnings("unchecked")
		private  boolean renderRetryWhen(Stream consumer, final List streamTree) {
			if (RetryWhenAction.class.isAssignableFrom(consumer.getClass())) {
				RetryWhenAction operation = (RetryWhenAction) consumer;
				parseComposable(operation.retryStream(), streamTree);
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		private  boolean renderGroupBy(Stream consumer, final List streamTree) {
			if (GroupByAction.class.isAssignableFrom(consumer.getClass())) {
				GroupByAction operation = (GroupByAction) consumer;
				for (PushSubscription s : operation.groupByMap().values()) {
					loopSubscriptions(s, streamTree);
					if (debugVisitor != null) {
						debugVisitor.newLine(debugVisitor.d, false);
					}
				}
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		private  boolean renderMerge(Stream consumer, final List streamTree) {
			if (FanInAction.class.isAssignableFrom(consumer.getClass())) {
				FanInAction operation = (FanInAction) consumer;
				operation.getSubscription().forEach(
						new Consumer() {
							Subscription delegateSubscription;

							@Override
							public void accept(FanInSubscription.InnerSubscription subscription) {
								delegateSubscription = subscription.getDelegate();
								if (PushSubscription.class.isAssignableFrom(delegateSubscription.getClass())) {
									Publisher publisher = ((PushSubscription) delegateSubscription).getPublisher();
									if (publisher == null || references.contains(publisher)) return;
									if (Action.class.isAssignableFrom(publisher.getClass())) {
										parseComposable(((Action) publisher).findOldestUpstream(Stream.class), streamTree);
									} else if (Stream.class.isAssignableFrom(publisher.getClass())) {
										parseComposable((Stream) publisher, streamTree);
									}
								}
							}
						});
				return true;
			}
			return false;
		}

		public Map toMap() {
			return streamTree;
		}

		@Override
		public String toString() {
			return debugVisitor.toString();
		}
	}

	private final static class StreamKey implements Serializable {

		private final Stream stream;
		private final Object    key;

		public StreamKey(Stream composable) {
			this.stream = composable;
			if (GroupedStream.class.isAssignableFrom(stream.getClass())) {
				this.key = ((GroupedStream) stream).key();
			} else {
				this.key = composable.getClass().getSimpleName().isEmpty() ? composable.getClass().getName() + "" +
						composable :
						composable
								.getClass()
								.getSimpleName().replaceAll("Action", "");
			}
		}

		@Override
		public String toString() {
			return key.toString();
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;

			StreamKey streamKey = (StreamKey) o;

			if (!stream.equals(streamKey.stream)) return false;

			return true;
		}

		@Override
		public int hashCode() {
			return stream.hashCode();
		}
	}
}