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

com.autonomouslogic.commons.rxjava3.internal.ZipAll Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package com.autonomouslogic.commons.rxjava3.internal;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.functions.Predicate;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.reactivestreams.Publisher;

@RequiredArgsConstructor
@SuppressWarnings("unchecked")
public class ZipAll {
	private static final Predicate PREDICATE = in -> {
		var objs = (Object[]) in;
		for (var obj : objs) {
			if (((Optional) obj).isPresent()) {
				return true;
			}
		}
		return false;
	};

	private static final Function IDENTITY = v -> v;

	@NonNull
	private final Function zipper;

	private final boolean delayError;
	private final int bufferSize;

	@NonNull
	private final Publisher[] sources;

	public Flowable createFlowable() {
		return Flowable.zipArray(IDENTITY, delayError, bufferSize, padSources())
				.takeWhile(PREDICATE)
				.map(zipper);
	}

	private Publisher[] padSources() {
		var padded = new Publisher[sources.length];
		for (int i = 0; i < sources.length; i++) {
			padded[i] = Flowable.concat(map(sources[i]), pad());
		}
		return padded;
	}

	private Publisher> map(Publisher source) {
		return Flowable.fromPublisher(source).map(Optional::of);
	}

	private Publisher> pad() {
		return Flowable.generate(emitter -> emitter.onNext(Optional.empty()));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy