001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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 * Base class for import rules.
024 */
025abstract class AbstractImportRule {
026
027    /** Indicates whether to allow access or not. */
028    private final boolean allowed;
029
030    /** Indicates if the rule only applies to this package. */
031    private final boolean localOnly;
032
033    /**
034     * Indicates if the name is to be interpreted
035     * as a regular expression.
036     */
037    private final boolean regExp;
038
039    /**
040     * Constructs an instance.
041     *
042     * @param allow whether to allow access.
043     * @param localOnly whether the rule is to be applied locally only.
044     * @param regExp whether the name is to be interpreted as a regular
045     *        expression.
046     */
047    protected AbstractImportRule(final boolean allow, final boolean localOnly,
048        final boolean regExp) {
049        allowed = allow;
050        this.localOnly = localOnly;
051        this.regExp = regExp;
052    }
053
054    /**
055     * Verifies whether a package name is used.
056     *
057     * @param forImport the import to check.
058     * @return a result {@link AccessResult} indicating whether it can be used.
059     */
060    public abstract AccessResult verifyImport(String forImport);
061
062    /**
063     * Return true if the guard is to only be applied locally or false.
064     *
065     * @return whether the guard is to only be applied locally.
066     */
067    public boolean isLocalOnly() {
068        return localOnly;
069    }
070
071    /**
072     * Return true if the name is to be interpreted as a regular expression or false.
073     *
074     * @return whether the name is to be interpreted as a regular expression.
075     */
076    protected boolean isRegExp() {
077        return regExp;
078    }
079
080    /**
081     * Returns the appropriate {@link AccessResult} based on whether there
082     * was a match and if the rule is to allow access.
083     *
084     * @param matched indicates whether there was a match.
085     * @return An appropriate {@link AccessResult}.
086     */
087    protected AccessResult calculateResult(final boolean matched) {
088        AccessResult result = AccessResult.UNKNOWN;
089
090        if (matched) {
091            if (allowed) {
092                result = AccessResult.ALLOWED;
093            }
094            else {
095                result = AccessResult.DISALLOWED;
096            }
097        }
098
099        return result;
100    }
101
102}