-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c347b40
commit c80a5cb
Showing
3 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.treasuredata.android; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
class Debouncer <T> { | ||
interface Callback<T> { | ||
void call(T arg); | ||
} | ||
|
||
private final ScheduledExecutorService sched = Executors.newScheduledThreadPool(1); | ||
private final ConcurrentHashMap<T, TimerTask> delayedMap = new ConcurrentHashMap<T, TimerTask>(); | ||
private final Callback<T> callback; | ||
private final int interval; | ||
|
||
Debouncer(Callback<T> c, int interval) { | ||
this.callback = c; | ||
this.interval = interval; | ||
} | ||
|
||
void call(T key) { | ||
TimerTask task = new TimerTask(key); | ||
|
||
TimerTask prev; | ||
do { | ||
prev = delayedMap.putIfAbsent(key, task); | ||
if (prev == null) | ||
sched.schedule(task, interval, TimeUnit.MILLISECONDS); | ||
} while (prev != null && !prev.extend()); // Exit only if new task was added to map, or existing task was extended successfully | ||
} | ||
|
||
void terminate() { | ||
sched.shutdownNow(); | ||
} | ||
|
||
// The task that wakes up when the wait time elapses | ||
private class TimerTask implements Runnable { | ||
private final T key; | ||
private long dueTime; | ||
private final Object lock = new Object(); | ||
|
||
TimerTask(T key) { | ||
this.key = key; | ||
extend(); | ||
} | ||
|
||
boolean extend() { | ||
synchronized (lock) { | ||
if (dueTime < 0) // Task has been shutdown | ||
return false; | ||
dueTime = System.currentTimeMillis() + interval; | ||
return true; | ||
} | ||
} | ||
|
||
public void run() { | ||
synchronized (lock) { | ||
long remaining = dueTime - System.currentTimeMillis(); | ||
if (remaining > 0) { // Re-schedule task | ||
sched.schedule(this, remaining, TimeUnit.MILLISECONDS); | ||
} else { // Mark as terminated and invoke callback | ||
dueTime = -1; | ||
try { | ||
callback.call(key); | ||
} finally { | ||
delayedMap.remove(key); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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