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
022/**
023 * Represents whether a module is allowed to be imported or not.
024 */
025class ModuleImportRule extends AbstractImportRule {
026
027    /** Module to control access to. */
028    private final String moduleName;
029
030    /**
031     * Constructs an instance.
032     *
033     * @param allow whether to allow access.
034     * @param localOnly whether the rule is to be applied locally only
035     * @param moduleName the module to apply the rule on.
036     * @param regExp whether the module name is to be interpreted as a regular expression.
037     */
038    /* package */ ModuleImportRule(final boolean allow, final boolean localOnly,
039            final String moduleName, final boolean regExp) {
040        super(allow, localOnly, regExp);
041        this.moduleName = moduleName;
042    }
043
044    /**
045     * Verifies whether a module name is used.
046     *
047     * @param forImport the import to check.
048     * @return a result {@link AccessResult} indicating whether it can be used.
049     */
050    @Override
051    public AccessResult verifyImport(final String forImport) {
052        final boolean moduleMatch;
053        if (isRegExp()) {
054            moduleMatch = forImport.matches(moduleName);
055        }
056        else {
057            moduleMatch = forImport.equals(moduleName);
058        }
059        return calculateResult(moduleMatch);
060    }
061}