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

org.davidmoten.kool.internal.operators.stream.DoOnDispose Maven / Gradle / Ivy

There is a newer version: 0.1.35
Show newest version
package org.davidmoten.kool.internal.operators.stream;

import org.davidmoten.kool.Stream;
import org.davidmoten.kool.StreamIterator;
import org.davidmoten.kool.function.Action;

public final class DoOnDispose implements Stream {

    private final Action action;
    private final Stream source;
    private final boolean before;

    public DoOnDispose(Action action, Stream source, boolean before) {
        this.action = action;
        this.source = source;
        this.before = before;
    }

    @Override
    public StreamIterator iterator() {
        return new StreamIterator() {

            StreamIterator it = source.iteratorNullChecked();

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public T next() {
                return it.nextNullChecked();
            }

            @Override
            public void dispose() {
                if (it != null) {
                    if (before) {
                        action.callUnchecked();
                    }
                    it.dispose();
                    it = null;
                    if (!before) {
                        action.callUnchecked();
                    }
                }
            }

        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy