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