Add constructor accepting collection of file alteration observers
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
*/
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 FileAlterationMonitor() {
* 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) {

MODIFY
src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
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 void testCollectionConstructorShouldDoNothingWithNullObservers() {
@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());

Add constructor accepting collection of file alteration observers #236.