This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 914060e  Better internal name and Javadoc.
914060e is described below

commit 914060ec41c375144054b993813a7f8aff1b35d6
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Tue Sep 7 11:33:32 2021 -0400

    Better internal name and Javadoc.
    
    Simple impl for PathUtils#waitFor().
---
 .../java/org/apache/commons/io/file/PathUtils.java  | 21 +++++++++++----------
 .../commons/io/monitor/FileAlterationMonitor.java   | 16 ++++++++--------
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 29827b5..44f04e7 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -1072,37 +1072,38 @@ public final class PathUtils {
     /**
      * 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();
             }
         }
diff --git 
a/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java 
b/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
index 713e663..126f940 100644
--- a/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
+++ b/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
@@ -34,7 +34,7 @@ public final class FileAlterationMonitor implements Runnable {
 
     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;
@@ -44,17 +44,17 @@ public final class FileAlterationMonitor implements 
Runnable {
      * 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 final class FileAlterationMonitor implements Runnable {
      * @return the interval
      */
     public long getInterval() {
-        return interval;
+        return intervalMillis;
     }
 
     /**
@@ -171,7 +171,7 @@ public final class FileAlterationMonitor implements 
Runnable {
      * @throws Exception if an error occurs initializing the observer
      */
     public synchronized void stop() throws Exception {
-        stop(interval);
+        stop(intervalMillis);
     }
 
     /**
@@ -211,7 +211,7 @@ public final class FileAlterationMonitor implements 
Runnable {
                 break;
             }
             try {
-                Thread.sleep(interval);
+                Thread.sleep(intervalMillis);
             } catch (final InterruptedException ignored) {
                 // ignore
             }

Reply via email to