Search in sources :

Example 1 with FileWatcherEvent

use of org.gradle.internal.filewatch.FileWatcherEvent in project gradle by gradle.

the class WatchServicePoller method handleWatchKey.

private List<FileWatcherEvent> handleWatchKey(WatchKey watchKey) {
    final Path watchedPath = (Path) watchKey.watchable();
    Transformer<FileWatcherEvent, WatchEvent<?>> watchEventTransformer = new Transformer<FileWatcherEvent, WatchEvent<?>>() {

        @Override
        public FileWatcherEvent transform(WatchEvent<?> event) {
            WatchEvent.Kind kind = event.kind();
            File file = null;
            if (kind.type() == Path.class) {
                WatchEvent<Path> ev = Cast.uncheckedCast(event);
                file = watchedPath.resolve(ev.context()).toFile();
            }
            return toEvent(kind, file);
        }
    };
    List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
    watchKey.reset();
    if (watchEvents.isEmpty()) {
        return Collections.singletonList(FileWatcherEvent.delete(watchedPath.toFile()));
    } else {
        return CollectionUtils.collect(watchEvents, watchEventTransformer);
    }
}
Also used : Path(java.nio.file.Path) Transformer(org.gradle.api.Transformer) WatchEvent(java.nio.file.WatchEvent) File(java.io.File) FileWatcherEvent(org.gradle.internal.filewatch.FileWatcherEvent)

Aggregations

File (java.io.File)1 Path (java.nio.file.Path)1 WatchEvent (java.nio.file.WatchEvent)1 Transformer (org.gradle.api.Transformer)1 FileWatcherEvent (org.gradle.internal.filewatch.FileWatcherEvent)1