KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > Identifier


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.src;
21
22 /** Represents one identifier.
23 *
24 * @author Petr Hamernik, Jaroslav Tulach
25 */

26 public final class Identifier extends Object JavaDoc implements java.io.Serializable JavaDoc {
27     /**
28      * The identifier was not yet resolved, it was constructed without context information
29      * or with unsuitable context.
30      */

31     public static final int NOT_YET_RESOLVED = 0;
32     /**
33      * The identifier was resolved, the full name is filled in.
34      */

35     public static final int RESOLVED = 1;
36     /**
37      * The identifier cannot be resolved. Although the full name is == source name,
38      * the identifier does not correspond to anything.
39      */

40     public static final int UNRESOLVED = 2;
41     
42     /** Full name of identifier. */
43     private String JavaDoc fullName;
44
45     /** The name which is either the same like fullName either
46     * not fully qualified name - which was taken from the source
47     * by java parser.
48     */

49     private String JavaDoc sourceName;
50
51     /** Position after last dot in the identifier.
52     * It is used in getName method.
53     */

54     private int namePos;
55
56     /** Resolver for sourceName */
57     private transient Resolver resolver;
58     
59     /**
60      * Status of the identifier - one of the symbolic resolution constants
61      */

62     private int status;
63
64     static final long serialVersionUID =-2614114568575211024L;
65
66     /** New identifier.
67     * @param resolver Resolver for fullName
68     * @param sourceName name for code generation
69     */

70     private Identifier(Resolver resolver, String JavaDoc sourceName) {
71         this.fullName = null;
72         this.sourceName = sourceName;
73         this.resolver = resolver;
74         this.status = NOT_YET_RESOLVED;
75     }
76
77     Identifier(String JavaDoc fullName, String JavaDoc sourceName, int status) {
78         if (sourceName == null)
79             throw new IllegalArgumentException JavaDoc();
80         this.sourceName = sourceName;
81         this.fullName = fullName;
82         this.status = status;
83     }
84
85     /** Initiates namePos variable */
86     private void initNamePos() {
87         setFullName();
88         namePos = fullName.lastIndexOf("."); // NOI18N
89
if (namePos != -1)
90             namePos++;
91         if (fullName.startsWith(".") || fullName.endsWith(".")) // NOI18N
92
throw new IllegalArgumentException JavaDoc(fullName);
93     }
94
95     /** Sets fullName variable. */
96     private void setFullName() {
97         if (fullName == null) {
98             fullName = (resolver == null ? sourceName : resolver.resolve().intern());
99             resolver = null; // clear it
100
if (fullName == null) { // not resolved?
101
fullName = sourceName;
102             }
103         }
104     }
105
106     /** Finds the existing instance */
107     private Object JavaDoc readResolve() {
108     if (fullName == null) {
109         // in case the identifier was not resolved before it was saved, it's too late, sorry.
110
return create(sourceName);
111     } else {
112             return create(fullName, sourceName);
113     }
114     }
115
116     /** Create an identifier with the same source name and fully qualified name.
117     *
118     * @param name the fully qualified name to create the identifier from
119     * @return the identifier
120     */

121     public static Identifier create (String JavaDoc name) {
122         String JavaDoc n = name.intern();
123         return new Identifier(null, n);
124     }
125
126     /** Create an identifier.
127     *
128     * @param fullName fully qualified name
129     * @param sourceName name for code generation
130     * @return the identifier
131     */

132     public static Identifier create(String JavaDoc fullName, String JavaDoc sourceName) {
133         Identifier ret = new Identifier(null, sourceName.intern());
134         ret.fullName = fullName.intern();
135         ret.initNamePos();
136         return ret;
137     }
138     
139     public static Identifier create(String JavaDoc full, String JavaDoc sourceName,
140     int status) {
141     Identifier id = Identifier.create(full, sourceName);
142     id.status = status;
143     return id;
144     }
145
146     /** Create an Identifier
147     *
148     * @param resolver a Resolver
149     * @param name the name of the identifier to create
150     */

151     public static Identifier create(Resolver resolver, String JavaDoc name) {
152         return new Identifier(resolver, name.intern());
153     }
154
155     /** Get the simple name within a package.
156     * @return the simple name
157     */

158     public String JavaDoc getName () {
159         int pos = sourceName.lastIndexOf("."); // NOI18N
160
return (pos == -1) ? sourceName : sourceName.substring(pos + 1);
161     }
162
163     /** Get the identifier for the code generation.
164     * @return the name from the source code
165     */

166     public String JavaDoc getSourceName () {
167         return sourceName;
168     }
169
170     /** Get the package prefix.
171     * @return the prefix, with no trailing dot, or an empty string if in the default package
172     */

173     public String JavaDoc getQualifier () {
174         if (fullName == null) {
175             initNamePos();
176         }
177         return (namePos == -1) ? "" : fullName.substring(0, namePos - 1); // NOI18N
178
}
179
180     /** Test whether this identifier is qualified by package.
181     * @return <CODE>true</CODE> if so
182     */

183     public boolean isQualified () {
184         if (fullName == null) {
185             initNamePos();
186         }
187         return (namePos != -1);
188     }
189
190     /** Get the qualified name with the package prefix (if any).
191     * @return the fully qualified name
192     */

193     public String JavaDoc getFullName () {
194         if (fullName == null) {
195             initNamePos();
196         }
197         return fullName;
198     }
199     
200     /**
201      * Returns the resolution status of the identifier. An identifier can be
202      * one of the following:<UL>
203      * <LI> NOT_YET_RESOLVED - no attempt to resolve the identifier was done.
204      * <LI> UNRESOLVED - the identifier does not point to a symbol
205      * <LI> RESOLVED - the identifier identifies the entity specified by the identifier's
206      * fullName property.
207      * </UL>
208      */

209     public int getResolutionStatus() {
210         return this.status;
211     }
212
213     /** This function was changed to match the behaviour of {@link Type#toString Type.toString} that
214         returns text representation suitable for the source file.
215     * @return source name of the identifier.
216     */

217     public String JavaDoc toString() {
218         return getSourceName();
219     }
220
221     /** Compare the specified Identifier with this Identifier for equality.
222     * @param id Identifier to be compared with this
223     * @param source Determine if the source name should be also compared.
224     * If <CODE>false</CODE> only fully qualified name is compared.
225     * @return <CODE>true</CODE> if the specified object equals to
226     * specified Identifier otherwise <CODE>false</CODE>.
227     */

228     public boolean compareTo(Identifier id, boolean source) {
229         if (fullName == null) {
230             initNamePos();
231         }
232         if (id.fullName == null) {
233             id.initNamePos();
234         }
235         if (id.fullName == fullName) {
236             return source ? (id.sourceName == sourceName) : true;
237         }
238         return false;
239     }
240
241     /** Compare the specified object with this Identifier for equality.
242     * There are tested only full qualified name.
243     * @param o Object to be compared with this
244     * @return <CODE>true</CODE> if the specified object is Identifier
245     * with the same fully qualified name,
246     * otherwise <CODE>false</CODE>.
247     */

248     public boolean equals(Object JavaDoc o) {
249         return (o instanceof Identifier) ? compareTo((Identifier) o, false) : false;
250     }
251
252     /** @return the hash code of full name String object.
253     */

254     public int hashCode() {
255         if (fullName == null) {
256             initNamePos();
257         }
258         return fullName.hashCode();
259     }
260
261     /** The interface allows lazy resolving of an Identifier to a fully qualified name. */
262     public static interface Resolver {
263
264         /**
265          * @return fully qualified name
266          */

267         String JavaDoc resolve();
268     }
269 }
270
Popular Tags