Package com.illumon.util.files
Class DirWatchService
java.lang.Object
com.illumon.util.files.DirWatchService
Utilities to assist with file and directory operations.
See MatcherType for details on the matcher types supported.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumThe preferred watcher type is the built-in Java one as it's more efficient, but it doesn't catch all new files -
Constructor Summary
ConstructorsConstructorDescriptionDirWatchService(String dirToWatch, Consumer<DirWatchService.ExceptionConsumerParameter> exceptionConsumer, DirWatchService.WatchServiceType watchServiceType, long pollIntervalMillis, WatchEvent.Kind... kinds) Constructor to create a directory watch service. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddExactFileWatch(String separator, String filePattern, BiConsumer<Path, WatchEvent.Kind> consumer) Adds an exact match file pattern to watch for; equivalent to adding with the EXACT_MATCH_WITH_SEPARATOR MatcherTypevoidaddFileWatchAtEnd(Predicate<String> matcher, BiConsumer<Path, WatchEvent.Kind> consumer) Adds a file pattern to watch for at the end of the ordered watch listvoidaddFileWatchAtStart(Predicate<String> matcher, BiConsumer<Path, WatchEvent.Kind> consumer) Adds a file pattern to watch for at the start of the ordered watch listmakeEndsWithMatcher(String suffix) makeRegexMatcher(String regex) makeStartsWithMatcher(String prefix) voidstart()Starts the watch service thread.voidstop()Request that the fileWatcher thread stop.
-
Constructor Details
-
DirWatchService
public DirWatchService(@NotNull String dirToWatch, @NotNull Consumer<DirWatchService.ExceptionConsumerParameter> exceptionConsumer, @NotNull DirWatchService.WatchServiceType watchServiceType, long pollIntervalMillis, @NotNull WatchEvent.Kind... kinds) Constructor to create a directory watch service. This initializes the instance but doesn't add any watch patterns, and doesn't start the watch thread.- Parameters:
dirToWatch- Directory to watch for changesexceptionConsumer- Consumer to accept exceptions if they occur. Even if the watch service has terminated, stop() should be called before restarting it. The exceptionConsumer must accept two arguments - the Exception generated, and a boolean which specifies whether the WatchService has terminated as a result of the exception.watchServiceType- the watch service type, from the WatchServiceType enumpollIntervalMillis- for a poll service, the interval between pollskinds- The kinds of events that may need to be watched from java.nio.file.StandardWatchEventKinds, valid options are: ENTRY_CREATE, ENTRY_DELETE, and ENTRY_MODIFY
-
-
Method Details
-
addExactFileWatch
public void addExactFileWatch(@NotNull String separator, @NotNull String filePattern, @NotNull BiConsumer<Path, WatchEvent.Kind> consumer) Adds an exact match file pattern to watch for; equivalent to adding with the EXACT_MATCH_WITH_SEPARATOR MatcherType- Parameters:
filePattern- The exact file pattern to watch for (i.e. the part before the separator must match this)consumer- The consumer to be called when the pattern is matched
-
addFileWatchAtEnd
public void addFileWatchAtEnd(@NotNull Predicate<String> matcher, @NotNull BiConsumer<Path, WatchEvent.Kind> consumer) Adds a file pattern to watch for at the end of the ordered watch list- Parameters:
matcher- The filtering predicate, returns true for files that should be consumedconsumer- Consumer to be called with the file and event type
-
addFileWatchAtStart
public void addFileWatchAtStart(@NotNull Predicate<String> matcher, @NotNull BiConsumer<Path, WatchEvent.Kind> consumer) Adds a file pattern to watch for at the start of the ordered watch list- Parameters:
matcher- The filtering predicate, returns true for files that should be consumedconsumer- Consumer to be called with the file and event type
-
stop
Request that the fileWatcher thread stop. This will not return until the thread has stopped.- Throws:
Exception
-
start
Starts the watch service thread. Even if it's initially empty the service should start as it could get files later.- Throws:
IOException- from the Java watch serviceException
-
makeRegexMatcher
-
makeStartsWithMatcher
-
makeEndsWithMatcher
-