@@ -49,11 +49,11 @@ public class Scanner extends AbstractLifeCycle
49
49
private static int __scannerId = 0 ;
50
50
private int _scanInterval ;
51
51
private int _scanCount = 0 ;
52
- private final List <Listener > _listeners = new ArrayList <Listener >();
53
- private final Map <String , TimeNSize > _prevScan = new HashMap <String , TimeNSize >();
54
- private final Map <String , TimeNSize > _currentScan = new HashMap <String , TimeNSize >();
52
+ private final List <Listener > _listeners = new ArrayList <>();
53
+ private final Map <String , TimeNSize > _prevScan = new HashMap <>();
54
+ private final Map <String , TimeNSize > _currentScan = new HashMap <>();
55
55
private FilenameFilter _filter ;
56
- private final List <File > _scanDirs = new ArrayList <File >();
56
+ private final List <File > _scanDirs = new ArrayList <>();
57
57
private volatile boolean _running = false ;
58
58
private boolean _reportExisting = true ;
59
59
private boolean _reportDirs = true ;
@@ -66,8 +66,7 @@ public enum Notification
66
66
ADDED , CHANGED , REMOVED
67
67
}
68
68
69
- ;
70
- private final Map <String , Notification > _notifications = new HashMap <String , Notification >();
69
+ private final Map <String , Notification > _notifications = new HashMap <>();
71
70
72
71
static class TimeNSize
73
72
{
@@ -412,11 +411,7 @@ public synchronized void scan()
412
411
if (l instanceof ScanListener )
413
412
((ScanListener )l ).scan ();
414
413
}
415
- catch (Exception e )
416
- {
417
- LOG .warn (e );
418
- }
419
- catch (Error e )
414
+ catch (Throwable e )
420
415
{
421
416
LOG .warn (e );
422
417
}
@@ -428,16 +423,11 @@ public synchronized void scan()
428
423
*/
429
424
public synchronized void scanFiles ()
430
425
{
431
- if (_scanDirs == null )
432
- return ;
433
-
434
426
_currentScan .clear ();
435
- Iterator <File > itor = _scanDirs .iterator ();
436
- while (itor .hasNext ())
427
+ for (File dir : _scanDirs )
437
428
{
438
- File dir = itor .next ();
439
-
440
429
if ((dir != null ) && (dir .exists ()))
430
+ {
441
431
try
442
432
{
443
433
scanFile (dir .getCanonicalFile (), _currentScan , 0 );
@@ -446,6 +436,7 @@ public synchronized void scanFiles()
446
436
{
447
437
LOG .warn ("Error scanning files." , e );
448
438
}
439
+ }
449
440
}
450
441
}
451
442
@@ -455,11 +446,11 @@ public synchronized void scanFiles()
455
446
* @param currentScan the info from the most recent pass
456
447
* @param oldScan info from the previous pass
457
448
*/
458
- public synchronized void reportDifferences (Map <String , TimeNSize > currentScan , Map <String , TimeNSize > oldScan )
449
+ private synchronized void reportDifferences (Map <String , TimeNSize > currentScan , Map <String , TimeNSize > oldScan )
459
450
{
460
451
// scan the differences and add what was found to the map of notifications:
461
452
462
- Set <String > oldScanKeys = new HashSet <String >(oldScan .keySet ());
453
+ Set <String > oldScanKeys = new HashSet <>(oldScan .keySet ());
463
454
464
455
// Look for new and changed files
465
456
for (Map .Entry <String , TimeNSize > entry : currentScan .entrySet ())
@@ -484,17 +475,8 @@ public synchronized void reportDifferences(Map<String, TimeNSize> currentScan, M
484
475
else if (!oldScan .get (file ).equals (currentScan .get (file )))
485
476
{
486
477
Notification old = _notifications .put (file , Notification .CHANGED );
487
- if (old != null )
488
- {
489
- switch (old )
490
- {
491
- case ADDED :
492
- _notifications .put (file , Notification .ADDED );
493
- break ;
494
- default :
495
- break ;
496
- }
497
- }
478
+ if (old == Notification .ADDED )
479
+ _notifications .put (file , Notification .ADDED );
498
480
}
499
481
}
500
482
@@ -504,17 +486,8 @@ else if (!oldScan.get(file).equals(currentScan.get(file)))
504
486
if (!currentScan .containsKey (file ))
505
487
{
506
488
Notification old = _notifications .put (file , Notification .REMOVED );
507
- if (old != null )
508
- {
509
- switch (old )
510
- {
511
- case ADDED :
512
- _notifications .remove (file );
513
- break ;
514
- default :
515
- break ;
516
- }
517
- }
489
+ if (old == Notification .ADDED )
490
+ _notifications .remove (file );
518
491
}
519
492
}
520
493
@@ -523,7 +496,7 @@ else if (!oldScan.get(file).equals(currentScan.get(file)))
523
496
524
497
// Process notifications
525
498
// Only process notifications that are for stable files (ie same in old and current scan).
526
- List <String > bulkChanges = new ArrayList <String >();
499
+ List <String > bulkChanges = new ArrayList <>();
527
500
for (Iterator <Entry <String , Notification >> iter = _notifications .entrySet ().iterator (); iter .hasNext (); )
528
501
{
529
502
Entry <String , Notification > entry = iter .next ();
@@ -577,7 +550,7 @@ private void scanFile(File f, Map<String, TimeNSize> scanInfoMap, int depth)
577
550
578
551
if (f .isFile () || depth > 0 && _reportDirs && f .isDirectory ())
579
552
{
580
- if (( _filter == null ) || (( _filter != null ) && _filter .accept (f .getParentFile (), f .getName () )))
553
+ if (_filter == null || _filter .accept (f .getParentFile (), f .getName ()))
581
554
{
582
555
if (LOG .isDebugEnabled ())
583
556
LOG .debug ("scan accepted {}" , f );
@@ -597,9 +570,9 @@ private void scanFile(File f, Map<String, TimeNSize> scanInfoMap, int depth)
597
570
File [] files = f .listFiles ();
598
571
if (files != null )
599
572
{
600
- for (int i = 0 ; i < files . length ; i ++ )
573
+ for (File file : files )
601
574
{
602
- scanFile (files [ i ] , scanInfoMap , depth + 1 );
575
+ scanFile (file , scanInfoMap , depth + 1 );
603
576
}
604
577
}
605
578
else
@@ -624,20 +597,14 @@ private void warn(Object listener, String filename, Throwable th)
624
597
*/
625
598
private void reportAddition (String filename )
626
599
{
627
- Iterator <Listener > itor = _listeners .iterator ();
628
- while (itor .hasNext ())
600
+ for (Listener l : _listeners )
629
601
{
630
- Listener l = itor .next ();
631
602
try
632
603
{
633
604
if (l instanceof DiscreteListener )
634
605
((DiscreteListener )l ).fileAdded (filename );
635
606
}
636
- catch (Exception e )
637
- {
638
- warn (l , filename , e );
639
- }
640
- catch (Error e )
607
+ catch (Throwable e )
641
608
{
642
609
warn (l , filename , e );
643
610
}
@@ -651,20 +618,14 @@ private void reportAddition(String filename)
651
618
*/
652
619
private void reportRemoval (String filename )
653
620
{
654
- Iterator <Listener > itor = _listeners .iterator ();
655
- while (itor .hasNext ())
621
+ for (Object l : _listeners )
656
622
{
657
- Object l = itor .next ();
658
623
try
659
624
{
660
625
if (l instanceof DiscreteListener )
661
626
((DiscreteListener )l ).fileRemoved (filename );
662
627
}
663
- catch (Exception e )
664
- {
665
- warn (l , filename , e );
666
- }
667
- catch (Error e )
628
+ catch (Throwable e )
668
629
{
669
630
warn (l , filename , e );
670
631
}
@@ -678,20 +639,14 @@ private void reportRemoval(String filename)
678
639
*/
679
640
private void reportChange (String filename )
680
641
{
681
- Iterator <Listener > itor = _listeners .iterator ();
682
- while (itor .hasNext ())
642
+ for (Listener l : _listeners )
683
643
{
684
- Listener l = itor .next ();
685
644
try
686
645
{
687
646
if (l instanceof DiscreteListener )
688
647
((DiscreteListener )l ).fileChanged (filename );
689
648
}
690
- catch (Exception e )
691
- {
692
- warn (l , filename , e );
693
- }
694
- catch (Error e )
649
+ catch (Throwable e )
695
650
{
696
651
warn (l , filename , e );
697
652
}
@@ -700,20 +655,14 @@ private void reportChange(String filename)
700
655
701
656
private void reportBulkChanges (List <String > filenames )
702
657
{
703
- Iterator <Listener > itor = _listeners .iterator ();
704
- while (itor .hasNext ())
658
+ for (Listener l : _listeners )
705
659
{
706
- Listener l = itor .next ();
707
660
try
708
661
{
709
662
if (l instanceof BulkListener )
710
663
((BulkListener )l ).filesChanged (filenames );
711
664
}
712
- catch (Exception e )
713
- {
714
- warn (l , filenames .toString (), e );
715
- }
716
- catch (Error e )
665
+ catch (Throwable e )
717
666
{
718
667
warn (l , filenames .toString (), e );
719
668
}
0 commit comments