Better internal name and Javadoc.
Simple impl for PathUtils#waitFor().
MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -1072,37 +1072,38 @@ public static Path setReadOnly(final Path path, final boolean readOnly, final Li
/**
* Waits for the file system to propagate a file creation, with a timeout.
* <p>
- * This method repeatedly tests {@link File#exists()} until it returns true up to the maximum time specified in seconds.
+ * This method repeatedly tests {@link Files#exists(Path,LinkOption...)} until it returns true up to the maximum time
+ * specified.
* </p>
*
* @param file the file to check, must not be {@code null}.
- * @param duration the maximum time in seconds to wait.
+ * @param timeout the maximum time to wait.
* @param options options indicating how symbolic links are handled.
* @return true if file exists.
* @throws NullPointerException if the file is {@code null}.
* @since 2.12.0
*/
- public static boolean waitFor(final Path file, final Duration duration, LinkOption... options) {
+ public static boolean waitFor(final Path file, final Duration timeout, LinkOption... options) {
Objects.requireNonNull(file, "file");
- final Instant finishInstant = Instant.now().plus(duration);
- boolean wasInterrupted = false;
+ final Instant finishInstant = Instant.now().plus(timeout);
+ boolean interrupted = false;
final long minSleepMillis = 100;
try {
while (!Files.exists(file, options)) {
- final long remainingMillis = finishInstant.minusMillis(System.currentTimeMillis()).toEpochMilli();
- if (remainingMillis < 0) {
+ final Instant now = Instant.now();
+ if (now.isAfter(finishInstant)) {
return false;
}
try {
- Thread.sleep(Math.min(minSleepMillis, remainingMillis));
+ Thread.sleep(Math.min(minSleepMillis, finishInstant.minusMillis(now.toEpochMilli()).toEpochMilli()));
} catch (final InterruptedException ignore) {
- wasInterrupted = true;
+ interrupted = true;
} catch (final Exception ex) {
break;
}
}
} finally {
- if (wasInterrupted) {
+ if (interrupted) {
Thread.currentThread().interrupt();
}
}

MODIFY
src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
private static final FileAlterationObserver[] EMPTY_ARRAY = {};
- private final long interval;
+ private final long intervalMillis;
private final List<FileAlterationObserver> observers = new CopyOnWriteArrayList<>();
private Thread thread;
private ThreadFactory threadFactory;
* Constructs a monitor with a default interval of 10 seconds.
*/
public FileAlterationMonitor() {
- this(10000);
+ this(10_000);
}
/**
* Constructs a monitor with the specified interval.
*
- * @param interval The amount of time in milliseconds to wait between
+ * @param intervalMillis The amount of time in milliseconds to wait between
* checks of the file system.
*/
- public FileAlterationMonitor(final long interval) {
- this.interval = interval;
+ public FileAlterationMonitor(final long intervalMillis) {
+ this.intervalMillis = intervalMillis;
}
/**
@@ -98,7 +98,7 @@ public FileAlterationMonitor(final long interval, final FileAlterationObserver..
* @return the interval
*/
public long getInterval() {
- return interval;
+ return intervalMillis;
}
/**
@@ -171,7 +171,7 @@ public synchronized void start() throws Exception {
* @throws Exception if an error occurs initializing the observer
*/
public synchronized void stop() throws Exception {
- stop(interval);
+ stop(intervalMillis);
}
/**
@@ -211,7 +211,7 @@ public void run() {
break;
}
try {
- Thread.sleep(interval);
+ Thread.sleep(intervalMillis);
} catch (final InterruptedException ignored) {
// ignore
}

Better internal name and Javadoc.

Simple impl for PathUtils#waitFor().