View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.imports;
21  
22  /**
23   * Represents whether a module is allowed to be imported or not.
24   */
25  class ModuleImportRule extends AbstractImportRule {
26  
27      /** Module to control access to. */
28      private final String moduleName;
29  
30      /**
31       * Constructs an instance.
32       *
33       * @param allow whether to allow access.
34       * @param localOnly whether the rule is to be applied locally only
35       * @param moduleName the module to apply the rule on.
36       * @param regExp whether the module name is to be interpreted as a regular expression.
37       */
38      /* package */ ModuleImportRule(final boolean allow, final boolean localOnly,
39              final String moduleName, final boolean regExp) {
40          super(allow, localOnly, regExp);
41          this.moduleName = moduleName;
42      }
43  
44      /**
45       * Verifies whether a module name is used.
46       *
47       * @param forImport the import to check.
48       * @return a result {@link AccessResult} indicating whether it can be used.
49       */
50      @Override
51      public AccessResult verifyImport(final String forImport) {
52          final boolean moduleMatch;
53          if (isRegExp()) {
54              moduleMatch = forImport.matches(moduleName);
55          }
56          else {
57              moduleMatch = forImport.equals(moduleName);
58          }
59          return calculateResult(moduleMatch);
60      }
61  }