001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.imports;
021
022import java.util.regex.Pattern;
023
024/**
025 * Represents an import rules for a specific file. Only the file name is
026 * considered and only files processed by TreeWalker. The file's extension is ignored.
027 */
028class FileImportControl extends AbstractImportControl {
029
030    /** The name for the file. */
031    private final String name;
032    /** The regex pattern for exact matches - only not null if regex is true. */
033    private final Pattern patternForExactMatch;
034    /** If this file name represents a regular expression. */
035    private final boolean regex;
036
037    /**
038     * Construct a file node.
039     *
040     * @param parent the parent node.
041     * @param name the name of the file.
042     * @param regex flags interpretation of name as regex pattern.
043     */
044    /* package */ FileImportControl(PkgImportControl parent, String name, boolean regex) {
045        super(parent, MismatchStrategy.DELEGATE_TO_PARENT);
046        this.regex = regex;
047        this.name = name;
048        patternForExactMatch = createPatternForExactMatch(name);
049    }
050
051    /**
052     * Creates a Pattern from {@code expression}.
053     *
054     * @param expression a self-contained regular expression matching the full file name exactly.
055     * @return a Pattern.
056     */
057    private static Pattern createPatternForExactMatch(String expression) {
058        return Pattern.compile(expression);
059    }
060
061    @Override
062    public AbstractImportControl locateFinest(String forPkg, String forFileName) {
063        AbstractImportControl finestMatch = null;
064        // Check if we are a match.
065        if (matchesExactly(forPkg, forFileName)) {
066            finestMatch = this;
067        }
068        return finestMatch;
069    }
070
071    @Override
072    protected boolean matchesExactly(String pkg, String fileName) {
073        final boolean result;
074        if (fileName == null) {
075            result = false;
076        }
077        else if (regex) {
078            result = patternForExactMatch.matcher(fileName).matches();
079        }
080        else {
081            result = name.equals(fileName);
082        }
083        return result;
084    }
085
086}