com.aol.cyclops.streams.operators.OnePerOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-streams Show documentation
Show all versions of cyclops-streams Show documentation
Sequential Streams and Stream Utilities for Java 8
package com.aol.cyclops.streams.operators;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Stream;
import lombok.Value;
import com.aol.cyclops.streams.StreamUtils;
@Value
public class OnePerOperator {
Stream stream;
public Stream onePer( long time, TimeUnit t) {
Iterator it = stream.iterator();
long next = t.toNanos(time);
return StreamUtils.stream(new Iterator(){
volatile long last = -1;
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public T next() {
T nextValue = it.next();
LockSupport.parkNanos(next-System.nanoTime()-last);
last= System.nanoTime();
return nextValue;
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy