File tree Expand file tree Collapse file tree
main/java/org/apache/commons/io/monitor
test/java/org/apache/commons/io/monitor Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616 */
1717package org .apache .commons .io .monitor ;
1818
19+ import java .util .Collection ;
20+ import java .util .Collections ;
1921import java .util .List ;
22+ import java .util .Optional ;
2023import java .util .concurrent .CopyOnWriteArrayList ;
2124import java .util .concurrent .ThreadFactory ;
2225
@@ -52,6 +55,21 @@ public FileAlterationMonitor(final long interval) {
5255 this .interval = interval ;
5356 }
5457
58+ /**
59+ * Wrapper constructor for {@link #FileAlterationMonitor(long, FileAlterationObserver...)}.
60+ * Constructs a monitor with the specified interval and collection of observers.
61+ *
62+ * @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
65+ */
66+ public FileAlterationMonitor (final long interval , final Collection <FileAlterationObserver > observers ) {
67+ this (interval , Optional .ofNullable (observers )
68+ .orElse (Collections .emptyList ())
69+ .toArray (new FileAlterationObserver [0 ])
70+ );
71+ }
72+
5573 /**
5674 * Constructs a monitor with the specified interval and set of observers.
5775 *
Original file line number Diff line number Diff line change @@ -53,6 +53,31 @@ public void testDefaultConstructor() {
5353 assertEquals (10000 , monitor .getInterval (), "Interval" );
5454 }
5555
56+ @ Test
57+ public void testCollectionConstructorShouldDoNothingWithNullCollection () {
58+ Collection <FileAlterationObserver > observers = null ;
59+ final FileAlterationMonitor monitor = new FileAlterationMonitor (0 , observers );
60+ assertFalse (monitor .getObservers ().iterator ().hasNext ());
61+ }
62+
63+ @ Test
64+ public void testCollectionConstructorShouldDoNothingWithNullObservers () {
65+ Collection <FileAlterationObserver > observers = new ArrayList <>(5 );
66+ final FileAlterationMonitor monitor = new FileAlterationMonitor (0 , observers );
67+ assertFalse (monitor .getObservers ().iterator ().hasNext ());
68+ }
69+
70+ @ Test
71+ public void testCollectionConstructor () {
72+ Collection <FileAlterationObserver > observers = new ArrayList <>();
73+ FileAlterationObserver observer = new FileAlterationObserver ("foo" );
74+ observers .add (observer );
75+
76+ FileAlterationMonitor monitor = new FileAlterationMonitor (0 , observers );
77+ Iterator <FileAlterationObserver > iterator = monitor .getObservers ().iterator ();
78+ assertEquals (observer , iterator .next ());
79+ }
80+
5681 /**
5782 * Test add/remove observers.
5883 */
You can’t perform that action at this time.
0 commit comments