mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
09.08.2019 59949f78c8315dfb5f9b5c83553176e33cd64626
Nothing real.
1 files modified
116 ■■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java 116 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
@@ -3,8 +3,6 @@
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -57,17 +55,9 @@
            }
            return false;
        }
        if (mode == Mode.TREE) {
            // In this run only register all direct childs of currentDirectory.
            String topLevelDir = getTopLevel(item.getPath());
            if (topLevelDir == null) {
                // item is not inside the current directory.
        if (!checkDirectoryMatchAndRegisterSubDirectories(item)) {
                return false;
            }
            if (!subDirectories.containsKey(topLevelDir)) {
                subDirectories.put(topLevelDir, item);
            }
        }
        if (searchKeyWords == null && blackListSearchKeyWords == null) {
            processFinishedFlag();
            return true;
@@ -93,6 +83,35 @@
    }
    /**
     * This method has only effect in FLAT view. This method has to be called for every item of the list before
     * {@link #reduce(List)} may work, because this method registers sub directories of the current directory needed
     * by {@link #reduce(List)}.
     *
     * @param item
     * @return false, if the given item is not a sub item of the current directory. You may skip further checkings for this
     * item. If true, this item might be part of the result.
     */
    boolean checkDirectoryMatchAndRegisterSubDirectories(BorgFilesystemItem item) {
        if (mode != Mode.TREE) {
            return true;
        }
        if (StringUtils.isNotEmpty(currentDirectory) && !item.getPath().startsWith(currentDirectory)) {
            // item is not inside the current directory.
            return false;
        }
        // In this run only register all direct childs of currentDirectory.
        String topLevelDir = getTopLevel(item.getPath());
        if (topLevelDir == null) {
            // item is not inside the current directory.
            return false;
        }
        if (!subDirectories.containsKey(topLevelDir)) {
            subDirectories.put(topLevelDir, item);
        }
        return true;
}
    /**
     * After processing a list by using {@link #matches(BorgFilesystemItem)} you should call finally this method with
     * your result list to reduce the files and directories for mode {@link Mode#TREE}. For the mode {@link Mode#FLAT}
     * nothing is done.
@@ -103,14 +122,8 @@
     * @return The original list for mode {@link Mode#FLAT} or the reduced list for the tree view.
     */
    public List<BorgFilesystemItem> reduce(List<BorgFilesystemItem> list) {
        if (mode == FileSystemFilter.Mode.TREE) {
            if (MapUtils.isEmpty(subDirectories)) {
                // If matches was not called before, do this now for getting all subdirectories.
                subDirectories = new HashMap<>();
                for (BorgFilesystemItem item : list) {
                    // Needed for building subdirectories...
                    this.matches(item);
                }
        if (mode != FileSystemFilter.Mode.TREE) {
            return list;
            }
            Set<String> set = new HashSet<>();
            List<BorgFilesystemItem> list2 = list;
@@ -142,40 +155,10 @@
                    list.add(item);
                }
            }
        }
        return list;
    }
    /**
     * @param path The path of the current item.
     * @return null if the item is not a child of the current directory otherwise the top level sub directory name of
     * the current directory.
     */
    String getTopLevel(String path) {
        if (StringUtils.isEmpty(currentDirectory)) {
            int pos = path.indexOf('/');
            if (pos < 0) {
                return path;
            }
            return path.substring(0, pos);
        }
        if (!path.startsWith(currentDirectory)) {
            // item is not a child of currentDirectory.
            return null;
        }
        if (path.length() <= currentDirectory.length() + 1) {
            // Don't show the current directory itself.
            return null;
        }
        path = StringUtils.removeStart(path, currentDirectory);
        int pos = path.indexOf('/');
        if (pos < 0) {
            return path;
        }
        return path.substring(0, pos);
    }
    /**
     * @param searchString The search string. If this string contains several key words separated by white chars,
     *                     all key words must be found.
     * @return this for chaining.
@@ -211,6 +194,41 @@
    }
    /**
     * currentDirectory '': <tt>home</tt> -&gt; <tt>home</tt><br>
     * currentDirectory '': <tt>home/kai</tt> -&gt; <tt>home</tt><br>
     * currentDirectory 'home': <tt>home</tt> -&gt; <tt>null</tt><br>
     * currentDirectory 'home': <tt>home/kai</tt> -&gt; <tt>kai</tt><br>
     * currentDirectory 'home': <tt>home/kai/test.java</tt> -&gt; <tt>kai</tt><br>
     *
     * @param path The path of the current item.
     * @return null if the item is not a child of the current directory otherwise the top level sub directory name of
     * the current directory.
     */
    String getTopLevel(String path) {
        if (StringUtils.isEmpty(currentDirectory)) {
            int pos = path.indexOf('/');
            if (pos < 0) {
                return path;
            }
            return path.substring(0, pos);
        }
        if (!path.startsWith(currentDirectory)) {
            // item is not a child of currentDirectory.
            return null;
        }
        if (path.length() <= currentDirectory.length() + 1) {
            // Don't show the current directory itself.
            return null;
        }
        path = StringUtils.removeStart(path, currentDirectory);
        int pos = path.indexOf('/');
        if (pos < 0) {
            return path;
        }
        return path.substring(0, pos);
    }
    /**
     * @param mode
     * @return this for chaining.
     */