apoc.util.QueueBasedSpliterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apoc-common Show documentation
Show all versions of apoc-common Show documentation
Data types package for Neo4j Procedures
package apoc.util;
import org.neo4j.procedure.TerminationGuard;
import java.util.Spliterator;
import java.util.concurrent.BlockingQueue;
import java.util.function.Consumer;
/**
* @author mh
* @since 06.12.17
*/
public class QueueBasedSpliterator implements Spliterator {
private final BlockingQueue queue;
private T tombstone;
private TerminationGuard terminationGuard;
private volatile boolean foundTombstone = false;
private final int timeoutSeconds;
public QueueBasedSpliterator(BlockingQueue queue, T tombstone, TerminationGuard terminationGuard, int timeoutSeconds) {
this.queue = queue;
this.tombstone = tombstone;
this.terminationGuard = terminationGuard;
this.timeoutSeconds = timeoutSeconds;
}
@Override
public boolean tryAdvance(Consumer super T> action) {
if (foundTombstone) return false;
terminationGuard.check();
T element = QueueUtil.take(queue, timeoutSeconds, () -> terminationGuard.check());
if (element.equals(tombstone)) {
foundTombstone = true;
return false;
} else {
action.accept(element);
return true;
}
}
public Spliterator trySplit() {
return null;
}
public long estimateSize() {
return Long.MAX_VALUE;
}
public int characteristics() {
return NONNULL;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy