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

apoc.util.QueueBasedSpliterator Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
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 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