From 132623419d3457e422dc5a34a6bf441184728f18 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 16 Aug 2021 10:28:37 +0200 Subject: [PATCH] moved `fileWalker` --- .../dataexchange/handler/DataExchange.java | 25 +-------------- src/main/java/ru/bclib/util/PathUtil.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java b/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java index 4b3b65dd..82471339 100644 --- a/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java +++ b/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java @@ -27,7 +27,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.BiConsumer; -import java.util.function.Consumer; import java.util.stream.Stream; abstract public class DataExchange { @@ -133,7 +132,7 @@ abstract public class DataExchange { public void loadCache() { if (fileCache == null) { fileCache = new ArrayList<>(8); - fileWalker(localFolder.toFile(), p -> fileCache.add(new SubFile(localFolder.relativize(p) + PathUtil.fileWalker(localFolder.toFile(), p -> fileCache.add(new SubFile(localFolder.relativize(p) .toString(), FileHash.create(p.toFile())))); /*//this tests if we can trick the system to load files that are not beneath the base-folder @@ -444,26 +443,4 @@ abstract public class DataExchange { BCLib.LOGGER.error(localBaseFolder + " (from " + folderID + ") is outside the game directory " + GAME_FOLDER + ". Sync is not allowed."); } } - - - /** - * A simple directory walker that ignores dot-files - * - * @param path The path where you want to start - * @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object - * for each visited file - */ - public static void fileWalker(File path, Consumer pathConsumer) { - if (!path.exists()) return; - for (final File f : path.listFiles()) { - if (f.getName() - .startsWith(".")) continue; - if (f.isDirectory()) { - fileWalker(f, pathConsumer); - } - else if (f.isFile()) { - pathConsumer.accept(f.toPath()); - } - } - } } diff --git a/src/main/java/ru/bclib/util/PathUtil.java b/src/main/java/ru/bclib/util/PathUtil.java index b7dfbd47..d48e37a0 100644 --- a/src/main/java/ru/bclib/util/PathUtil.java +++ b/src/main/java/ru/bclib/util/PathUtil.java @@ -1,8 +1,18 @@ package ru.bclib.util; +import java.io.File; import java.nio.file.Path; +import java.util.function.Consumer; public class PathUtil { + /** + * Tests if a path is a child-path. + *

+ * A path is a child of another if it is located in the parent or any of the parents subdirectories + * @param parent The folder we search for the {@code child} + * @param child The folder you want to test + * @return {@code true} if {@code child} is in {@code parent} or any of its sub directories + */ public static boolean isChildOf(Path parent, Path child){ if (child==null || parent==null) return false; @@ -14,4 +24,25 @@ public class PathUtil { return child.equals(parent); } + + /** + * A simple directory walker that ignores dot-files + * + * @param path The path where you want to start + * @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object + * for each visited file + */ + public static void fileWalker(File path, Consumer pathConsumer) { + if (!path.exists()) return; + for (final File f : path.listFiles()) { + if (f.getName() + .startsWith(".")) continue; + if (f.isDirectory()) { + fileWalker(f, pathConsumer); + } + else if (f.isFile()) { + pathConsumer.accept(f.toPath()); + } + } + } }