com.crabshue.commons.file.nio.FileRetrievalUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-nio-file Show documentation
Show all versions of commons-nio-file Show documentation
Library for file system operations (using java.nio.file).
package com.crabshue.commons.file.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.file.nio.exceptions.FileErrorContext;
import com.crabshue.commons.file.nio.exceptions.FileErrorType;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
/**
* Utility class dedicated to the retrieval of files.
*/
@Slf4j
public class FileRetrievalUtils {
/**
* Retrieve a file by name in a folder.
*
* @param filename the name of the file
* @param folderPath the folder
* @return the requested file
*/
public static Optional retrieveFileInFolder(@NonNull final String filename,
@NonNull final Path folderPath) {
final Path ret = folderPath.resolve(filename);
if (Files.notExists(ret) || !Files.isRegularFile(ret)) {
return Optional.empty();
}
logger.info("File [{}] found in folder [{}]", ret, folderPath);
return Optional.ofNullable(ret);
}
/**
* Retrieve files by name in a collection of files.
*
* @param filename the name of the file
* @param candidates the candidates
* @return the requested file
*/
public static Collection retrieveFilesByNameAmongFiles(@NonNull final String filename,
@NonNull final Collection candidates) {
logger.debug("Retrieving files with name [{}] among files [{}]", filename, candidates);
final List ret = candidates.stream()
.filter(file -> Objects.equals(filename, file.getFileName().toString()))
.collect(Collectors.toList());
logger.info("Retrieved files [{}]", ret);
return ret;
}
/**
* Retrieve a file by name in a collection of files.
* If multiple files are retrieved with the filename, the first one is returned.
*
* @param filename the name of the file
* @param candidates the candidates
* @return the requested file
*/
public static Optional retrieveFileByNameAmongFiles(@NonNull final String filename,
@NonNull final Collection candidates) {
logger.debug("Retrieving file with name [{}] among files [{}]", filename, candidates);
return FileRetrievalUtils.retrieveFilesByNameAmongFiles(filename, candidates)
.stream()
.findFirst()
.map(file -> {
logger.info("Retrieved file [{}]", file);
return file;
});
}
/**
* List the {@link Path files} in a {@link Path folder} and its sub-folders.
*
* @param folderPath the path to the folder.
* @return the collection of files in the folder.
*/
public static Collection listFilesRecursively(@NonNull final Path folderPath,
@NonNull Predicate... predicates) {
logger.trace("List files recursively in folder [{}]", folderPath);
final List ret = new ArrayList<>();
try {
Files.list(folderPath)
.filter(candidate -> Files.isDirectory(candidate))
.peek(folder -> logger.debug("[{}] is a folder. Will search for files inside it.", folder))
.map(folder -> FileRetrievalUtils.listFilesRecursively(folder, predicates))
.forEach(ret::addAll);
Files.list(folderPath)
.filter(Files::isRegularFile)
.filter(file ->
Stream.of(predicates)
.map(predicate -> predicate.test(file))
.reduce(true, (a, b) -> a && b)
)
.peek(file -> logger.debug("Retrieved file [{}]", file))
.forEach(ret::add);
} catch (IOException e) {
throw new SystemException(FileErrorType.ERROR_READING_FOLDER, e)
.addContextValue(FileErrorContext.FOLDER, folderPath);
}
return ret;
}
/**
* List the {@link Path files} matching a glob pattern in a {@link Path folder} and its sub-folders.
*
* @param folderPath the path to the folder.
* @param glob a Glob pattern
* @return the collection of files in the folder.
*/
public static Collection listFilesRecursively(@NonNull final Path folderPath,
@NonNull final String glob) {
logger.trace("List files with glob [{}] recursively in folder [{}]", glob, folderPath);
final Collection ret = new ArrayList<>();
try {
Files.list(folderPath)
.filter(candidate -> Files.isDirectory(candidate))
.peek(folder -> logger.debug("[{}] is a folder. Will search for files inside it.", folder))
.map(folder -> FileRetrievalUtils.listFilesRecursively(folder, glob))
.forEach(ret::addAll);
Files.newDirectoryStream(folderPath, glob)
.forEach(ret::add);
} catch (IOException e) {
throw new SystemException(FileErrorType.ERROR_READING_FOLDER, e)
.addContextValue(FileErrorContext.FOLDER, folderPath);
}
return ret;
}
}