You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/05/22 12:37:07 UTC

[commons-io] branch master updated: Add constructor accepting collection of file alteration observers #236.

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 a740a04  Add constructor accepting collection of file alteration observers #236.
a740a04 is described below

commit a740a0425e79e5ca902d5d41d38de5fb38a2afd5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat May 22 08:37:03 2021 -0400

    Add constructor accepting collection of file alteration observers #236.
---
 src/changes/changes.xml                              |  3 +++
 .../commons/io/monitor/FileAlterationMonitor.java    | 20 +++++++++++++-------
 .../io/monitor/FileAlterationMonitorTestCase.java    |  6 ++----
 3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f5de5b5..8d784e2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -216,6 +216,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add constructor ThresholdingOutputStream(int, IOConsumer, IOFunction) and make the class concrete.
       </action>
+      <action dev="ggregory" type="add" due-to="nstdspace, Gary Gregory">
+        Add constructor accepting collection of file alteration observers #236.
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
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 534ea8d..8da0f63 100644
--- a/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
+++ b/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
@@ -32,6 +32,8 @@ import java.util.concurrent.ThreadFactory;
  */
 public final class FileAlterationMonitor implements Runnable {
 
+    private static final FileAlterationObserver[] EMPTY_ARRAY = new FileAlterationObserver[0];
+
     private final long interval;
     private final List<FileAlterationObserver> observers = new CopyOnWriteArrayList<>();
     private Thread thread;
@@ -49,32 +51,36 @@ public final class FileAlterationMonitor implements Runnable {
      * Constructs a monitor with the specified interval.
      *
      * @param interval The amount of time in milliseconds to wait between
-     * checks of the file system
+     * checks of the file system.
      */
     public FileAlterationMonitor(final long interval) {
         this.interval = interval;
     }
 
     /**
-     * Wrapper constructor for {@link #FileAlterationMonitor(long, FileAlterationObserver...)}.
      * Constructs a monitor with the specified interval and collection of observers.
      *
      * @param interval The amount of time in milliseconds to wait between
-     * checks of the file system
-     * @param observers The collection of observers to add to the monitor
+     * checks of the file system.
+     * @param observers The collection of observers to add to the monitor.
+     * @since 2.9.0
      */
     public FileAlterationMonitor(final long interval, final Collection<FileAlterationObserver> observers) {
-        this(interval, Optional.ofNullable(observers)
+        // @formatter:off
+        this(interval,
+            Optional
+                .ofNullable(observers)
                 .orElse(Collections.emptyList())
-                .toArray(new FileAlterationObserver[0])
+                .toArray(EMPTY_ARRAY)
         );
+        // @formatter:on
     }
 
     /**
      * Constructs a monitor with the specified interval and set of observers.
      *
      * @param interval The amount of time in milliseconds to wait between
-     * checks of the file system
+     * checks of the file system.
      * @param observers The set of observers to add to the monitor.
      */
     public FileAlterationMonitor(final long interval, final FileAlterationObserver... observers) {
diff --git a/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java b/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
index 5167352..6f1e4a3 100644
--- a/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
+++ b/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.concurrent.Executors;
@@ -69,10 +70,7 @@ public class FileAlterationMonitorTestCase extends AbstractMonitorTestCase {
 
     @Test
     public void testCollectionConstructor() {
-        Collection<FileAlterationObserver> observers = new ArrayList<>();
-        FileAlterationObserver observer = new FileAlterationObserver("foo");
-        observers.add(observer);
-
+        Collection<FileAlterationObserver> observers = Arrays.asList(new FileAlterationObserver("foo"));
         FileAlterationMonitor monitor = new FileAlterationMonitor(0, observers);
         Iterator<FileAlterationObserver> iterator = monitor.getObservers().iterator();
         assertEquals(observer, iterator.next());