Skip to content

Commit 4766da6

Browse files
committed
Rewrite onEvent to on
1 parent 3e4ac38 commit 4766da6

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Start using java-watch:
3737
var directory = Path.of("tmp", "test-dir");
3838
var watcherSetup = Watcher.watch(directory, WatchScope.PATH_AND_CHILDREN)
3939
.withExecutor(Executors.newCachedThreadPool()) // optionally configure a custom thread pool
40-
.onEvent(watchEvent -> {
40+
.on(watchEvent -> {
4141
System.err.println(watchEvent);
4242
});
4343

src/main/java/engineering/swat/watch/Watcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public static Watcher watch(Path path, WatchScope scope) {
9999
* @param eventHandler a callback that handles the watch event, will be called once per event.
100100
* @return this for optional method chaining
101101
*/
102-
public Watcher onEvent(Consumer<WatchEvent> eventHandler) {
102+
public Watcher on(Consumer<WatchEvent> eventHandler) {
103103
this.eventHandler = eventHandler;
104104
return this;
105105
}
106106

107107
/**
108-
* Optionally configure the executor in which the {@link #onEvent(Consumer)} callbacks are scheduled.
108+
* Optionally configure the executor in which the {@link #on(Consumer)} callbacks are scheduled.
109109
* If not defined, every task will be scheduled on the {@link java.util.concurrent.ForkJoinPool#commonPool()}.
110110
* @param callbackHandler worker pool to use
111111
* @return this for optional method chaining
@@ -119,7 +119,7 @@ public Watcher withExecutor(Executor callbackHandler) {
119119
* Start watch the path for events.
120120
* @return a subscription for the watch, when closed, new events will stop being registered to the worker pool.
121121
* @throws IOException in case the starting of the watcher caused an underlying IO exception
122-
* @throws IllegalStateException the watchers is not configured correctly (for example, missing {@link #onEvent(Consumer)}, or a watcher is started twice)
122+
* @throws IllegalStateException the watchers is not configured correctly (for example, missing {@link #on(Consumer)}, or a watcher is started twice)
123123
*/
124124
public ActiveWatch start() throws IOException {
125125
if (this.eventHandler == NULL_HANDLER) {

src/test/java/engineering/swat/watch/DeleteLockTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static void recursiveDelete(Path target) throws IOException {
7777
}
7878

7979
private void deleteAndVerify(Path target, WatchScope scope) throws IOException {
80-
try (var watch = Watcher.watch(target, scope).onEvent(ev -> {}).start()) {
80+
try (var watch = Watcher.watch(target, scope).on(ev -> {}).start()) {
8181
recursiveDelete(target);
8282
assertFalse(Files.exists(target), "The file/directory shouldn't exist anymore");
8383
}

src/test/java/engineering/swat/watch/RecursiveWatchTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void newDirectoryWithFilesChangesDetected() throws IOException {
7474
var created = new AtomicBoolean(false);
7575
var changed = new AtomicBoolean(false);
7676
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
77-
.onEvent(ev -> {
77+
.on(ev -> {
7878
logger.debug("Event received: {}", ev);
7979
if (ev.calculateFullPath().equals(target.get())) {
8080
switch (ev.getKind()) {
@@ -106,7 +106,7 @@ void correctRelativePathIsReported() throws IOException {
106106
Path relative = Path.of("a","b", "c", "d.txt");
107107
var seen = new AtomicBoolean(false);
108108
var watcher = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
109-
.onEvent(ev -> {
109+
.on(ev -> {
110110
logger.debug("Seen event: {}", ev);
111111
if (ev.getRelativePath().equals(relative)) {
112112
seen.set(true);
@@ -131,7 +131,7 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException, InterruptedExc
131131
.orElseThrow();
132132
var seen = new AtomicBoolean(false);
133133
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
134-
.onEvent(ev -> {
134+
.on(ev -> {
135135
if (ev.getKind() == Kind.DELETED && ev.calculateFullPath().equals(target)) {
136136
seen.set(true);
137137
}

src/test/java/engineering/swat/watch/SingleDirectoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException, InterruptedExc
6868
var seenDelete = new AtomicBoolean(false);
6969
var seenCreate = new AtomicBoolean(false);
7070
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
71-
.onEvent(ev -> {
71+
.on(ev -> {
7272
if (ev.getKind() == Kind.DELETED && ev.calculateFullPath().equals(target)) {
7373
seenDelete.set(true);
7474
}

src/test/java/engineering/swat/watch/SingleFileTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void singleFileShouldNotTriggerOnOtherFilesInSameDir() throws IOException, Inter
6666
var seen = new AtomicBoolean(false);
6767
var others = new AtomicBoolean(false);
6868
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
69-
.onEvent(ev -> {
69+
.on(ev -> {
7070
if (ev.calculateFullPath().equals(target)) {
7171
seen.set(true);
7272
}
@@ -95,7 +95,7 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep
9595
var seen = new AtomicBoolean(false);
9696
var others = new AtomicBoolean(false);
9797
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
98-
.onEvent(ev -> {
98+
.on(ev -> {
9999
if (ev.calculateFullPath().equals(target)) {
100100
seen.set(true);
101101
}

src/test/java/engineering/swat/watch/SmokeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void watchDirectory() throws IOException, InterruptedException {
6767
var changed = new AtomicBoolean(false);
6868
var target = testDir.getTestFiles().get(0);
6969
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
70-
.onEvent(ev -> {if (ev.getKind() == MODIFIED && ev.calculateFullPath().equals(target)) { changed.set(true); }})
70+
.on(ev -> {if (ev.getKind() == MODIFIED && ev.calculateFullPath().equals(target)) { changed.set(true); }})
7171
;
7272

7373
try (var activeWatch = watchConfig.start() ) {
@@ -84,7 +84,7 @@ void watchRecursiveDirectory() throws IOException, InterruptedException {
8484
.findFirst()
8585
.orElseThrow();
8686
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
87-
.onEvent(ev -> { if (ev.getKind() == MODIFIED && ev.calculateFullPath().equals(target)) { changed.set(true);}})
87+
.on(ev -> { if (ev.getKind() == MODIFIED && ev.calculateFullPath().equals(target)) { changed.set(true);}})
8888
;
8989

9090
try (var activeWatch = watchConfig.start() ) {
@@ -102,7 +102,7 @@ void watchSingleFile() throws IOException {
102102
.orElseThrow();
103103

104104
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
105-
.onEvent(ev -> {
105+
.on(ev -> {
106106
if (ev.calculateFullPath().equals(target)) {
107107
changed.set(true);
108108
}

src/test/java/engineering/swat/watch/TortureTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void pressureOnFSShouldNotMissNewFilesAnything() throws InterruptedException, IO
148148
var seenCreates = ConcurrentHashMap.<Path>newKeySet();
149149
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
150150
.withExecutor(pool)
151-
.onEvent(ev -> {
151+
.on(ev -> {
152152
var fullPath = ev.calculateFullPath();
153153
switch (ev.getKind()) {
154154
case CREATED:
@@ -212,7 +212,7 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
212212
try {
213213
var watcher = Watcher
214214
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
215-
.onEvent(e -> seen.add(e.calculateFullPath()));
215+
.on(e -> seen.add(e.calculateFullPath()));
216216
startRegistering.acquire();
217217
try (var c = watcher.start()) {
218218
startedWatching.release();
@@ -279,7 +279,7 @@ void manyRegisterAndUnregisterSameTime() throws InterruptedException, IOExceptio
279279
for (int k = 0; k < 1000; k++) {
280280
var watcher = Watcher
281281
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
282-
.onEvent(e -> {
282+
.on(e -> {
283283
if (e.calculateFullPath().equals(target)) {
284284
seen.add(id);
285285
}
@@ -345,7 +345,7 @@ void pressureOnFSShouldNotMissDeletes() throws InterruptedException, IOException
345345
final var happened = new Semaphore(0);
346346
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
347347
.withExecutor(pool)
348-
.onEvent(ev -> {
348+
.on(ev -> {
349349
events.getAndIncrement();
350350
happened.release();
351351
var fullPath = ev.calculateFullPath();

0 commit comments

Comments
 (0)