Skip to content

Commit cb07075

Browse files
committed
Clarify logic for when to ignore events
1 parent 10692a7 commit cb07075

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/main/java/engineering/swat/watch/impl/mac/MacWatchKey.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,40 @@ public T context() {
141141
*/
142142
private static class Configuration {
143143
private final Kind<?>[] kinds;
144-
private final boolean singleDirectory;
144+
private final boolean watchFileTree;
145145

146146
public Configuration() {
147147
this(new Kind<?>[0], new Modifier[0]);
148148
}
149149

150150
public Configuration(Kind<?>[] kinds, Modifier[] modifiers) {
151151
// Extract only the relevant information from `modifiers`
152-
var fileTree = false;
152+
var watchFileTree = false;
153153
for (var m : modifiers) {
154-
fileTree |= m == ExtendedWatchEventModifier.FILE_TREE;
154+
watchFileTree |= m == ExtendedWatchEventModifier.FILE_TREE;
155155
}
156156

157157
this.kinds = Arrays.copyOf(kinds, kinds.length);
158-
this.singleDirectory = !fileTree;
158+
this.watchFileTree = watchFileTree;
159159
}
160160

161161
/**
162162
* Tests if an event should be ignored by a watch key with this
163163
* configuration. This is the case when one of the following is true:
164164
* (a) the watch key isn't configured to watch events of the given
165-
* {@code kind}; (b) the watch key is configured to watch only the root
166-
* level of a file tree, but the given {@code context} (a {@code Path})
167-
* points to a non-root level.
165+
* {@code kind}; (b) the watch key is configured to watch only a single
166+
* directory, but the given {@code context} (a {@code Path}) points to a
167+
* file in a subdirectory.
168168
*/
169169
public boolean ignore(Kind<?> kind, @Nullable Object context) {
170170
for (var k : kinds) {
171171
if (k == kind) {
172-
return singleDirectory &&
173-
context instanceof Path && ((Path) context).getNameCount() > 1;
172+
if (watchFileTree) {
173+
return false;
174+
} else { // Watch a single directory
175+
return context instanceof Path &&
176+
((Path) context).getNameCount() > 1; // File in subdirectory
177+
}
174178
}
175179
}
176180
return true;

0 commit comments

Comments
 (0)