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

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

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

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableTransformer;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicReference;
import lombok.RequiredArgsConstructor;
import org.reactivestreams.Publisher;

@RequiredArgsConstructor
public class CheckOrder implements FlowableTransformer {
	private static final int MAX_CONCURRENCY = 1;

	@lombok.NonNull
	private final Comparator comparator;

	@Override
	public @NonNull Publisher apply(@NonNull @lombok.NonNull Flowable upstream) {
		final var last = new AtomicReference();
		return upstream.flatMap(
				current -> {
					if (last.get() != null && comparator.compare(last.get(), current) > 0) {
						return Flowable.error(new RuntimeException(
								String.format("Stream isn't ordered - last: %s, current: %s", last, current)));
					}
					last.set(current);
					return Flowable.just(current);
				},
				MAX_CONCURRENCY);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy