Skip to content

Commit 340a39b

Browse files
committed
Extract internal API out of ActiveWatch into new impl.EventHandlingWatch
1 parent 2fbabb8 commit 340a39b

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,8 @@
3636
*/
3737
public interface ActiveWatch extends Closeable {
3838

39-
/**
40-
* Handles `event`. The purpose of this method is to trigger the event
41-
* handler of this watch "from the outside" (in addition to having native
42-
* file system libraries trigger the event handler "from the inside"). This
43-
* is useful to report synthetic events (e.g., while handling overflows).
44-
*/
45-
void handleEvent(WatchEvent event);
46-
4739
/**
4840
* Gets the path watched by this watch.
4941
*/
5042
Path getPath();
51-
52-
/**
53-
* Relativizes the full path of `event` against the path watched by this
54-
* watch (as per `getPath()`). Returns a new event whose root path and
55-
* relative path are set in accordance with the relativization.
56-
*/
57-
default WatchEvent relativize(WatchEvent event) {
58-
var fullPath = event.calculateFullPath();
59-
60-
var kind = event.getKind();
61-
var rootPath = getPath();
62-
var relativePath = rootPath.relativize(fullPath);
63-
return new WatchEvent(kind, rootPath, relativePath);
64-
}
6543
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* BSD 2-Clause License
3+
*
4+
* Copyright (c) 2023, Swat.engineering
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
package engineering.swat.watch.impl;
28+
29+
import engineering.swat.watch.ActiveWatch;
30+
import engineering.swat.watch.WatchEvent;
31+
32+
public interface EventHandlingWatch extends ActiveWatch {
33+
34+
/**
35+
* Handles `event`. The purpose of this method is to trigger the event
36+
* handler of this watch "from the outside" (in addition to having native
37+
* file system libraries trigger the event handler "from the inside"). This
38+
* is useful to report synthetic events (e.g., while handling overflows).
39+
*/
40+
void handleEvent(WatchEvent event);
41+
42+
/**
43+
* Relativizes the full path of `event` against the path watched by this
44+
* watch (as per `getPath()`). Returns a new event whose root path and
45+
* relative path are set in accordance with the relativization.
46+
*/
47+
default WatchEvent relativize(WatchEvent event) {
48+
var fullPath = event.calculateFullPath();
49+
50+
var kind = event.getKind();
51+
var rootPath = getPath();
52+
var relativePath = rootPath.relativize(fullPath);
53+
return new WatchEvent(kind, rootPath, relativePath);
54+
}
55+
}

src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
import org.apache.logging.log4j.Logger;
3838
import org.checkerframework.checker.nullness.qual.Nullable;
3939

40-
import engineering.swat.watch.ActiveWatch;
4140
import engineering.swat.watch.WatchEvent;
41+
import engineering.swat.watch.impl.EventHandlingWatch;
4242

43-
public abstract class JDKBaseWatch implements ActiveWatch {
43+
public abstract class JDKBaseWatch implements EventHandlingWatch {
4444
private final Logger logger = LogManager.getLogger();
4545

4646
protected final Path path;
@@ -116,7 +116,7 @@ private WatchEvent.Kind translate(java.nio.file.WatchEvent.Kind<?> jdkKind) {
116116
throw new IllegalArgumentException("Unexpected watch kind: " + jdkKind);
117117
}
118118

119-
// -- ActiveWatch --
119+
// -- EventHandlingWatch --
120120

121121
@Override
122122
public void handleEvent(WatchEvent e) {

0 commit comments

Comments
 (0)