|
22 | 22 | * Other similar events occuring within the period are simply not discarded.
|
23 | 23 | * </p>
|
24 | 24 | */
|
25 |
| -public class EventCoalescor implements Closeable { |
26 |
| - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
27 |
| - private final EventBus eventBus; |
28 |
| - private final long coalescePeriod; |
| 25 | +public abstract class EventCoalescor<T> implements Closeable { |
29 | 26 |
|
30 |
| - private final Set<Object> queue = new LinkedHashSet<>(); |
| 27 | + /** |
| 28 | + * Create an instance of an {@link EventCoalescor} which accept all kind of events, |
| 29 | + * as it is untyped. |
| 30 | + * |
| 31 | + * @param eventBus the event bus where to post processed events |
| 32 | + * @param coalescePeriod the coalesce period |
| 33 | + * @return the generic event coalescor |
| 34 | + */ |
| 35 | + public static EventCoalescor<Object> generic(EventBus eventBus, long coalescePeriod) { |
| 36 | + return new GenericEventCoalescor(eventBus, coalescePeriod); |
| 37 | + } |
31 | 38 |
|
32 |
| - public EventCoalescor(EventBus eventBus, long coalescePeriod) { |
| 39 | + final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
| 40 | + final EventBus eventBus; |
| 41 | + final long coalescePeriod; |
| 42 | + |
| 43 | + EventCoalescor(EventBus eventBus, long coalescePeriod) { |
33 | 44 | this.eventBus = eventBus;
|
34 | 45 | this.coalescePeriod = coalescePeriod;
|
35 | 46 | }
|
36 | 47 |
|
37 |
| - public void post(final Object event) { |
38 |
| - synchronized (queue) { |
39 |
| - if (queue.add(event)) { |
40 |
| - executor.schedule(new Runnable() { |
41 |
| - @Override |
42 |
| - public void run() { |
43 |
| - try { |
44 |
| - eventBus.post(event); |
45 |
| - } finally { |
46 |
| - synchronized (queue) { |
47 |
| - queue.remove(event); |
48 |
| - } |
49 |
| - } |
50 |
| - } |
51 |
| - }, coalescePeriod, TimeUnit.MILLISECONDS); |
52 |
| - } |
53 |
| - } |
54 |
| - } |
| 48 | + public abstract void post(final T event); |
55 | 49 |
|
56 | 50 | @Override
|
57 | 51 | public void close() throws IOException {
|
58 | 52 | executor.shutdownNow();
|
59 | 53 | }
|
| 54 | + |
| 55 | + /** |
| 56 | + * generic coalescor, using untyped events |
| 57 | + */ |
| 58 | + private static class GenericEventCoalescor extends EventCoalescor<Object> { |
| 59 | + private final Set<Object> queue = new LinkedHashSet<>(); |
| 60 | + |
| 61 | + private GenericEventCoalescor(EventBus eventBus, long coalescePeriod) { |
| 62 | + super(eventBus, coalescePeriod); |
| 63 | + } |
| 64 | + |
| 65 | + public void post(final Object event) { |
| 66 | + synchronized (queue) { |
| 67 | + if (queue.add(event)) { |
| 68 | + executor.schedule(new Runnable() { |
| 69 | + @Override |
| 70 | + public void run() { |
| 71 | + try { |
| 72 | + eventBus.post(event); |
| 73 | + } finally { |
| 74 | + synchronized (queue) { |
| 75 | + queue.remove(event); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + }, coalescePeriod, TimeUnit.MILLISECONDS); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
60 | 84 | }
|
0 commit comments