Package gw.fs.watcher
Class DirectoryWatcher
- java.lang.Object
-
- gw.fs.watcher.DirectoryWatcher
-
public class DirectoryWatcher extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDirectoryWatcher.FileEvent
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.nio.file.WatchKey,java.nio.file.Path>_watchedDirectoriesprivate java.nio.file.WatchService_watchService
-
Constructor Summary
Constructors Constructor Description DirectoryWatcher()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Map<java.nio.file.Path,DirectoryWatcher.FileEvent>getChangesSinceLastTime()Checks all the watched directories (that are being kept in a map) for changes since the last call to this method.voidstopWatching()Close the watch service.private voidwatchDirectory(java.nio.file.Path dir)voidwatchDirectoryTree(java.nio.file.Path dir)Walk the directories under given path, and register a watcher for every directory.
-
-
-
Method Detail
-
stopWatching
public void stopWatching()
Close the watch service. Releases resources. After calling, this instance becomes invalid and can't be used any more.
-
watchDirectoryTree
public void watchDirectoryTree(java.nio.file.Path dir)
Walk the directories under given path, and register a watcher for every directory.- Parameters:
dir- the starting point under which to listen for changes, if it doesn't exist, do nothing.
-
watchDirectory
private void watchDirectory(java.nio.file.Path dir) throws java.io.IOException- Throws:
java.io.IOException
-
getChangesSinceLastTime
public java.util.Map<java.nio.file.Path,DirectoryWatcher.FileEvent> getChangesSinceLastTime()
Checks all the watched directories (that are being kept in a map) for changes since the last call to this method. If a file is already marked as CREATE, leave it as CREATE; if it's marked as DELETE, that's totally invalid, so just ignore that. If it's marked as MODIFY, no need to change it. So only put something in the map if it's not already in there. If a create follows a delete, it means the editor deleted and re-created the file, and who knows what the contents are now, so it should be treated as a modification. If the file was created, then deleted, then just ignore it entirely and remove the event.- Returns:
- FileEvent enum for each watched Path found, indicating the type of modification that happened.
-
-