Skip to content

Commit a871a79

Browse files
committed
Refactor to separe init and operational code
1 parent 0610d16 commit a871a79

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/ch/powerunit/extensions/async/impl/FilePool.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public FilePool(Path directory, WatchEvent.Kind<?>... events) {
3737

3838
@Override
3939
public Collection<WatchEvent<Path>> call() throws Exception {
40-
if (watcher == null) {
41-
watcher = directory.getFileSystem().newWatchService();
42-
key = directory.register(watcher, events);
43-
}
40+
initIfNeeded();
4441
try {
4542
return key.pollEvents().stream().filter(IGNORE_OVERFLOW).map(COERCE_TO_PATH)
4643
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
@@ -53,6 +50,13 @@ public void close() {
5350
Optional.ofNullable(watcher).ifPresent(FilePool::safeCloseWatchService);
5451
}
5552

53+
private void initIfNeeded() throws IOException {
54+
if (watcher == null) {
55+
watcher = directory.getFileSystem().newWatchService();
56+
key = directory.register(watcher, events);
57+
}
58+
}
59+
5660
private static void safeCloseWatchService(WatchService watcher) {
5761
try {
5862
watcher.close();

0 commit comments

Comments
 (0)