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

com.aol.cyclops.streams.operators.DebounceOperator 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.stream.Stream;

import lombok.Value;

import com.aol.cyclops.streams.StreamUtils;
@Value
public class DebounceOperator {
	Stream stream;
	public Stream debounce(long time, TimeUnit t){
		Iterator it = stream.iterator();
		long timeNanos = t.toNanos(time);
		return StreamUtils.stream(new Iterator(){
			volatile long last = 0;
			@Override
			public boolean hasNext() {
				return it.hasNext();
			}
			@Override
			public T next() {
				long elapsedNanos = 1;
				T nextValue=null;
				while(elapsedNanos>0 && it.hasNext()){
						
						nextValue = it.next();
						if(last==0)
							last= System.nanoTime();
						elapsedNanos= timeNanos - (System.nanoTime()-last);
				}
				
				
				
				last= System.nanoTime();
				return nextValue;
			}
			
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy