
br.com.objectos.io.WatchServiceJava6 Maven / Gradle / Ivy
/*
* Copyright (C) 2011-2021 Objectos Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.objectos.io;
import static br.com.objectos.collections.Collections.newGrowableMap;
import static br.com.objectos.collections.Collections.toImmutableSet;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.collections.ImmutableSet;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;
final class WatchServiceJava6 extends Thread implements WatchService {
private final ImmutableSet directories;
private final int timeout = 1 * 1000;
WatchServiceJava6(ImmutableSet directories) {
super("WatchService");
this.directories = directories;
}
@Override
public final void onShutdownHook() {
stopService();
}
@Override
public final void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(timeout);
for (WatchDirectoryJava6 dir : directories) {
dir.checkForEvents();
}
} catch (InterruptedException e) {
return;
}
}
}
@Override
public final void startService() {
start();
}
@Override
public final void stopService() {
interrupt();
}
static final class Builder implements WatchServiceBuilder {
private final Map directories = newGrowableMap();
public final WatchService build() {
Collection values;
values = directories.values();
ImmutableSet set;
set = toImmutableSet(values);
return new WatchServiceJava6(set);
}
@Override
public final void watchDirectory(
Directory directory, WatchServiceListener listener, EventKind event) {
checkNotNull(directory, "directory == null");
if (directories.containsKey(directory)) {
throw new IllegalArgumentException(directory.getPath() + " has already been registered");
}
checkNotNull(listener, "listener == null");
checkNotNull(event, "event == null");
EnumSet events;
events = EnumSet.of(event);
WatchDirectoryJava6 option;
option = new WatchDirectoryJava6(directory, listener, events);
option.register();
directories.put(directory, option);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy