Skip to content

Commit bd95736

Browse files
committed
[breaking] EventCoalescor is now abstract and generified
The default implementation, the old EventCoalescor, is implemented as a private static inner class of the EventCoalescor, it's an implementation called "generic". It might be built, using the factory method: "EventCoalescor.generic" So FileWatchEventCoalescor inherit from EventCoalescor for the type FileWatchEvent.
1 parent 59454c0 commit bd95736

File tree

5 files changed

+58
-47
lines changed

5 files changed

+58
-47
lines changed

restx-barbarywatch/src/main/java/restx/common/watch/BarbaryWatchService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static class WatchDir implements Closeable{
4949
private final WatchService watcher;
5050
private final Map<WatchKey, Path> keys;
5151
private final boolean recursive;
52-
private final EventCoalescor coalescor;
52+
private final EventCoalescor<Object> coalescor;
5353
private final Path root;
5454
private boolean trace = false;
5555

@@ -98,7 +98,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
9898
this.keys = new HashMap<>();
9999
this.recursive = settings.recurse();
100100
this.root = dir;
101-
this.coalescor = new EventCoalescor(eventBus, settings.coalescePeriod());
101+
this.coalescor = EventCoalescor.generic(eventBus, settings.coalescePeriod());
102102

103103
if (recursive) {
104104
registerAll(dir);

restx-common/src/main/java/restx/common/watch/EventCoalescor.java

+48-24
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,63 @@
2222
* Other similar events occuring within the period are simply not discarded.
2323
* </p>
2424
*/
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 {
2926

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+
}
3138

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) {
3344
this.eventBus = eventBus;
3445
this.coalescePeriod = coalescePeriod;
3546
}
3647

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);
5549

5650
@Override
5751
public void close() throws IOException {
5852
executor.shutdownNow();
5953
}
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+
}
6084
}

restx-common/src/main/java/restx/common/watch/FileWatchEventCoalescor.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
import com.google.common.eventbus.EventBus;
44

5-
import java.io.Closeable;
6-
import java.io.IOException;
75
import java.nio.file.Path;
86
import java.nio.file.StandardWatchEventKinds;
97
import java.util.ArrayDeque;
108
import java.util.Deque;
119
import java.util.HashMap;
12-
import java.util.concurrent.Executors;
13-
import java.util.concurrent.ScheduledExecutorService;
1410
import java.util.concurrent.TimeUnit;
1511

1612
/**
@@ -25,17 +21,12 @@
2521
*
2622
* @author apeyrard
2723
*/
28-
public class FileWatchEventCoalescor implements Closeable {
29-
30-
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
31-
private final EventBus eventBus;
32-
private final long coalescePeriod;
24+
public class FileWatchEventCoalescor extends EventCoalescor<FileWatchEvent> {
3325

3426
private final HashMap<FileWatchEventKey, Deque<EventReference>> queue = new HashMap<>();
3527

3628
public FileWatchEventCoalescor(EventBus eventBus, long coalescePeriod) {
37-
this.eventBus = eventBus;
38-
this.coalescePeriod = coalescePeriod;
29+
super(eventBus, coalescePeriod);
3930
}
4031

4132
/**
@@ -157,11 +148,6 @@ void clear() {
157148
}
158149
}
159150

160-
@Override
161-
public void close() throws IOException {
162-
executor.shutdownNow();
163-
}
164-
165151
/**
166152
* key used for the storage of an event, composed by file paths, two event with same keys, are for the same physical file
167153
*/

restx-common/src/main/java/restx/common/watch/StdWatcherService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static class WatchDir implements Closeable {
4444
private final WatchService watcher;
4545
private final Map<WatchKey,Path> keys;
4646
private final boolean recursive;
47-
private final EventCoalescor coalescor;
47+
private final EventCoalescor<Object> coalescor;
4848
private final Path root;
4949
private boolean trace = false;
5050

@@ -96,7 +96,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
9696
this.keys = new HashMap<>();
9797
this.recursive = settings.recurse();
9898
this.root = dir;
99-
this.coalescor = new EventCoalescor(eventBus, settings.coalescePeriod());
99+
this.coalescor = EventCoalescor.generic(eventBus, settings.coalescePeriod());
100100

101101
if (recursive) {
102102
registerAll(dir);

restx-common/src/test/java/restx/common/watch/EventCoalescorTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package restx.common.watch;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
36
import com.google.common.eventbus.EventBus;
47
import com.google.common.eventbus.Subscribe;
58
import org.junit.Test;
69

710
import java.util.ArrayList;
811
import java.util.List;
912

10-
import static org.assertj.core.api.Assertions.assertThat;
11-
1213
/**
1314
* User: xavierhanin
1415
* Date: 9/11/13
@@ -27,7 +28,7 @@ public void onMessage(String msg) {
2728
}
2829
});
2930

30-
EventCoalescor coalescor = new EventCoalescor(eventBus, 30);
31+
EventCoalescor coalescor = EventCoalescor.generic(eventBus, 30);
3132

3233
coalescor.post("test1");
3334
coalescor.post("test2");

0 commit comments

Comments
 (0)