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

io.smallrye.mutiny.operators.multi.MultiSkipFirstUntilOp Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package io.smallrye.mutiny.operators.multi;

import java.util.function.Predicate;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.helpers.ParameterValidation;
import io.smallrye.mutiny.subscription.MultiSubscriber;

/**
 * Skips the items from upstream until the passed predicates returns {@code true}.
 *
 * @param  the type of item
 */
public final class MultiSkipFirstUntilOp extends AbstractMultiOperator {

    private final Predicate predicate;

    public MultiSkipFirstUntilOp(Multi upstream, Predicate predicate) {
        super(upstream);
        this.predicate = ParameterValidation.nonNull(predicate, "predicate");
    }

    @Override
    public void subscribe(MultiSubscriber subscriber) {
        ParameterValidation.nonNullNpe(subscriber, "subscriber");
        upstream.subscribe(new MultiSkipFirstUntilProcessor<>(subscriber, predicate));
    }

    static final class MultiSkipFirstUntilProcessor extends MultiOperatorProcessor {

        private final Predicate predicate;
        private boolean gateOpen = false;

        MultiSkipFirstUntilProcessor(MultiSubscriber downstream, Predicate predicate) {
            super(downstream);
            this.predicate = predicate;
        }

        @Override
        public void onItem(T t) {
            if (isDone()) {
                return;
            }

            if (gateOpen) {
                downstream.onItem(t);
                return;
            }

            boolean toBeSkipped;
            try {
                toBeSkipped = predicate.test(t);
            } catch (Throwable e) {
                failAndCancel(e);
                return;
            }

            if (!toBeSkipped) {
                gateOpen = true;
                downstream.onItem(t);
                return;
            }

            request(1);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy