KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > Guard


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
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
package com.puppycrawl.tools.checkstyle.checks.imports;
20
21 /**
22  * Represents whether a package is allowed to be used or not.
23  * @author Oliver Burn
24  */

25 class Guard
26 {
27     /** Indicates if allow access or not. */
28     private final boolean mAllowed;
29     /** Package to control access to. */
30     private final String JavaDoc mPkgName;
31     /** Package to control access to. */
32     private final String JavaDoc mClassName;
33
34     /**
35      * Indicates if must be an exact match. Only valid if guard using a
36      * package.
37      */

38     private final boolean mExactMatch;
39     /** Indicates if the guard only applies to this package. */
40     private final boolean mLocalOnly;
41
42     /**
43      * Constructs an instance.
44      * @param aAllow whether to allow access.
45      * @param aLocalOnly whether guard is to be applied locally only
46      * @param aPkgName the package to apply guard on.
47      * @param aExactMatch whether the package must match exactly.
48      */

49     Guard(final boolean aAllow, final boolean aLocalOnly,
50         final String JavaDoc aPkgName, final boolean aExactMatch)
51     {
52         mAllowed = aAllow;
53         mLocalOnly = aLocalOnly;
54         mPkgName = aPkgName;
55         mClassName = null;
56         mExactMatch = aExactMatch;
57     }
58
59     /**
60      * Constructs an instance.
61      * @param aAllow whether to allow access.
62      * @param aLocalOnly whether guard is to be applied locally only
63      * @param aClassName the class to apply guard on.
64      */

65     Guard(final boolean aAllow, final boolean aLocalOnly,
66         final String JavaDoc aClassName)
67     {
68         mAllowed = aAllow;
69         mLocalOnly = aLocalOnly;
70         mPkgName = null;
71         mClassName = aClassName;
72         mExactMatch = true; // not used.
73
}
74
75     /**
76      * Verifies whether a package name be used.
77      * @param aForImport the package to check.
78      * @return a result {@link AccessResult} indicating whether it can be used.
79      */

80     AccessResult verifyImport(final String JavaDoc aForImport)
81     {
82         assert aForImport != null;
83         if (mClassName != null) {
84             final boolean classMatch = mClassName.equals(aForImport);
85             return calculateResult(classMatch);
86         }
87
88         // Must be checking a package. First check that we actually match
89
// the package. Then check if matched and we must be an exact match.
90
// In this case, the text after the first "." must not contain
91
// another "." as this indicates that it is not an exact match.
92
assert mPkgName != null;
93         //boolean pkgMatch = aForImport.startsWith(mPkgName + ".");
94
boolean pkgMatch = aForImport.startsWith(mPkgName + ".");
95         if (pkgMatch && mExactMatch) {
96             pkgMatch = (aForImport.indexOf('.', (mPkgName.length() + 1)) == -1);
97         }
98         return calculateResult(pkgMatch);
99     }
100
101     /**
102      * @return returns whether the guard is to only be applied locally.
103      */

104     boolean isLocalOnly()
105     {
106         return mLocalOnly;
107     }
108
109     /**
110      * Returns the appropriate {@link AccessResult} based on whether there
111      * was a match and if the guard is to allow access.
112      * @param aMatched indicates whether there was a match.
113      * @return An appropriate {@link AccessResult}.
114      */

115     private AccessResult calculateResult(final boolean aMatched)
116     {
117         if (aMatched) {
118             return mAllowed ? AccessResult.ALLOWED : AccessResult.DISALLOWED;
119         }
120         return AccessResult.UNKNOWN;
121     }
122 }
123
Popular Tags