From 5dc6e700cbcbc01833a02a193f660a286b934898 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 13 Jan 2023 02:49:57 +0800 Subject: Code cleanup. --- .../incremental/ObjectCollectionFactory.java | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java') diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java index 04ae96e..095108b 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java @@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Consumer; import java.util.function.Supplier; +import java.util.stream.Stream; /** * Incremental backup is implemented as git-like file collection. @@ -45,17 +46,19 @@ public class ObjectCollectionFactory { throw new IllegalArgumentException("given file is not a directory"); Set files = new HashSet<>(); - for (Iterator iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext(); ) { - Path path = iter.next(); - if (Files.isSameFile(path, directory.toPath())) - continue; - File file = path.toFile(); - if (file.isDirectory()) { - subCollections.put(file.getName(), fromDirectory(file, ignoredFiles)); - } else if (!ignoredFiles.contains(file.getName())) { - files.add(file); // add to the set to be processed - } else { - PrintUtil.info(String.format("Skipping file %s.", file.getName())); + try (Stream walk = Files.walk(directory.toPath(), 1)) { + for (Iterator iter = walk.iterator(); iter.hasNext(); ) { + Path path = iter.next(); + if (Files.isSameFile(path, directory.toPath())) + continue; + File file = path.toFile(); + if (file.isDirectory()) { + subCollections.put(file.getName(), fromDirectory(file, ignoredFiles)); + } else if (!ignoredFiles.contains(file.getName())) { + files.add(file); // add to the set to be processed + } else { + PrintUtil.info(String.format("Skipping file %s.", file.getName())); + } } } @@ -125,7 +128,7 @@ public class ObjectCollectionFactory { public void process() { workers.clear(); for (int i = 0; i < threads; i++) { - ParallelWorker worker = new ParallelWorker(taskList, consumer, i); + ParallelWorker worker = new ParallelWorker<>(taskList, consumer, i); workers.add(worker); worker.start(); } -- cgit v1.2.3