001/*
002 * Copyright 2023 the original author or authors.
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * https://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.tools.io;
017
018import static java.util.Objects.requireNonNull;
019
020import lombok.Getter;
021
022/**
023 * Utility class to define file type prefixes.
024 *
025 * @author Sven Haag
026 */
027public enum FileTypePrefix {
028
029    /** "file:" */
030    FILE("file:"),
031
032    /** "classpath:" */
033    CLASSPATH("classpath:"),
034
035    /** "external:" */
036    EXTERNAL("external:"),
037
038    /** "url:" */
039    URL("url:");
040
041    @Getter
042    private final String prefix;
043
044    FileTypePrefix(final String prefix) {
045        this.prefix = prefix;
046    }
047
048    /**
049     * @param path to be checked
050     *
051     * @return true if the given path is prefixed with this enum
052     */
053    public boolean is(final String path) {
054        return null != path && path.startsWith(getPrefix());
055    }
056
057    /**
058     * @param path from which the prefix should be removed
059     *
060     * @return path without {@link #getPrefix()}
061     */
062    public String removePrefix(final String path) {
063        requireNonNull(path);
064        if (is(path)) {
065            return path.substring(getPrefix().length());
066        }
067        return path;
068    }
069
070    @Override
071    public String toString() {
072        return prefix;
073    }
074}