View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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 class is allowed to be imported or not.
24   * @author Oliver Burn
25   */
26  class ClassImportRule extends AbstractImportRule {
27      /** Package to control access to. */
28      private final String className;
29  
30      /**
31       * Constructs an instance.
32       * @param allow whether to allow access.
33       * @param localOnly whether the rule is to be applied locally only
34       * @param className the class to apply the rule on.
35       * @param regExp whether the class name is to be interpreted as a regular
36       *        expression.
37       */
38      ClassImportRule(final boolean allow, final boolean localOnly,
39          final String className, final boolean regExp) {
40          super(allow, localOnly, regExp);
41          this.className = className;
42      }
43  
44      /**
45       * Verifies whether a class name is used.
46       * @param forImport the import to check.
47       * @return a result {@link AccessResult} indicating whether it can be used.
48       */
49      @Override
50      public AccessResult verifyImport(final String forImport) {
51          final boolean classMatch;
52  
53          if (isRegExp()) {
54              classMatch = forImport.matches(className);
55          }
56          else {
57              classMatch = forImport.equals(className);
58          }
59  
60          return calculateResult(classMatch);
61      }
62  }