Fix use of implementation-specific exception

This commit is contained in:
Lukas Rieger (Blue) 2024-05-22 16:00:54 +02:00
parent 3db6833fc6
commit 8455b50fc3
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
3 changed files with 45 additions and 10 deletions

View File

@ -32,7 +32,6 @@
import de.bluecolored.bluemap.core.util.WatchService;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
@ -67,7 +66,7 @@ public void run() {
try {
while (!closed)
this.watchService.take().forEach(this::updateRegion);
} catch (ClosedWatchServiceException ignore) {
} catch (WatchService.ClosedException ignore) {
} catch (InterruptedException iex) {
Thread.currentThread().interrupt();
} finally {

View File

@ -24,6 +24,7 @@
*/
package de.bluecolored.bluemap.core.util;
import lombok.experimental.StandardException;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -35,11 +36,33 @@
*/
public interface WatchService<T> extends AutoCloseable {
/**
* Retrieves and consumes the next batch of events.
* @throws ClosedException If the watch-service is closed
*/
@Nullable
List<T> poll();
/**
* Retrieves and consumes the next batch of events,
* waiting if necessary up to the specified wait time if none are yet present.
* @throws ClosedException If the watch-service is closed, or it is closed while waiting for the next event
* @throws InterruptedException If interrupted while waiting
*/
@Nullable List<T> poll(long timeout, TimeUnit unit) throws InterruptedException;
/**
* Retrieves and consumes the next batch of events,
* waiting if necessary until an event becomes available.
* @throws ClosedException If the watch-service is closed, or it is closed while waiting for the next event
* @throws InterruptedException If interrupted while waiting
*/
List<T> take() throws InterruptedException;
/**
* Thrown when the WatchService is closed or gets closed when polling or while waiting for events
*/
@StandardException
class ClosedException extends RuntimeException {}
}

View File

@ -30,6 +30,7 @@
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchKey;
@ -52,22 +53,34 @@ public MCAWorldRegionWatchService(Path regionFolder) throws IOException {
@Override
public @Nullable List<Vector2i> poll() {
WatchKey key = watchService.poll();
if (key == null) return null;
return processWatchKey(key);
try {
WatchKey key = watchService.poll();
if (key == null) return null;
return processWatchKey(key);
} catch (ClosedWatchServiceException e) {
throw new ClosedException(e);
}
}
@Override
public @Nullable List<Vector2i> poll(long timeout, TimeUnit unit) throws InterruptedException {
WatchKey key = watchService.poll(timeout, unit);
if (key == null) return null;
return processWatchKey(key);
try {
WatchKey key = watchService.poll(timeout, unit);
if (key == null) return null;
return processWatchKey(key);
} catch (ClosedWatchServiceException e) {
throw new ClosedException(e);
}
}
@Override
public List<Vector2i> take() throws InterruptedException {
WatchKey key = watchService.take();
return processWatchKey(key);
try {
WatchKey key = watchService.take();
return processWatchKey(key);
} catch (ClosedWatchServiceException e) {
throw new ClosedException(e);
}
}
@Override