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

com.oath.cyclops.internal.stream.spliterators.SkipWhileTimeSpliterator Maven / Gradle / Ivy

The newest version!
package com.oath.cyclops.internal.stream.spliterators;

import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * Created by johnmcclean on 22/12/2016.
 */
public class SkipWhileTimeSpliterator extends Spliterators.AbstractSpliterator implements CopyableSpliterator {
    private final Spliterator source;
    private final long time;
    private final TimeUnit t;
    final long toRun;

    boolean closed = false;
    public SkipWhileTimeSpliterator(final Spliterator source, long time, TimeUnit t) {
        super(source.estimateSize(),source.characteristics() & Spliterator.ORDERED);

        this.source = source;
        this.time = time;
        this.t=t;

        toRun = t.toNanos(time);

    }
    boolean open = false;
    long start =-1;
    @Override
    public void forEachRemaining(Consumer action) {
        start = System.nanoTime();
        while(!closed){
            boolean canAdvance = source.tryAdvance(t -> {
                if(!open) {
                    open = System.nanoTime()-start >= toRun;

                    if (open)
                        action.accept(t);
                }else{
                    action.accept(t);
                }

            });
            if(!canAdvance){
                closed = true;
                return;
            }

        }


    }

    @Override
    public boolean tryAdvance(Consumer action) {
        if(closed)
            return true;
        if(start == -1) {
            start = System.nanoTime();
        }

        boolean[] sent = {false};
        for(;;) {
            boolean canAdvance = source.tryAdvance(t -> {
                if (!open) {
                    open = System.nanoTime() - start >= toRun;

                    if (open) {
                        action.accept(t);
                        sent[0]=true;
                    }
                } else {
                    action.accept(t);
                    sent[0]=true;
                }
            });

            if(!canAdvance){
                closed = true;
                return false;
            }
            if(sent[0]){
                return canAdvance;
            }
        }

    }

    @Override
    public Spliterator copy() {
        return new SkipWhileTimeSpliterator<>(CopyableSpliterator.copy(source),time,t);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy