001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019: package com.puppycrawl.tools.checkstyle.api;
020:
021: import java.io.Serializable;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: /**
026: * Represents a Java visibility scope.
027: *
028: * @author <a href="mailto:lkuehne@users.sourceforge.net">Lars Kühne</a>
029: */
030: public final class Scope implements Comparable, Serializable {
031: // Note that although this class might seem to be an
032: // implementation detail, this class has to be public because it
033: // is used as a parameter in GlobalProperties.setJavadocScope()
034:
035: /** poor man's enum for nothing scope */
036: private static final int SCOPECODE_NOTHING = 0;
037: /** poor man's enum for public scope */
038: private static final int SCOPECODE_PUBLIC = 1;
039: /** poor man's enum for protected scope */
040: private static final int SCOPECODE_PROTECTED = 2;
041: /** poor man's enum for package scope */
042: private static final int SCOPECODE_PACKAGE = 3;
043: /** poor man's enum for private scope */
044: private static final int SCOPECODE_PRIVATE = 4;
045: /** poor man's enum for anonymous inner class scope */
046: private static final int SCOPECODE_ANONINNER = 5;
047:
048: /** none scopename */
049: private static final String SCOPENAME_NOTHING = "nothing";
050: /** public scopename */
051: private static final String SCOPENAME_PUBLIC = "public";
052: /** protected scopename */
053: private static final String SCOPENAME_PROTECTED = "protected";
054: /** package scopename */
055: private static final String SCOPENAME_PACKAGE = "package";
056: /** private scopename */
057: private static final String SCOPENAME_PRIVATE = "private";
058: /** anon inner scopename */
059: private static final String SCOPENAME_ANONINNER = "anoninner";
060:
061: /** nothing scope. */
062: public static final Scope NOTHING = new Scope(SCOPECODE_NOTHING,
063: SCOPENAME_NOTHING);
064:
065: /** public scope. */
066: public static final Scope PUBLIC = new Scope(SCOPECODE_PUBLIC,
067: SCOPENAME_PUBLIC);
068:
069: /** protected scope. */
070: public static final Scope PROTECTED = new Scope(
071: SCOPECODE_PROTECTED, SCOPENAME_PROTECTED);
072:
073: /** package scope. */
074: public static final Scope PACKAGE = new Scope(SCOPECODE_PACKAGE,
075: SCOPENAME_PACKAGE);
076:
077: /** private scope. */
078: public static final Scope PRIVATE = new Scope(SCOPECODE_PRIVATE,
079: SCOPENAME_PRIVATE);
080:
081: /** anon inner scope. */
082: public static final Scope ANONINNER = new Scope(
083: SCOPECODE_ANONINNER, SCOPENAME_ANONINNER);
084:
085: /** map from scope names to the respective Scope */
086: private static final Map NAME_TO_SCOPE = new HashMap();
087: static {
088: NAME_TO_SCOPE.put(SCOPENAME_NOTHING, NOTHING);
089: NAME_TO_SCOPE.put(SCOPENAME_PUBLIC, PUBLIC);
090: NAME_TO_SCOPE.put(SCOPENAME_PROTECTED, PROTECTED);
091: NAME_TO_SCOPE.put(SCOPENAME_PACKAGE, PACKAGE);
092: NAME_TO_SCOPE.put(SCOPENAME_PRIVATE, PRIVATE);
093: NAME_TO_SCOPE.put(SCOPENAME_ANONINNER, ANONINNER);
094: }
095:
096: /** the SCOPECODE_XYZ value of this scope. */
097: private final int mCode;
098:
099: /** the name of this scope. */
100: private final String mName;
101:
102: /**
103: * {@inheritDoc}
104: */
105: public String toString() {
106: return "Scope[" + mCode + " (" + mName + ")]";
107: }
108:
109: /**
110: * @return the name of this scope.
111: */
112: public String getName() {
113: return mName;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public int compareTo(Object aObject) {
120: final Scope s = (Scope) aObject;
121: return this .mCode - s.mCode;
122: }
123:
124: /**
125: * Checks if this scope is a subscope of another scope.
126: * Example: PUBLIC is a subscope of PRIVATE.
127: *
128: * @param aScope a <code>Scope</code> value
129: * @return if <code>this</code> is a subscope of <code>aScope</code>.
130: */
131: public boolean isIn(Scope aScope) {
132: return (compareTo(aScope) <= 0);
133: }
134:
135: /**
136: * Creates a new <code>Scope</code> instance.
137: *
138: * @param aCode one of the SCOPECODE_XYZ values.
139: * @param aName one of the SCOPENAME_XYZ values.
140: */
141: private Scope(int aCode, String aName) {
142: mCode = aCode;
143: mName = aName;
144: }
145:
146: /**
147: * Scope factory method.
148: *
149: * @param aScopeName scope name, such as "nothing", "public", etc.
150: * @return the <code>Scope</code> associated with <code>aScopeName</code>
151: */
152: public static Scope getInstance(String aScopeName) {
153: // TODO: change scope....
154: // canonicalize argument
155: final String scopeName = aScopeName.trim().toLowerCase();
156:
157: final Scope retVal = (Scope) NAME_TO_SCOPE.get(scopeName);
158: if (retVal == null) {
159: throw new IllegalArgumentException(scopeName);
160: }
161: return retVal;
162: }
163:
164: /**
165: * Ensures that we don't get multiple instances of one Scope
166: * during deserialization. See Section 3.6 of the Java Object
167: * Serialization Specification for details.
168: *
169: * @return the serialization replacement object
170: */
171: private Object readResolve() {
172: return getInstance(mName);
173: }
174: }
|