KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > Scope


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.api;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Represents a Java visibility scope.
27  *
28  * @author <a HREF="mailto:lkuehne@users.sourceforge.net">Lars Kühne</a>
29  */

30 public final class Scope implements Comparable JavaDoc, Serializable JavaDoc
31 {
32     // Note that although this class might seem to be an
33
// implementation detail, this class has to be public because it
34
// is used as a parameter in GlobalProperties.setJavadocScope()
35

36     /** poor man's enum for nothing scope */
37     private static final int SCOPECODE_NOTHING = 0;
38     /** poor man's enum for public scope */
39     private static final int SCOPECODE_PUBLIC = 1;
40     /** poor man's enum for protected scope */
41     private static final int SCOPECODE_PROTECTED = 2;
42     /** poor man's enum for package scope */
43     private static final int SCOPECODE_PACKAGE = 3;
44     /** poor man's enum for private scope */
45     private static final int SCOPECODE_PRIVATE = 4;
46     /** poor man's enum for anonymous inner class scope */
47     private static final int SCOPECODE_ANONINNER = 5;
48
49     /** none scopename */
50     private static final String JavaDoc SCOPENAME_NOTHING = "nothing";
51     /** public scopename */
52     private static final String JavaDoc SCOPENAME_PUBLIC = "public";
53     /** protected scopename */
54     private static final String JavaDoc SCOPENAME_PROTECTED = "protected";
55     /** package scopename */
56     private static final String JavaDoc SCOPENAME_PACKAGE = "package";
57     /** private scopename */
58     private static final String JavaDoc SCOPENAME_PRIVATE = "private";
59     /** anon inner scopename */
60     private static final String JavaDoc SCOPENAME_ANONINNER = "anoninner";
61
62     /** nothing scope. */
63     public static final Scope NOTHING =
64         new Scope(SCOPECODE_NOTHING, SCOPENAME_NOTHING);
65
66     /** public scope. */
67     public static final Scope PUBLIC =
68         new Scope(SCOPECODE_PUBLIC, SCOPENAME_PUBLIC);
69
70     /** protected scope. */
71     public static final Scope PROTECTED =
72         new Scope(SCOPECODE_PROTECTED, SCOPENAME_PROTECTED);
73
74     /** package scope. */
75     public static final Scope PACKAGE =
76         new Scope(SCOPECODE_PACKAGE, SCOPENAME_PACKAGE);
77
78     /** private scope. */
79     public static final Scope PRIVATE =
80         new Scope(SCOPECODE_PRIVATE, SCOPENAME_PRIVATE);
81
82     /** anon inner scope. */
83     public static final Scope ANONINNER =
84         new Scope(SCOPECODE_ANONINNER, SCOPENAME_ANONINNER);
85
86     /** map from scope names to the respective Scope */
87     private static final Map JavaDoc NAME_TO_SCOPE = new HashMap JavaDoc();
88     static {
89         NAME_TO_SCOPE.put(SCOPENAME_NOTHING, NOTHING);
90         NAME_TO_SCOPE.put(SCOPENAME_PUBLIC, PUBLIC);
91         NAME_TO_SCOPE.put(SCOPENAME_PROTECTED, PROTECTED);
92         NAME_TO_SCOPE.put(SCOPENAME_PACKAGE, PACKAGE);
93         NAME_TO_SCOPE.put(SCOPENAME_PRIVATE, PRIVATE);
94         NAME_TO_SCOPE.put(SCOPENAME_ANONINNER, ANONINNER);
95     }
96
97     /** the SCOPECODE_XYZ value of this scope. */
98     private final int mCode;
99
100     /** the name of this scope. */
101     private final String JavaDoc mName;
102
103     /**
104      * {@inheritDoc}
105      */

106     public String JavaDoc toString()
107     {
108         return "Scope[" + mCode + " (" + mName + ")]";
109     }
110
111     /**
112      * @return the name of this scope.
113      */

114     public String JavaDoc getName()
115     {
116         return mName;
117     }
118
119     /**
120      * {@inheritDoc}
121      */

122     public int compareTo(Object JavaDoc aObject)
123     {
124         final Scope s = (Scope) aObject;
125         return this.mCode - s.mCode;
126     }
127
128     /**
129      * Checks if this scope is a subscope of another scope.
130      * Example: PUBLIC is a subscope of PRIVATE.
131      *
132      * @param aScope a <code>Scope</code> value
133      * @return if <code>this</code> is a subscope of <code>aScope</code>.
134      */

135     public boolean isIn(Scope aScope)
136     {
137         return (compareTo(aScope) <= 0);
138     }
139
140     /**
141      * Creates a new <code>Scope</code> instance.
142      *
143      * @param aCode one of the SCOPECODE_XYZ values.
144      * @param aName one of the SCOPENAME_XYZ values.
145      */

146     private Scope(int aCode, String JavaDoc aName)
147     {
148         mCode = aCode;
149         mName = aName;
150     }
151
152     /**
153      * Scope factory method.
154      *
155      * @param aScopeName scope name, such as "nothing", "public", etc.
156      * @return the <code>Scope</code> associated with <code>aScopeName</code>
157      */

158     public static Scope getInstance(String JavaDoc aScopeName)
159     {
160         // TODO: change scope....
161
// canonicalize argument
162
final String JavaDoc scopeName = aScopeName.trim().toLowerCase();
163
164         final Scope retVal = (Scope) NAME_TO_SCOPE.get(scopeName);
165         if (retVal == null) {
166             throw new IllegalArgumentException JavaDoc(scopeName);
167         }
168         return retVal;
169     }
170
171     /**
172      * Ensures that we don't get multiple instances of one Scope
173      * during deserialization. See Section 3.6 of the Java Object
174      * Serialization Specification for details.
175      *
176      * @return the serialization replacement object
177      */

178     private Object JavaDoc readResolve()
179     {
180         return getInstance(mName);
181     }
182 }
183
Popular Tags