All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.file.nio.FilenameUtils Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy