Skip to content

Commit a740a04

Browse files
author
Gary Gregory
committed
Add constructor accepting collection of file alteration observers #236.
1 parent 3a67213 commit a740a04

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ The <action> type attribute can be add,update,fix,remove.
216216
<action dev="ggregory" type="add" due-to="Gary Gregory">
217217
Add constructor ThresholdingOutputStream(int, IOConsumer, IOFunction) and make the class concrete.
218218
</action>
219+
<action dev="ggregory" type="add" due-to="nstdspace, Gary Gregory">
220+
Add constructor accepting collection of file alteration observers #236.
221+
</action>
219222
<!-- UPDATES -->
220223
<action dev="ggregory" type="update" due-to="Dependabot">
221224
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
public final class FileAlterationMonitor implements Runnable {
3434

35+
private static final FileAlterationObserver[] EMPTY_ARRAY = new FileAlterationObserver[0];
36+
3537
private final long interval;
3638
private final List<FileAlterationObserver> observers = new CopyOnWriteArrayList<>();
3739
private Thread thread;
@@ -49,32 +51,36 @@ public FileAlterationMonitor() {
4951
* Constructs a monitor with the specified interval.
5052
*
5153
* @param interval The amount of time in milliseconds to wait between
52-
* checks of the file system
54+
* checks of the file system.
5355
*/
5456
public FileAlterationMonitor(final long interval) {
5557
this.interval = interval;
5658
}
5759

5860
/**
59-
* Wrapper constructor for {@link #FileAlterationMonitor(long, FileAlterationObserver...)}.
6061
* Constructs a monitor with the specified interval and collection of observers.
6162
*
6263
* @param interval The amount of time in milliseconds to wait between
63-
* checks of the file system
64-
* @param observers The collection of observers to add to the monitor
64+
* checks of the file system.
65+
* @param observers The collection of observers to add to the monitor.
66+
* @since 2.9.0
6567
*/
6668
public FileAlterationMonitor(final long interval, final Collection<FileAlterationObserver> observers) {
67-
this(interval, Optional.ofNullable(observers)
69+
// @formatter:off
70+
this(interval,
71+
Optional
72+
.ofNullable(observers)
6873
.orElse(Collections.emptyList())
69-
.toArray(new FileAlterationObserver[0])
74+
.toArray(EMPTY_ARRAY)
7075
);
76+
// @formatter:on
7177
}
7278

7379
/**
7480
* Constructs a monitor with the specified interval and set of observers.
7581
*
7682
* @param interval The amount of time in milliseconds to wait between
77-
* checks of the file system
83+
* checks of the file system.
7884
* @param observers The set of observers to add to the monitor.
7985
*/
8086
public FileAlterationMonitor(final long interval, final FileAlterationObserver... observers) {

src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.File;
2525
import java.util.ArrayList;
26+
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.Iterator;
2829
import java.util.concurrent.Executors;
@@ -69,10 +70,7 @@ public void testCollectionConstructorShouldDoNothingWithNullObservers() {
6970

7071
@Test
7172
public void testCollectionConstructor() {
72-
Collection<FileAlterationObserver> observers = new ArrayList<>();
73-
FileAlterationObserver observer = new FileAlterationObserver("foo");
74-
observers.add(observer);
75-
73+
Collection<FileAlterationObserver> observers = Arrays.asList(new FileAlterationObserver("foo"));
7674
FileAlterationMonitor monitor = new FileAlterationMonitor(0, observers);
7775
Iterator<FileAlterationObserver> iterator = monitor.getObservers().iterator();
7876
assertEquals(observer, iterator.next());

0 commit comments

Comments
 (0)