Created
June 4, 2016 22:21
-
-
Save sinnlosername/e0d9bcb9645048fddcc87082645b0343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Delay<K> { | |
private final HashMap<K, Long> map = new HashMap<>(); | |
private final long min_delay; | |
public Delay(long min_delay) { | |
this.min_delay = min_delay; | |
} | |
public void trigger(K key, Handler handler) { | |
if (map.containsKey(key)) { | |
long delay = System.currentTimeMillis()-map.get(key); | |
if (delay > min_delay) | |
handler.passed(key, delay); | |
else | |
handler.failed(key, delay); | |
} else { | |
map.put(key, System.currentTimeMillis()); | |
} | |
} | |
public abstract class Handler { | |
abstract void passed(K t, long delay); | |
abstract void failed(K t, long delay); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment