Skip to content

Commit 3672d34

Browse files
committed
Refactored top level name of the watcher function
1 parent 231f003 commit 3672d34

File tree

10 files changed

+37
-27
lines changed

10 files changed

+37
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Start using java-watch:
4343

4444
```java
4545
var directory = Path.of("tmp", "test-dir");
46-
var watcherSetup = Watcher.watch(directory, WatchScope.PATH_AND_CHILDREN)
46+
var watcherSetup = Watcher.build(directory, WatchScope.PATH_AND_CHILDREN)
4747
.withExecutor(Executors.newCachedThreadPool()) // optionally configure a custom thread pool
4848
.onOverflow(Approximation.DIFF) // optionally configure a handler for overflows
4949
.on(watchEvent -> {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,23 @@ private Watcher(Path path, WatchScope scope) {
7070
this.scope = scope;
7171
}
7272

73+
/**
74+
* Watch a path for updates, optionally also get events for its children/descendants
75+
* @deprecated please upgrade to {@linkplain #build}
76+
*
77+
*/
78+
@Deprecated(since = "0.6.0", forRemoval = true)
79+
public static Watcher watch(Path path, WatchScope scope) {
80+
return build(path, scope);
81+
}
82+
7383
/**
7484
* Watch a path for updates, optionally also get events for its children/descendants
7585
* @param path which absolute path to monitor, can be a file or a directory, but has to be absolute
7686
* @param scope for directories you can also choose to monitor it's direct children or all it's descendants
7787
* @throws IllegalArgumentException in case a path is not supported (in relation to the scope)
7888
*/
79-
public static Watcher watch(Path path, WatchScope scope) {
89+
public static Watcher build(Path path, WatchScope scope) {
8090
if (!path.isAbsolute()) {
8191
throw new IllegalArgumentException("We can only watch absolute paths");
8292
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void setupEverything() {
6262
void noDuplicateEvents() {
6363
assertThrowsExactly(IllegalArgumentException.class, () ->
6464
Watcher
65-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
65+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
6666
.on(System.out::println)
6767
.on(System.err::println)
6868
);
@@ -72,15 +72,15 @@ void noDuplicateEvents() {
7272
void onlyDirectoryWatchingOnDirectories() {
7373
assertThrowsExactly(IllegalArgumentException.class, () ->
7474
Watcher
75-
.watch(testDir.getTestFiles().get(0), WatchScope.PATH_AND_CHILDREN)
75+
.build(testDir.getTestFiles().get(0), WatchScope.PATH_AND_CHILDREN)
7676
);
7777
}
7878

7979
@Test
8080
void doNotStartWithoutEventHandler() {
8181
assertThrowsExactly(IllegalStateException.class, () ->
8282
Watcher
83-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
83+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
8484
.start()
8585
);
8686
}
@@ -91,7 +91,7 @@ void noRelativePaths() {
9191

9292
assertThrowsExactly(IllegalArgumentException.class, () ->
9393
Watcher
94-
.watch(relativePath, WatchScope.PATH_AND_CHILDREN)
94+
.build(relativePath, WatchScope.PATH_AND_CHILDREN)
9595
.start()
9696
);
9797
}
@@ -100,7 +100,7 @@ void noRelativePaths() {
100100
void nonExistingDirectory() throws IOException {
101101
var nonExistingDir = testDir.getTestDirectory().resolve("testd1");
102102
Files.createDirectory(nonExistingDir);
103-
var w = Watcher.watch(nonExistingDir, WatchScope.PATH_AND_CHILDREN);
103+
var w = Watcher.build(nonExistingDir, WatchScope.PATH_AND_CHILDREN);
104104
Files.delete(nonExistingDir);
105105
assertThrowsExactly(IllegalStateException.class, w::start);
106106
}

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

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

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void newDirectoryWithFilesChangesDetected() throws IOException {
7777
var target = new AtomicReference<Path>();
7878
var created = new AtomicBoolean(false);
7979
var changed = new AtomicBoolean(false);
80-
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
80+
var watchConfig = Watcher.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
8181
.on(ev -> {
8282
logger.debug("Event received: {}", ev);
8383
if (ev.calculateFullPath().equals(target.get())) {
@@ -109,7 +109,7 @@ void newDirectoryWithFilesChangesDetected() throws IOException {
109109
void correctRelativePathIsReported() throws IOException {
110110
Path relative = Path.of("a","b", "c", "d.txt");
111111
var seen = new AtomicBoolean(false);
112-
var watcher = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
112+
var watcher = Watcher.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
113113
.on(ev -> {
114114
logger.debug("Seen event: {}", ev);
115115
if (ev.getRelativePath().equals(relative)) {
@@ -134,7 +134,7 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
134134
.findAny()
135135
.orElseThrow();
136136
var seen = new AtomicBoolean(false);
137-
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
137+
var watchConfig = Watcher.build(target.getParent(), WatchScope.PATH_AND_CHILDREN)
138138
.on(ev -> {
139139
if (ev.getKind() == Kind.DELETED && ev.calculateFullPath().equals(target)) {
140140
seen.set(true);
@@ -160,7 +160,7 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
160160
// Configure and start watch
161161
var dropEvents = new AtomicBoolean(false); // Toggles overflow simulation
162162
var bookkeeper = new TestHelper.Bookkeeper();
163-
var watchConfig = Watcher.watch(parent, WatchScope.PATH_AND_ALL_DESCENDANTS)
163+
var watchConfig = Watcher.build(parent, WatchScope.PATH_AND_ALL_DESCENDANTS)
164164
.withExecutor(ForkJoinPool.commonPool())
165165
.filter(e -> !dropEvents.get())
166166
.onOverflow(whichFiles)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
7171
var target = testDir.getTestFiles().get(0);
7272
var seenDelete = new AtomicBoolean(false);
7373
var seenCreate = new AtomicBoolean(false);
74-
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
74+
var watchConfig = Watcher.build(target.getParent(), WatchScope.PATH_AND_CHILDREN)
7575
.on(ev -> {
7676
if (ev.getKind() == Kind.DELETED && ev.calculateFullPath().equals(target)) {
7777
seenDelete.set(true);
@@ -99,7 +99,7 @@ void alternativeAPITest() throws IOException {
9999
var target = testDir.getTestFiles().get(0);
100100
var seenDelete = new AtomicBoolean(false);
101101
var seenCreate = new AtomicBoolean(false);
102-
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
102+
var watchConfig = Watcher.build(target.getParent(), WatchScope.PATH_AND_CHILDREN)
103103
.on(new WatchEventListener() {
104104
@Override
105105
public void onCreated(WatchEvent ev) {
@@ -132,7 +132,7 @@ void memorylessRescanOnOverflow() throws IOException, InterruptedException {
132132
Files.writeString(directory.resolve("b.txt"), "bar");
133133

134134
var bookkeeper = new TestHelper.Bookkeeper();
135-
var watchConfig = Watcher.watch(directory, WatchScope.PATH_AND_CHILDREN)
135+
var watchConfig = Watcher.build(directory, WatchScope.PATH_AND_CHILDREN)
136136
.onOverflow(Approximation.ALL)
137137
.on(bookkeeper);
138138

@@ -169,7 +169,7 @@ void indexingRescanOnOverflow() throws IOException, InterruptedException {
169169

170170
var bookkeeper = new TestHelper.Bookkeeper();
171171
var dropEvents = new AtomicBoolean(false); // Toggles overflow simulation
172-
var watchConfig = Watcher.watch(directory, WatchScope.PATH_AND_CHILDREN)
172+
var watchConfig = Watcher.build(directory, WatchScope.PATH_AND_CHILDREN)
173173
.filter(e -> !dropEvents.get())
174174
.onOverflow(Approximation.DIFF)
175175
.on(bookkeeper);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void singleFileShouldNotTriggerOnOtherFilesInSameDir() throws IOException, Inter
7171
var target = testDir.getTestFiles().get(0);
7272
var seen = new AtomicBoolean(false);
7373
var others = new AtomicBoolean(false);
74-
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
74+
var watchConfig = Watcher.build(target, WatchScope.PATH_ONLY)
7575
.on(ev -> {
7676
if (ev.calculateFullPath().equals(target)) {
7777
seen.set(true);
@@ -100,7 +100,7 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep
100100
var target = testDir.getTestDirectory();
101101
var seen = new AtomicBoolean(false);
102102
var others = new AtomicBoolean(false);
103-
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
103+
var watchConfig = Watcher.build(target, WatchScope.PATH_ONLY)
104104
.on(ev -> {
105105
if (ev.calculateFullPath().equals(target)) {
106106
seen.set(true);
@@ -162,7 +162,7 @@ private ActiveWatch startWatchAndTriggerOverflow(Approximation whichFiles, TestH
162162
var file = parent.resolve("a.txt");
163163

164164
var watch = Watcher
165-
.watch(file, WatchScope.PATH_ONLY)
165+
.build(file, WatchScope.PATH_ONLY)
166166
.onOverflow(whichFiles)
167167
.on(bookkeeper)
168168
.start();

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

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

@@ -82,7 +82,7 @@ void watchRecursiveDirectory() throws IOException {
8282
.filter(p -> !p.getParent().equals(testDir.getTestDirectory()))
8383
.findFirst()
8484
.orElseThrow();
85-
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
85+
var watchConfig = Watcher.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
8686
.on(ev -> { if (ev.getKind() == MODIFIED && ev.calculateFullPath().equals(target)) { changed.set(true);}})
8787
;
8888

@@ -100,7 +100,7 @@ void watchSingleFile() throws IOException {
100100
.findFirst()
101101
.orElseThrow();
102102

103-
var watchConfig = Watcher.watch(target, WatchScope.PATH_ONLY)
103+
var watchConfig = Watcher.build(target, WatchScope.PATH_ONLY)
104104
.on(ev -> {
105105
if (ev.calculateFullPath().equals(target)) {
106106
changed.set(true);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void pressureOnFSShouldNotMissNewFilesAnything(Approximation whichFiles) throws
153153
var io = new IOGenerator(THREADS, root, pool);
154154

155155
var seenCreates = ConcurrentHashMap.<Path>newKeySet();
156-
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
156+
var watchConfig = Watcher.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
157157
.withExecutor(pool)
158158
.onOverflow(whichFiles)
159159
.on(ev -> {
@@ -223,7 +223,7 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
223223
var r = new Thread(() -> {
224224
try {
225225
var watcher = Watcher
226-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
226+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
227227
.on(e -> seen.add(e.calculateFullPath()));
228228
startRegistering.acquire();
229229
try (var c = watcher.start()) {
@@ -295,7 +295,7 @@ void manyRegisterAndUnregisterSameTime(Approximation whichFiles) throws Interrup
295295
startRegistering.acquire();
296296
for (int k = 0; k < 1000; k++) {
297297
var watcher = Watcher
298-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
298+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
299299
.onOverflow(whichFiles)
300300
.on(e -> {
301301
if (e.calculateFullPath().equals(target)) {
@@ -359,7 +359,7 @@ void pressureOnFSShouldNotMissDeletes(Approximation whichFiles) throws Interrupt
359359

360360
final var events = new AtomicInteger(0);
361361
final var happened = new Semaphore(0);
362-
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
362+
var watchConfig = Watcher.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
363363
.withExecutor(pool)
364364
.onOverflow(whichFiles)
365365
.on(ev -> {

src/test/java/engineering/swat/watch/impl/overflows/IndexingRescannerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void onlyEventsForFilesInScopeAreIssued() throws IOException, InterruptedExcepti
7474
// Configure a non-recursive directory watch that monitors only the
7575
// children (not all descendants) of `path`
7676
var eventsOnlyForChildren = new AtomicBoolean(true);
77-
var watchConfig = Watcher.watch(path, WatchScope.PATH_AND_CHILDREN)
77+
var watchConfig = Watcher.build(path, WatchScope.PATH_AND_CHILDREN)
7878
.onOverflow(Approximation.NONE) // Disable the auto-handler here; we'll have an explicit one below
7979
.on(e -> {
8080
if (e.getRelativePath().getNameCount() > 1) {

0 commit comments

Comments
 (0)