1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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   * Base class for import rules.
24   */
25  abstract class AbstractImportRule {
26  
27      /** Indicates whether to allow access or not. */
28      private final boolean allowed;
29  
30      /** Indicates if the rule only applies to this package. */
31      private final boolean localOnly;
32  
33      /**
34       * Indicates if the name is to be interpreted
35       * as a regular expression.
36       */
37      private final boolean regExp;
38  
39      /**
40       * Constructs an instance.
41       * @param allow whether to allow access.
42       * @param localOnly whether the rule is to be applied locally only.
43       * @param regExp whether the name is to be interpreted as a regular
44       *        expression.
45       */
46      protected AbstractImportRule(final boolean allow, final boolean localOnly,
47          final boolean regExp) {
48          allowed = allow;
49          this.localOnly = localOnly;
50          this.regExp = regExp;
51      }
52  
53      /**
54       * Verifies whether a package name is used.
55       * @param forImport the import to check.
56       * @return a result {@link AccessResult} indicating whether it can be used.
57       */
58      public abstract AccessResult verifyImport(String forImport);
59  
60      /**
61       * Return true if the guard is to only be applied locally or false.
62       * @return whether the guard is to only be applied locally.
63       */
64      public boolean isLocalOnly() {
65          return localOnly;
66      }
67  
68      /**
69       * Return true if the name is to be interpreted as a regular expression or false.
70       * @return whether the name is to be interpreted as a regular expression.
71       */
72      protected boolean isRegExp() {
73          return regExp;
74      }
75  
76      /**
77       * Returns the appropriate {@link AccessResult} based on whether there
78       * was a match and if the rule is to allow access.
79       * @param matched indicates whether there was a match.
80       * @return An appropriate {@link AccessResult}.
81       */
82      protected AccessResult calculateResult(final boolean matched) {
83          AccessResult result = AccessResult.UNKNOWN;
84  
85          if (matched) {
86              if (allowed) {
87                  result = AccessResult.ALLOWED;
88              }
89              else {
90                  result = AccessResult.DISALLOWED;
91              }
92          }
93  
94          return result;
95      }
96  
97  }