Skip to content

Commit 0acdfcb

Browse files
authored
Merge pull request #37 from SWAT-engineering/feat/better-top-level-name
Refactored top level name of the watcher function
2 parents d57aa42 + bc66975 commit 0acdfcb

File tree

10 files changed

+43
-43
lines changed

10 files changed

+43
-43
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 = Watch.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 renamed to src/main/java/engineering/swat/watch/Watch.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* <p>It will avoid common errors using the raw apis, and will try to use the most native api where possible.</p>
5454
* Note, there are differences per platform that cannot be avoided, please review the readme of the library.
5555
*/
56-
public class Watcher {
56+
public class Watch {
5757
private final Logger logger = LogManager.getLogger();
5858
private final Path path;
5959
private final WatchScope scope;
@@ -65,7 +65,7 @@ public class Watcher {
6565
private static final Predicate<WatchEvent> TRUE_FILTER = e -> true;
6666
private volatile Predicate<WatchEvent> eventFilter = TRUE_FILTER;
6767

68-
private Watcher(Path path, WatchScope scope) {
68+
private Watch(Path path, WatchScope scope) {
6969
this.path = path;
7070
this.scope = scope;
7171
}
@@ -76,7 +76,7 @@ private Watcher(Path path, WatchScope scope) {
7676
* @param scope for directories you can also choose to monitor it's direct children or all it's descendants
7777
* @throws IllegalArgumentException in case a path is not supported (in relation to the scope)
7878
*/
79-
public static Watcher watch(Path path, WatchScope scope) {
79+
public static Watch build(Path path, WatchScope scope) {
8080
if (!path.isAbsolute()) {
8181
throw new IllegalArgumentException("We can only watch absolute paths");
8282
}
@@ -95,7 +95,7 @@ public static Watcher watch(Path path, WatchScope scope) {
9595
default:
9696
throw new IllegalArgumentException("Unsupported scope: " + scope);
9797
}
98-
return new Watcher(path, scope);
98+
return new Watch(path, scope);
9999
}
100100

101101
/**
@@ -105,7 +105,7 @@ public static Watcher watch(Path path, WatchScope scope) {
105105
* @param eventHandler a callback that handles the watch event, will be called once per event.
106106
* @return this for optional method chaining
107107
*/
108-
public Watcher on(Consumer<WatchEvent> eventHandler) {
108+
public Watch on(Consumer<WatchEvent> eventHandler) {
109109
if (this.eventHandler != EMPTY_HANDLER) {
110110
throw new IllegalArgumentException("on handler cannot be set more than once");
111111
}
@@ -116,7 +116,7 @@ public Watcher on(Consumer<WatchEvent> eventHandler) {
116116
/**
117117
* Convenience variant of {@link #on(Consumer)}, which allows you to only respond to certain events
118118
*/
119-
public Watcher on(WatchEventListener listener) {
119+
public Watch on(WatchEventListener listener) {
120120
if (this.eventHandler != EMPTY_HANDLER) {
121121
throw new IllegalArgumentException("on handler cannot be set more than once");
122122
}
@@ -149,7 +149,7 @@ public Watcher on(WatchEventListener listener) {
149149
* ({@code true}) or dropped ({@code false})
150150
* @return {@code this} (to support method chaining)
151151
*/
152-
Watcher filter(Predicate<WatchEvent> predicate) {
152+
Watch filter(Predicate<WatchEvent> predicate) {
153153
if (this.eventFilter != TRUE_FILTER) {
154154
throw new IllegalArgumentException("filter cannot be set more than once");
155155
}
@@ -163,7 +163,7 @@ Watcher filter(Predicate<WatchEvent> predicate) {
163163
* @param callbackHandler worker pool to use
164164
* @return this for optional method chaining
165165
*/
166-
public Watcher withExecutor(Executor callbackHandler) {
166+
public Watch withExecutor(Executor callbackHandler) {
167167
this.executor = callbackHandler;
168168
return this;
169169
}
@@ -179,7 +179,7 @@ public Watcher withExecutor(Executor callbackHandler) {
179179
* files/directories to approximate
180180
* @return This watcher for optional method chaining
181181
*/
182-
public Watcher onOverflow(Approximation whichFiles) {
182+
public Watch onOverflow(Approximation whichFiles) {
183183
this.approximateOnOverflow = whichFiles;
184184
return this;
185185
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ static void setupEverything() {
6161
@Test
6262
void noDuplicateEvents() {
6363
assertThrowsExactly(IllegalArgumentException.class, () ->
64-
Watcher
65-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
64+
Watch
65+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
6666
.on(System.out::println)
6767
.on(System.err::println)
6868
);
@@ -71,16 +71,16 @@ void noDuplicateEvents() {
7171
@Test
7272
void onlyDirectoryWatchingOnDirectories() {
7373
assertThrowsExactly(IllegalArgumentException.class, () ->
74-
Watcher
75-
.watch(testDir.getTestFiles().get(0), WatchScope.PATH_AND_CHILDREN)
74+
Watch
75+
.build(testDir.getTestFiles().get(0), WatchScope.PATH_AND_CHILDREN)
7676
);
7777
}
7878

7979
@Test
8080
void doNotStartWithoutEventHandler() {
8181
assertThrowsExactly(IllegalStateException.class, () ->
82-
Watcher
83-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
82+
Watch
83+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
8484
.start()
8585
);
8686
}
@@ -90,8 +90,8 @@ void noRelativePaths() {
9090
var relativePath = testDir.getTestDirectory().resolve("d1").relativize(testDir.getTestDirectory());
9191

9292
assertThrowsExactly(IllegalArgumentException.class, () ->
93-
Watcher
94-
.watch(relativePath, WatchScope.PATH_AND_CHILDREN)
93+
Watch
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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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 = Watch.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: 4 additions & 4 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 = Watch.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 = Watch.build(target, WatchScope.PATH_ONLY)
104104
.on(ev -> {
105105
if (ev.calculateFullPath().equals(target)) {
106106
seen.set(true);
@@ -161,8 +161,8 @@ private ActiveWatch startWatchAndTriggerOverflow(Approximation whichFiles, TestH
161161
var parent = testDir.getTestDirectory();
162162
var file = parent.resolve("a.txt");
163163

164-
var watch = Watcher
165-
.watch(file, WatchScope.PATH_ONLY)
164+
var watch = Watch
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 = Watch.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 = Watch.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 = Watch.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: 6 additions & 6 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 = Watch.build(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
157157
.withExecutor(pool)
158158
.onOverflow(whichFiles)
159159
.on(ev -> {
@@ -222,8 +222,8 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
222222
for (int t = 0; t < TORTURE_REGISTRATION_THREADS; t++) {
223223
var r = new Thread(() -> {
224224
try {
225-
var watcher = Watcher
226-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
225+
var watcher = Watch
226+
.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
227227
.on(e -> seen.add(e.calculateFullPath()));
228228
startRegistering.acquire();
229229
try (var c = watcher.start()) {
@@ -294,8 +294,8 @@ void manyRegisterAndUnregisterSameTime(Approximation whichFiles) throws Interrup
294294
var id = Thread.currentThread().getId();
295295
startRegistering.acquire();
296296
for (int k = 0; k < 1000; k++) {
297-
var watcher = Watcher
298-
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
297+
var watcher = Watch
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 = Watch.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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import engineering.swat.watch.TestHelper;
4444
import engineering.swat.watch.WatchEvent;
4545
import engineering.swat.watch.WatchScope;
46-
import engineering.swat.watch.Watcher;
46+
import engineering.swat.watch.Watch;
4747
import engineering.swat.watch.impl.EventHandlingWatch;
4848

4949
class IndexingRescannerTests {
@@ -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 = Watch.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)