For some code I\'m writing I could use a nice general implementation of debounce in Java.
public interface Callback {
public void call(Object
This looks like it could work:
class Debouncer implements Callback {
private Callback callback;
private Map scheduled = new HashMap();
private int delay;
public Debouncer(Callback c, int delay) {
this.callback = c;
this.delay = delay;
}
public void call(final Object arg) {
final int h = arg.hashCode();
Timer task = scheduled.remove(h);
if (task != null) { task.cancel(); }
task = new Timer();
scheduled.put(h, task);
task.schedule(new TimerTask() {
@Override
public void run() {
callback.call(arg);
scheduled.remove(h);
}
}, this.delay);
}
}