@@ -249,60 +249,60 @@ public void addListener(final FileAlterationListener listener) {
249249 }
250250 }
251251
252- /**
253- * Checks whether the file and its children have been created, modified or deleted.
254- */
255- public void checkAndNotify () {
256-
257- // fire onStart()
258- listeners .forEach (listener -> listener .onStart (this ));
259-
260- // fire directory/file events
261- final File rootFile = rootEntry .getFile ();
262- if (rootFile .exists ()) {
263- checkAndNotify (rootEntry , rootEntry .getChildren (), listFiles (rootFile ));
264- } else if (rootEntry .isExists ()) {
265- checkAndNotify (rootEntry , rootEntry .getChildren (), FileUtils .EMPTY_FILE_ARRAY );
266- }
267- // Else: Didn't exist and still doesn't
268-
269- // fire onStop()
270- listeners .forEach (listener -> listener .onStop (this ));
271- }
272-
273252 /**
274253 * Compares two file lists for files which have been created, modified or deleted.
275254 *
276255 * @param parent The parent entry.
277256 * @param previous The original list of files.
278257 * @param files The current list of files.
279258 */
280- private void checkAndNotify (final FileEntry parent , final FileEntry [] previous , final File [] files ) {
259+ private void checkAndFire (final FileEntry parent , final FileEntry [] previous , final File [] files ) {
281260 int c = 0 ;
282261 final FileEntry [] current = files .length > 0 ? new FileEntry [files .length ] : FileEntry .EMPTY_FILE_ENTRY_ARRAY ;
283262 for (final FileEntry entry : previous ) {
284263 while (c < files .length && comparator .compare (entry .getFile (), files [c ]) > 0 ) {
285264 current [c ] = createFileEntry (parent , files [c ]);
286- doCreate (current [c ]);
265+ fireOnCreate (current [c ]);
287266 c ++;
288267 }
289268 if (c < files .length && comparator .compare (entry .getFile (), files [c ]) == 0 ) {
290- doMatch (entry , files [c ]);
291- checkAndNotify (entry , entry .getChildren (), listFiles (files [c ]));
269+ fireOnChange (entry , files [c ]);
270+ checkAndFire (entry , entry .getChildren (), listFiles (files [c ]));
292271 current [c ] = entry ;
293272 c ++;
294273 } else {
295- checkAndNotify (entry , entry .getChildren (), FileUtils .EMPTY_FILE_ARRAY );
296- doDelete (entry );
274+ checkAndFire (entry , entry .getChildren (), FileUtils .EMPTY_FILE_ARRAY );
275+ fireOnDelete (entry );
297276 }
298277 }
299278 for (; c < files .length ; c ++) {
300279 current [c ] = createFileEntry (parent , files [c ]);
301- doCreate (current [c ]);
280+ fireOnCreate (current [c ]);
302281 }
303282 parent .setChildren (current );
304283 }
305284
285+ /**
286+ * Checks whether the file and its children have been created, modified or deleted.
287+ */
288+ public void checkAndNotify () {
289+
290+ // fire onStart()
291+ listeners .forEach (listener -> listener .onStart (this ));
292+
293+ // fire directory/file events
294+ final File rootFile = rootEntry .getFile ();
295+ if (rootFile .exists ()) {
296+ checkAndFire (rootEntry , rootEntry .getChildren (), listFiles (rootFile ));
297+ } else if (rootEntry .isExists ()) {
298+ checkAndFire (rootEntry , rootEntry .getChildren (), FileUtils .EMPTY_FILE_ARRAY );
299+ }
300+ // Else: Didn't exist and still doesn't
301+
302+ // fire onStop()
303+ listeners .forEach (listener -> listener .onStop (this ));
304+ }
305+
306306 /**
307307 * Creates a new file entry for the specified file.
308308 *
@@ -327,28 +327,46 @@ public void destroy() throws Exception {
327327 // noop
328328 }
329329
330+ /**
331+ * Fires directory/file change events to the registered listeners.
332+ *
333+ * @param entry The previous file system entry.
334+ * @param file The current file.
335+ */
336+ private void fireOnChange (final FileEntry entry , final File file ) {
337+ if (entry .refresh (file )) {
338+ listeners .forEach (listener -> {
339+ if (entry .isDirectory ()) {
340+ listener .onDirectoryChange (file );
341+ } else {
342+ listener .onFileChange (file );
343+ }
344+ });
345+ }
346+ }
347+
330348 /**
331349 * Fires directory/file created events to the registered listeners.
332350 *
333351 * @param entry The file entry.
334352 */
335- private void doCreate (final FileEntry entry ) {
353+ private void fireOnCreate (final FileEntry entry ) {
336354 listeners .forEach (listener -> {
337355 if (entry .isDirectory ()) {
338356 listener .onDirectoryCreate (entry .getFile ());
339357 } else {
340358 listener .onFileCreate (entry .getFile ());
341359 }
342360 });
343- Stream .of (entry .getChildren ()).forEach (this ::doCreate );
361+ Stream .of (entry .getChildren ()).forEach (this ::fireOnCreate );
344362 }
345363
346364 /**
347365 * Fires directory/file delete events to the registered listeners.
348366 *
349367 * @param entry The file entry.
350368 */
351- private void doDelete (final FileEntry entry ) {
369+ private void fireOnDelete (final FileEntry entry ) {
352370 listeners .forEach (listener -> {
353371 if (entry .isDirectory ()) {
354372 listener .onDirectoryDelete (entry .getFile ());
@@ -358,24 +376,6 @@ private void doDelete(final FileEntry entry) {
358376 });
359377 }
360378
361- /**
362- * Fires directory/file change events to the registered listeners.
363- *
364- * @param entry The previous file system entry.
365- * @param file The current file.
366- */
367- private void doMatch (final FileEntry entry , final File file ) {
368- if (entry .refresh (file )) {
369- listeners .forEach (listener -> {
370- if (entry .isDirectory ()) {
371- listener .onDirectoryChange (file );
372- } else {
373- listener .onFileChange (file );
374- }
375- });
376- }
377- }
378-
379379 /**
380380 * Returns the directory being observed.
381381 *
0 commit comments