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

com.brambolt.nio.file.FileNames Maven / Gradle / Ivy

There is a newer version: 2022.05.01-7057
Show newest version
/*
 * Copyright 2017-2021 Brambolt.
 *
 * 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.
 */

package com.brambolt.nio.file;

import java.nio.file.Path;

/**
 * Utility for working with file names.
 */
public class FileNames {

    /**
     * Utility for splitting file names.
     */
    public static class Split { // Poor man's case class

        /**
         * Creates a file name split for the parameter path.
         * @param path The path to split.
         * @return A split object for the path.
         */
        public static Split apply(Path path) {
            return apply(path.getFileName().toString());
        }

        /**
         * Creates a file name split for the parameter file name.
         * @param fileName The file name to split.
         * @return A split object for the file name.
         */
        public static Split apply(String fileName) {
            int offset = fileName.lastIndexOf('.');
            return ((-1 < offset)
                ? new Split(fileName.substring(0, offset), fileName.substring(offset))
                : new Split(fileName, ""));
        }

        /** The file base name without the extension and the dot. */
        public final String prefix;

        /** The file name extension, without the dot. */
        public final String suffix;

        /**
         * Creates a basename-extension split.
         * @param prefix The basename without the dot and the extension.
         * @param suffix The extension.
         */
        public Split(String prefix, String suffix) {
            this.prefix = prefix;
            this.suffix = suffix;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy