com.crabshue.commons.file.nio.FilenameUtils 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.nio.file.Path;
import java.util.Optional;
import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.file.nio.exceptions.FileErrorContext;
import com.crabshue.commons.file.nio.exceptions.FileErrorType;
import lombok.NonNull;
/**
* Utility class related to filename operations.
*/
public class FilenameUtils {
/**
* Get the name of a file.
*
* @param file the file.
* @return the filename.
*/
public static String getFilename(@NonNull final Path file) {
return Optional.ofNullable(file.getFileName())
.map(Path::toString)
.orElseThrow(() ->
new ApplicationException(FileErrorType.ERROR_READING_FILENAME)
.addContextValue(FileErrorContext.FILE, file));
}
}