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