@@ -53,7 +53,7 @@ public JDKFileTreeWatch(Path root, Executor exec,
5353 BiConsumer <EventHandlingWatch , WatchEvent > eventHandler ) {
5454
5555 super (root , exec , eventHandler );
56- var internalEventHandler = new ChildWatchesUpdater (). andThen ( eventHandler );
56+ var internalEventHandler = eventHandler . andThen ( new ChildWatchesUpdater ());
5757 this .internal = new JDKDirectoryWatch (root , exec , internalEventHandler );
5858 }
5959
@@ -67,31 +67,38 @@ public JDKFileTreeWatch(Path root, Executor exec,
6767 private class ChildWatchesUpdater implements BiConsumer <EventHandlingWatch , WatchEvent > {
6868 @ Override
6969 public void accept (EventHandlingWatch watch , WatchEvent event ) {
70- var kind = event .getKind ();
71-
72- if (kind == WatchEvent .Kind .OVERFLOW ) {
73- forEachChild (JDKFileTreeWatch .this ::reportOverflowToChildWatch );
74- return ;
70+ switch (event .getKind ()) {
71+ case OVERFLOW :
72+ reportOverflowToChildWatches ();
73+ break ;
74+ case CREATED :
75+ var childWatch = openChildWatch (event .calculateFullPath ());
76+ // Events in the newly created directory might have been
77+ // missed between its creation and setting up its watch. So,
78+ // generate an overflow event for the watch.
79+ reportOverflowTo (childWatch );
80+ break ;
81+ case DELETED :
82+ closeChildWatch (event .calculateFullPath ());
83+ break ;
84+ case MODIFIED :
85+ break ;
7586 }
87+ }
7688
77- var child = event .calculateFullPath ();
78- var directory = child .toFile ().isDirectory ();
79-
80- if (kind == WatchEvent .Kind .CREATED && directory ) {
81- openChildWatch (child );
82- // Events in the newly created directory (`child`) might have
83- // been missed between its creation (`event`) and setting up its
84- // watch. Erring on the side of caution, generate an overflow
85- // event for the watch.
86- reportOverflowToChildWatch (child );
87- }
88- if (kind == WatchEvent .Kind .DELETED && directory ) {
89- closeChildWatch (child );
89+ private void reportOverflowToChildWatches () {
90+ for (var childWatch : childWatches .values ()) {
91+ reportOverflowTo (childWatch );
9092 }
9193 }
94+
95+ private void reportOverflowTo (EventHandlingWatch childWatch ) {
96+ var overflow = new WatchEvent (WatchEvent .Kind .OVERFLOW , childWatch .getPath ());
97+ childWatch .handleEvent (overflow );
98+ }
9299 }
93100
94- private void openChildWatch (Path child ) {
101+ private JDKFileTreeWatch openChildWatch (Path child ) {
95102 Function <Path , JDKFileTreeWatch > newChildWatch = p -> new JDKFileTreeWatch (child , exec , (w , e ) ->
96103 // Same as `eventHandler`, except each event is pre-processed such
97104 // that the last segment of the root path becomes the first segment
@@ -104,13 +111,12 @@ private void openChildWatch(Path child) {
104111 );
105112
106113 var childWatch = childWatches .computeIfAbsent (child , newChildWatch );
107- if (childWatch != null ) {
108- try {
109- childWatch .open ();
110- } catch (IOException e ) {
111- logger .error ("Could not open (nested) file tree watch for: {} ({})" , child , e );
112- }
114+ try {
115+ childWatch .open ();
116+ } catch (IOException e ) {
117+ logger .error ("Could not open (nested) file tree watch for: {} ({})" , child , e );
113118 }
119+ return childWatch ;
114120 }
115121
116122 private void closeChildWatch (Path child ) {
@@ -124,14 +130,6 @@ private void closeChildWatch(Path child) {
124130 }
125131 }
126132
127- private void reportOverflowToChildWatch (Path child ) {
128- var childWatch = childWatches .get (child );
129- if (childWatch != null ) {
130- var overflow = new WatchEvent (WatchEvent .Kind .OVERFLOW , child );
131- childWatch .handleEvent (overflow );
132- }
133- }
134-
135133 private void forEachChild (Consumer <Path > action ) {
136134 try (var children = Files .find (path , 1 , (p , attrs ) -> p != path && attrs .isDirectory ())) {
137135 children .forEach (action );
0 commit comments