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

com.aol.cyclops.streams.operators.OnePerOperator Maven / Gradle / Ivy

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