|
| 1 | +package org.baderlab.csplugins.enrichmentmap.util; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.Future; |
| 9 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * A timer that can be used to coalesce multiple quick calls to a handler in order |
| 15 | + * to avoid running the same task in quick succession. |
| 16 | + */ |
| 17 | +public class CoalesceTimer { |
| 18 | + |
| 19 | + public static final int DEFAULT_DELAY = 120; |
| 20 | + public static final int DEFAULT_THREADS = 1; |
| 21 | + |
| 22 | + private static final Object DEFAULT_KEY = new Object(); |
| 23 | + |
| 24 | + private final Map<Object,Future<?>> store; |
| 25 | + |
| 26 | + private final ScheduledThreadPoolExecutor executor; |
| 27 | + private final int delay; // milliseconds |
| 28 | + |
| 29 | + |
| 30 | + public CoalesceTimer() { |
| 31 | + this(DEFAULT_DELAY, DEFAULT_THREADS); |
| 32 | + } |
| 33 | + |
| 34 | + public CoalesceTimer(int delay, int threads) { |
| 35 | + // Synchronize the store because the future runs on a different thread than the one calling coalesce(). |
| 36 | + this.store = Collections.synchronizedMap(new HashMap<>()); |
| 37 | + this.delay = delay; |
| 38 | + |
| 39 | + executor = new ScheduledThreadPoolExecutor(threads, r -> { |
| 40 | + Thread thread = Executors.defaultThreadFactory().newThread(r); |
| 41 | + thread.setName(CoalesceTimer.class.getSimpleName() + "_" + thread.getName()); |
| 42 | + return thread; |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Starts a timer that will run the runnable after a short delay. If another |
| 49 | + * call to this method occurs before the timer expires the timer will be |
| 50 | + * reset. The result is multiple quick calls to this method where the time between |
| 51 | + * the calls is less than the delay will result in the runnable running once. |
| 52 | + * <br><br> |
| 53 | + * It is recommended to use a single thread if this method is preferred over |
| 54 | + * {@link CoalesceTimer#coalesce(Object, Runnable)} |
| 55 | + * |
| 56 | + * <pre> |
| 57 | + * public void handleEvent(RowsSetEvent e) { |
| 58 | + * if(e.containsColumn(CyNetwork.SELECTED)) { |
| 59 | + * coalesceTimer.coalesce(() -> updateUI()); |
| 60 | + * } |
| 61 | + * } |
| 62 | + * </pre> |
| 63 | + */ |
| 64 | + public synchronized void coalesce(Runnable runnable) { |
| 65 | + coalesce(DEFAULT_KEY, runnable); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Starts a timer that will run the runnable after a short delay. If another |
| 70 | + * call to this method occurs before the timer expires the timer will be |
| 71 | + * reset. The result is multiple quick calls to this method where the time between |
| 72 | + * the calls is less than the delay will result in the runnable running once. |
| 73 | + * |
| 74 | + * * <pre> |
| 75 | + * public void handleEvent(RowsSetEvent e) { |
| 76 | + * if(e.containsColumn(CyNetwork.SELECTED)) { |
| 77 | + * CyNetworkView networkView = applicationManager.getCurrentNetworkView(); |
| 78 | + * if(networkView != null) { |
| 79 | + * coalesceTimer.coalesce(networkView, () -> updateUI(networkView)); |
| 80 | + * } |
| 81 | + * } |
| 82 | + * } |
| 83 | + * </pre> |
| 84 | + */ |
| 85 | + public synchronized void coalesce(Object key, Runnable runnable) { |
| 86 | + Objects.requireNonNull(key); |
| 87 | + Objects.requireNonNull(runnable); |
| 88 | + |
| 89 | + Future<?> future = store.get(key); |
| 90 | + if(future != null) { |
| 91 | + future.cancel(false); |
| 92 | + } |
| 93 | + |
| 94 | + Runnable r = () -> { |
| 95 | + store.remove(key); |
| 96 | + runnable.run(); |
| 97 | + }; |
| 98 | + |
| 99 | + future = executor.schedule(r, delay, TimeUnit.MILLISECONDS); |
| 100 | + store.put(key, future); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public void shutdown() { |
| 105 | + executor.shutdown(); |
| 106 | + } |
| 107 | + |
| 108 | + public boolean isShutdown() { |
| 109 | + return executor.isShutdown(); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments