2929import java .util .HashMap ;
3030import java .util .Map ;
3131
32- import static java .nio .file .StandardWatchEventKinds .OVERFLOW ;
33-
34- /**
35- * Created by rroeser on 7/8/15.
36- */
37- public class WatcherEventObservable extends Observable <WatchEvent <?>> {
32+ public final class FileSystemWatcher {
3833
3934 static final boolean IS_MAC ;
4035
@@ -43,39 +38,32 @@ public class WatcherEventObservable extends Observable<WatchEvent<?>> {
4338 IS_MAC = os .contains ("mac" );
4439 }
4540
46- protected WatcherEventOnSubscribe watcherEventOnSubscribe ;
41+ private FileSystemWatcher () {}
4742
48- private static class WatcherEventOnSubscribe implements OnSubscribe <WatchEvent <?> > {
43+ private static class FileSystemEventOnSubscribe implements Observable . OnSubscribe <FileSystemEvent > {
4944 private WatchService watcher ;
5045
5146 private Scheduler scheduler ;
5247
5348 private volatile boolean close = false ;
5449
55- public void addPath (Path path , WatchEvent .Kind ... kinds ) throws Exception {
56- if (IS_MAC ) {
57- final WatchableFile watchableFile = new WatchableFile (path );
58- watchableFile .register (watcher , kinds );
59- } else {
60- path .register (watcher , kinds , SensitivityWatchEventModifier .HIGH );
61- }
62- }
63-
64- public WatcherEventOnSubscribe (Map <Path , WatchEvent .Kind []> paths , Scheduler scheduler ) {
50+ public FileSystemEventOnSubscribe (Map <Path , FileSystemEventKind []> paths , Scheduler scheduler ) {
6551 try {
6652 if (IS_MAC ) {
6753 this .watcher = MacOSXWatchServiceFactory .newWatchService ();
6854
6955 for (Path path : paths .keySet ()) {
7056 final WatchableFile watchableFile = new WatchableFile (path );
71- watchableFile .register (watcher , paths .get (path ));
57+ FileSystemEventKind [] kinds = paths .get (path );
58+ watchableFile .register (watcher , FileSystemEventKind .toWatchEventKinds (kinds ));
7259 }
7360 }
7461 else {
7562 this .watcher = FileSystems .getDefault ().newWatchService ();
7663
7764 for (Path path : paths .keySet ()) {
78- path .register (watcher , paths .get (path ), SensitivityWatchEventModifier .HIGH );
65+ FileSystemEventKind [] kinds = paths .get (path );
66+ path .register (watcher , FileSystemEventKind .toWatchEventKinds (kinds ), SensitivityWatchEventModifier .HIGH );
7967 }
8068 }
8169 this .scheduler = scheduler ;
@@ -85,8 +73,7 @@ public WatcherEventOnSubscribe(Map<Path, WatchEvent.Kind[]> paths, Scheduler sch
8573 }
8674
8775 @ Override
88- public void call (Subscriber <? super WatchEvent <?>> subscriber ) {
89-
76+ public void call (Subscriber <? super FileSystemEvent > subscriber ) {
9077 Scheduler .Worker worker = scheduler .createWorker ();
9178 subscriber .add (worker );
9279
@@ -99,14 +86,10 @@ public void call(Subscriber<? super WatchEvent<?>> subscriber) {
9986 }
10087
10188 for (WatchEvent <?> event : key .pollEvents ()) {
102- WatchEvent .Kind <?> kind = event .kind ();
103-
104- if (kind == OVERFLOW ) {
105- continue ;
106- } else {
107- subscriber .onNext (event );
108- }
89+ FileSystemEvent fileSystemEvent = new FileSystemEvent (event );
90+ subscriber .onNext (fileSystemEvent );
10991 }
92+
11093 if (!key .reset ()) {
11194 close ();
11295 }
@@ -127,64 +110,53 @@ public void close() {
127110 } catch (Exception e ) {}
128111 }
129112
130- public boolean isClosed () {
131- return close ;
132- }
133113 }
134114
135- protected WatcherEventObservable (WatcherEventOnSubscribe subscribe ) {
136- super (subscribe );
137-
138- this .watcherEventOnSubscribe = subscribe ;
139- }
115+ public static class Builder {
116+ private Map <Path , FileSystemEventKind []> paths = new HashMap <>();
140117
141- public static WatcherEventObservable create (Map <Path , WatchEvent .Kind []> paths , Scheduler scheduler ) {
142- try {
143- WatcherEventOnSubscribe watcherEventOnSubscribe
144- = new WatcherEventOnSubscribe (paths , scheduler );
118+ private Scheduler scheduler = Schedulers .newThread ();
145119
146- WatcherEventObservable watcherEventObservable
147- = new WatcherEventObservable (watcherEventOnSubscribe );
120+ Builder () {}
148121
149- watcherEventObservable
150- .doOnSubscribe (() -> {
151- })
152- .doOnUnsubscribe (watcherEventOnSubscribe ::close );
122+ public Builder addPath (Path path , FileSystemEventKind ... kinds ) {
123+ paths .put (path , kinds );
153124
154- return watcherEventObservable ;
155- } catch (Exception e ) {
156- throw new RuntimeException (e );
125+ return this ;
157126 }
158- }
159-
160- public static WatcherEventObservable create (Map <Path , WatchEvent .Kind []> paths ) {
161- return create (paths , Schedulers .newThread ());
162- }
163127
164- public static WatcherEventObservable create ( Path path , WatchEvent . Kind ... kinds ) {
165- Map < Path , WatchEvent . Kind []> paths = new HashMap <>( );
128+ public Builder addPaths ( Map < Path , FileSystemEventKind []> paths ) {
129+ this . paths . putAll ( paths );
166130
167- paths .put (path , kinds );
131+ return this ;
132+ }
168133
169- return create ( paths );
170- }
134+ public Builder withScheduler ( Scheduler scheduler ) {
135+ this . scheduler = scheduler ;
171136
172- public WatcherEventObservable addPath (Path path , WatchEvent .Kind ... kinds ) {
173- try {
174- watcherEventOnSubscribe .addPath (path , kinds );
175- } catch (Exception e ) {
176- throw new RuntimeException (e );
137+ return this ;
177138 }
178139
179- return this ;
180- }
140+ public Observable <FileSystemEvent > build () {
141+ try {
142+ FileSystemEventOnSubscribe fileSystemEventOnSubscribe
143+ = new FileSystemEventOnSubscribe (paths , scheduler );
144+
145+ Observable <FileSystemEvent > fileSystemEventObservable
146+ = Observable .create (fileSystemEventOnSubscribe );
147+
148+ fileSystemEventObservable
149+ .doOnUnsubscribe (fileSystemEventOnSubscribe ::close );
181150
151+ return fileSystemEventObservable ;
152+ } catch (Exception e ) {
153+ throw new RuntimeException (e );
154+ }
155+ }
182156
183- public void close () {
184- watcherEventOnSubscribe .close ();
185157 }
186158
187- public boolean isClosed () {
188- return watcherEventOnSubscribe . isClosed ();
159+ public static Builder newBuilder () {
160+ return new Builder ();
189161 }
190162}
0 commit comments