For some code I\'m writing I could use a nice general implementation of debounce
in Java.
public interface Callback {
public void call(Object
I don't know if it exists but it should be simple to implement.
class Debouncer implements Callback {
private CallBack c;
private volatile long lastCalled;
private int interval;
public Debouncer(Callback c, int interval) {
//init fields
}
public void call(Object arg) {
if( lastCalled + interval < System.currentTimeMillis() ) {
lastCalled = System.currentTimeMillis();
c.call( arg );
}
}
}
Of course this example oversimplifies it a bit, but this is more or less all you need. If you want to keep separate timeouts for different arguments, you'll need a Map
instead of just a long
to keep track of the last execution time.