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