Java Path File List nio deepListChildren(Path directory)

Here you can find the source of deepListChildren(Path directory)

Description

deep List Children

License

Apache License

Declaration

public static Set<Path> deepListChildren(Path directory) 

Method Source Code


//package com.java2s;
/*//from w  ww.  j av  a 2 s . co  m
 * Copyright (C) 2016 Thiago Gutenberg Carvalho da Costa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;

import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.HashSet;

import java.util.Set;
import java.util.function.BiPredicate;

import java.util.stream.Collectors;

public class Main {
    public static final String DEFAULT_GLOB = "*";

    public static Set<Path> deepListChildren(Path directory) {
        return deepListChildren(directory, DEFAULT_GLOB);
    }

    /**
     * Deep list children path from the provided directory and the glob filter.
     *
     * @param directory the directory
     * @param glob      the glob filter
     * @return a set of found path children
     */
    public static Set<Path> deepListChildren(final Path directory, String glob) {
        if (directory == null || !Files.isDirectory(directory)) {
            return Collections.emptySet();
        }
        glob = glob == null || glob.trim().isEmpty() ? DEFAULT_GLOB : glob;
        final PathMatcher pathMatcher = directory.getFileSystem().getPathMatcher("glob:" + glob);
        BiPredicate<Path, BasicFileAttributes> filter = DEFAULT_GLOB.equals(glob.trim())
                ? (path, basicFileAttributes) -> {
                    try {
                        return !Files.isSameFile(directory, path);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                : (path, basicFileAttributes) -> /*basicFileAttributes.isRegularFile() &&*/
                pathMatcher.matches(path.getFileName());
        try {
            Set<Path> children = Files.find(directory, Integer.MAX_VALUE, filter).collect(Collectors.toSet());
            return Collections.unmodifiableSet(new HashSet<>(children));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. anyPathMatcher(final ImmutableList pathMatchers)
  2. archive(Path archive, List targetFiles)
  3. asFileList(Object... paths)
  4. asPathsList(final File[] files)
  5. compileTreeWithErrors(JavaCompiler compiler, List options, File targetFolder, DiagnosticListener diagnosticListener, boolean addProcessorsToClasspath)
  6. fileListDeep(Path dir)
  7. findExpectedResultForTestcase(final Path testcase, final LinkedList expectedResults)
  8. getClassLoaderFromPaths(ArrayList paths)
  9. getDirectoryList(Path basePath)