refactor(LocalRunnerClient): co-locate watch service builder internals

Context:
Group WatchService build interfaces and registry implementation into a
single internal structure for better cohesion.
This commit is contained in:
2025-12-29 18:40:20 +08:00
parent e3294ec302
commit db3435fccf

View File

@@ -272,72 +272,71 @@ public class LocalRunnerClient extends RunnerClient {
})); }));
} }
private WatchServiceBuild registerWatchService(Path path) { private LocalWatchServiceBuild registerWatchService(Path path) {
return new LocalWatchServiceRegistry(path, watchService, executor); return new LocalWatchServiceBuild.BuildRegistry(path, watchService, executor);
} }
private interface WatchServiceBuild { private interface LocalWatchServiceBuild {
WatchServiceBuild registerCreate(WatchEventHandler handler); LocalWatchServiceBuild registerCreate(EventHandler handler);
WatchServiceBuild registerModify(WatchEventHandler handler); LocalWatchServiceBuild registerModify(EventHandler handler);
WatchServiceBuild registerDelete(WatchEventHandler handler); LocalWatchServiceBuild registerDelete(EventHandler handler);
WatchServiceBuild registerOverflow(WatchEventHandler handler); LocalWatchServiceBuild registerOverflow(EventHandler handler);
WatchServiceBuild initialLoad(WatchInitLoader loader); LocalWatchServiceBuild initialLoad(InitLoader loader);
void commit(); void commit();
}
private interface WatchEventHandler { interface EventHandler {
void handle(Path thisDir, Path context); void handle(Path thisDir, Path context);
} }
private interface WatchInitLoader { interface InitLoader {
void load(Path path); void load(Path path);
} }
private static class LocalWatchServiceRegistry implements WatchServiceBuild { class BuildRegistry implements LocalWatchServiceBuild {
private final Map<WatchEvent.Kind<?>, WatchEventHandler> handlers = new HashMap<>(); private final Map<WatchEvent.Kind<?>, EventHandler> handlers = new HashMap<>();
private final Path path; private final Path path;
private final WatchService watchService; private final WatchService watchService;
private final ExecutorService executor; private final ExecutorService executor;
private WatchInitLoader initLoader; private InitLoader initLoader;
private LocalWatchServiceRegistry(Path path, WatchService watchService, ExecutorService executor) { private BuildRegistry(Path path, WatchService watchService, ExecutorService executor) {
this.path = path; this.path = path;
this.watchService = watchService; this.watchService = watchService;
this.executor = executor; this.executor = executor;
} }
@Override @Override
public WatchServiceBuild registerCreate(WatchEventHandler handler) { public LocalWatchServiceBuild registerCreate(EventHandler handler) {
handlers.put(StandardWatchEventKinds.ENTRY_CREATE, handler); handlers.put(StandardWatchEventKinds.ENTRY_CREATE, handler);
return this; return this;
} }
@Override @Override
public WatchServiceBuild registerModify(WatchEventHandler handler) { public LocalWatchServiceBuild registerModify(EventHandler handler) {
handlers.put(StandardWatchEventKinds.ENTRY_MODIFY, handler); handlers.put(StandardWatchEventKinds.ENTRY_MODIFY, handler);
return this; return this;
} }
@Override @Override
public WatchServiceBuild registerDelete(WatchEventHandler handler) { public LocalWatchServiceBuild registerDelete(EventHandler handler) {
handlers.put(StandardWatchEventKinds.ENTRY_DELETE, handler); handlers.put(StandardWatchEventKinds.ENTRY_DELETE, handler);
return this; return this;
} }
@Override @Override
public WatchServiceBuild registerOverflow(WatchEventHandler handler) { public LocalWatchServiceBuild registerOverflow(EventHandler handler) {
handlers.put(StandardWatchEventKinds.OVERFLOW, handler); handlers.put(StandardWatchEventKinds.OVERFLOW, handler);
return this; return this;
} }
@Override @Override
public WatchServiceBuild initialLoad(WatchInitLoader loader) { public LocalWatchServiceBuild initialLoad(InitLoader loader) {
initLoader = loader; initLoader = loader;
return this; return this;
} }
@@ -366,7 +365,7 @@ public class LocalRunnerClient extends RunnerClient {
// 若事件所在目录不为为 path忽略并步入下一轮循环 // 若事件所在目录不为为 path忽略并步入下一轮循环
continue; continue;
} }
WatchEventHandler handler = handlers.get(kind); EventHandler handler = handlers.get(kind);
if (handler == null) { if (handler == null) {
continue; continue;
} }
@@ -385,6 +384,7 @@ public class LocalRunnerClient extends RunnerClient {
} }
} }
}
private sealed static abstract class LocalWatchServiceHelper permits LocalWatchServiceHelper.Dynamic, LocalWatchServiceHelper.Desc, LocalWatchServiceHelper.Common { private sealed static abstract class LocalWatchServiceHelper permits LocalWatchServiceHelper.Dynamic, LocalWatchServiceHelper.Desc, LocalWatchServiceHelper.Common {
@@ -394,15 +394,15 @@ public class LocalRunnerClient extends RunnerClient {
this.existedMetaActions = existedMetaActions; this.existedMetaActions = existedMetaActions;
} }
protected abstract @NotNull WatchInitLoader buildLoad(); protected abstract @NotNull LocalWatchServiceBuild.InitLoader buildLoad();
protected abstract @NotNull WatchEventHandler buildModify(); protected abstract @NotNull LocalWatchServiceBuild.EventHandler buildModify();
protected abstract @NotNull WatchEventHandler buildCreate(); protected abstract @NotNull LocalWatchServiceBuild.EventHandler buildCreate();
protected abstract @NotNull WatchEventHandler buildDelete(); protected abstract @NotNull LocalWatchServiceBuild.EventHandler buildDelete();
protected abstract @NotNull WatchEventHandler buildOverflow(); protected abstract @NotNull LocalWatchServiceBuild.EventHandler buildOverflow();
private static final class Dynamic extends LocalWatchServiceHelper { private static final class Dynamic extends LocalWatchServiceHelper {
@@ -417,29 +417,31 @@ public class LocalRunnerClient extends RunnerClient {
@NotNull @NotNull
protected WatchInitLoader buildLoad() { protected WatchInitLoader buildLoad() {
return null; return null;
protected LocalWatchServiceBuild.InitLoader buildLoad() {
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildModify() { protected WatchEventHandler buildModify() {
return null; return null;
protected LocalWatchServiceBuild.EventHandler buildModify() {
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildCreate() { protected LocalWatchServiceBuild.EventHandler buildCreate() {
return buildModify();
}
@Override
@NotNull
protected LocalWatchServiceBuild.EventHandler buildDelete() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildDelete() { protected LocalWatchServiceBuild.EventHandler buildOverflow() {
return null;
}
@Override
@NotNull
protected WatchEventHandler buildOverflow() {
return null; return null;
} }
} }
@@ -455,31 +457,31 @@ public class LocalRunnerClient extends RunnerClient {
@Override @Override
@NotNull @NotNull
protected WatchInitLoader buildLoad() { protected LocalWatchServiceBuild.InitLoader buildLoad() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildModify() { protected LocalWatchServiceBuild.EventHandler buildModify() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildCreate() { protected LocalWatchServiceBuild.EventHandler buildCreate() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildDelete() { protected LocalWatchServiceBuild.EventHandler buildDelete() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildOverflow() { protected LocalWatchServiceBuild.EventHandler buildOverflow() {
return null; return null;
} }
} }
@@ -495,31 +497,31 @@ public class LocalRunnerClient extends RunnerClient {
@Override @Override
@NotNull @NotNull
protected WatchInitLoader buildLoad() { protected LocalWatchServiceBuild.InitLoader buildLoad() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildModify() { protected LocalWatchServiceBuild.EventHandler buildModify() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildCreate() { protected LocalWatchServiceBuild.EventHandler buildCreate() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildDelete() { protected LocalWatchServiceBuild.EventHandler buildDelete() {
return null; return null;
} }
@Override @Override
@NotNull @NotNull
protected WatchEventHandler buildOverflow() { protected LocalWatchServiceBuild.EventHandler buildOverflow() {
return null; return null;
} }
} }