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 class is allowed to be imported or not. 024 */ 025class ClassImportRule extends AbstractImportRule { 026 027 /** Package to control access to. */ 028 private final String className; 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 className the class to apply the rule on. 036 * @param regExp whether the class name is to be interpreted as a regular expression. 037 */ 038 /* package */ ClassImportRule(final boolean allow, final boolean localOnly, 039 final String className, final boolean regExp) { 040 super(allow, localOnly, regExp); 041 this.className = className; 042 } 043 044 /** 045 * Verifies whether a class 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 classMatch; 053 054 if (isRegExp()) { 055 classMatch = forImport.matches(className); 056 } 057 else { 058 classMatch = forImport.equals(className); 059 } 060 061 return calculateResult(classMatch); 062 } 063 064}