org.davidmoten.kool.internal.operators.stream.DoOnDispose Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kool Show documentation
Show all versions of kool Show documentation
Streaming library supporting reuse and many operators
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();
}
}
}
};
}
}