Skip to content

Commit

Permalink
Fix use of implementation-specific exception
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed May 22, 2024
1 parent 3db6833 commit 8455b50
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package de.bluecolored.bluemap.core.util;

import lombok.experimental.StandardException;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -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 {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 8455b50

Please sign in to comment.