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

com.github.davidmoten.rx.internal.operators.OperatorDoOnNth Maven / Gradle / Ivy

package com.github.davidmoten.rx.internal.operators;

import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Action1;

public final class OperatorDoOnNth implements Operator {

    public static  OperatorDoOnNth create(Action1 action, int n) {
        return new OperatorDoOnNth(action, n);
    }

    private final Action1 action;
    private final int n;

    private OperatorDoOnNth(Action1 action, int n) {
        this.action = action;
        this.n = n;
    }

    @Override
    public Subscriber call(final Subscriber child) {
        return new Subscriber(child) {
            int count;

            @Override
            public void onCompleted() {
                child.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                child.onError(e);
            }

            @Override
            public void onNext(T t) {
                count++;
                if (count == n) {
                    action.call(t);
                }
                child.onNext(t);
            }

        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy