From dc59addb6fdcd65e10f1c91a0d868363c80e052a Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Sun, 20 Oct 2019 16:37:14 +0200 Subject: [PATCH] Code cleanup. Made method reportDifferences(...) private since it was exposing package private class TimeNSize and no code outside of jetty-util could have used it. Signed-off-by: Simone Bordet --- .../java/org/eclipse/jetty/util/Scanner.java | 98 +++++-------------- 1 file changed, 27 insertions(+), 71 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java index 7f86529599c..226209d9634 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java @@ -49,11 +49,11 @@ public class Scanner extends AbstractLifeCycle private static int __scannerId = 0; private int _scanInterval; private int _scanCount = 0; - private final List _listeners = new ArrayList(); - private final Map _prevScan = new HashMap(); - private final Map _currentScan = new HashMap(); + private final List _listeners = new ArrayList<>(); + private final Map _prevScan = new HashMap<>(); + private final Map _currentScan = new HashMap<>(); private FilenameFilter _filter; - private final List _scanDirs = new ArrayList(); + private final List _scanDirs = new ArrayList<>(); private volatile boolean _running = false; private boolean _reportExisting = true; private boolean _reportDirs = true; @@ -66,7 +66,7 @@ public class Scanner extends AbstractLifeCycle ADDED, CHANGED, REMOVED } - private final Map _notifications = new HashMap(); + private final Map _notifications = new HashMap<>(); static class TimeNSize { @@ -411,11 +411,7 @@ public class Scanner extends AbstractLifeCycle if (l instanceof ScanListener) ((ScanListener)l).scan(); } - catch (Exception e) - { - LOG.warn(e); - } - catch (Error e) + catch (Throwable e) { LOG.warn(e); } @@ -427,16 +423,11 @@ public class Scanner extends AbstractLifeCycle */ public synchronized void scanFiles() { - if (_scanDirs == null) - return; - _currentScan.clear(); - Iterator itor = _scanDirs.iterator(); - while (itor.hasNext()) + for (File dir : _scanDirs) { - File dir = itor.next(); - if ((dir != null) && (dir.exists())) + { try { scanFile(dir.getCanonicalFile(), _currentScan, 0); @@ -445,6 +436,7 @@ public class Scanner extends AbstractLifeCycle { LOG.warn("Error scanning files.", e); } + } } } @@ -454,11 +446,11 @@ public class Scanner extends AbstractLifeCycle * @param currentScan the info from the most recent pass * @param oldScan info from the previous pass */ - public synchronized void reportDifferences(Map currentScan, Map oldScan) + private synchronized void reportDifferences(Map currentScan, Map oldScan) { // scan the differences and add what was found to the map of notifications: - Set oldScanKeys = new HashSet(oldScan.keySet()); + Set oldScanKeys = new HashSet<>(oldScan.keySet()); // Look for new and changed files for (Map.Entry entry : currentScan.entrySet()) @@ -480,14 +472,8 @@ public class Scanner extends AbstractLifeCycle else if (!oldScan.get(file).equals(currentScan.get(file))) { Notification old = _notifications.put(file, Notification.CHANGED); - if (old != null) - { - switch (old) - { - case ADDED: - _notifications.put(file, Notification.ADDED); - } - } + if (old == Notification.ADDED) + _notifications.put(file, Notification.ADDED); } } @@ -497,14 +483,8 @@ public class Scanner extends AbstractLifeCycle if (!currentScan.containsKey(file)) { Notification old = _notifications.put(file, Notification.REMOVED); - if (old != null) - { - switch (old) - { - case ADDED: - _notifications.remove(file); - } - } + if (old == Notification.ADDED) + _notifications.remove(file); } } @@ -513,7 +493,7 @@ public class Scanner extends AbstractLifeCycle // Process notifications // Only process notifications that are for stable files (ie same in old and current scan). - List bulkChanges = new ArrayList(); + List bulkChanges = new ArrayList<>(); for (Iterator> iter = _notifications.entrySet().iterator(); iter.hasNext(); ) { Entry entry = iter.next(); @@ -565,7 +545,7 @@ public class Scanner extends AbstractLifeCycle if (f.isFile() || depth > 0 && _reportDirs && f.isDirectory()) { - if ((_filter == null) || ((_filter != null) && _filter.accept(f.getParentFile(), f.getName()))) + if (_filter == null || _filter.accept(f.getParentFile(), f.getName())) { if (LOG.isDebugEnabled()) LOG.debug("scan accepted {}", f); @@ -585,9 +565,9 @@ public class Scanner extends AbstractLifeCycle File[] files = f.listFiles(); if (files != null) { - for (int i = 0; i < files.length; i++) + for (File file : files) { - scanFile(files[i], scanInfoMap, depth + 1); + scanFile(file, scanInfoMap, depth + 1); } } else @@ -612,20 +592,14 @@ public class Scanner extends AbstractLifeCycle */ private void reportAddition(String filename) { - Iterator itor = _listeners.iterator(); - while (itor.hasNext()) + for (Listener l : _listeners) { - Listener l = itor.next(); try { if (l instanceof DiscreteListener) ((DiscreteListener)l).fileAdded(filename); } - catch (Exception e) - { - warn(l, filename, e); - } - catch (Error e) + catch (Throwable e) { warn(l, filename, e); } @@ -639,20 +613,14 @@ public class Scanner extends AbstractLifeCycle */ private void reportRemoval(String filename) { - Iterator itor = _listeners.iterator(); - while (itor.hasNext()) + for (Object l : _listeners) { - Object l = itor.next(); try { if (l instanceof DiscreteListener) ((DiscreteListener)l).fileRemoved(filename); } - catch (Exception e) - { - warn(l, filename, e); - } - catch (Error e) + catch (Throwable e) { warn(l, filename, e); } @@ -666,20 +634,14 @@ public class Scanner extends AbstractLifeCycle */ private void reportChange(String filename) { - Iterator itor = _listeners.iterator(); - while (itor.hasNext()) + for (Listener l : _listeners) { - Listener l = itor.next(); try { if (l instanceof DiscreteListener) ((DiscreteListener)l).fileChanged(filename); } - catch (Exception e) - { - warn(l, filename, e); - } - catch (Error e) + catch (Throwable e) { warn(l, filename, e); } @@ -688,20 +650,14 @@ public class Scanner extends AbstractLifeCycle private void reportBulkChanges(List filenames) { - Iterator itor = _listeners.iterator(); - while (itor.hasNext()) + for (Listener l : _listeners) { - Listener l = itor.next(); try { if (l instanceof BulkListener) ((BulkListener)l).filesChanged(filenames); } - catch (Exception e) - { - warn(l, filenames.toString(), e); - } - catch (Error e) + catch (Throwable e) { warn(l, filenames.toString(), e); }