KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeName


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 package org.netbeans.tax;
20
21 /**
22  * Immutable representation of <code>qName</code>.
23  *
24  * @author Libor Kramolis
25  * @version 0.1
26  */

27 public final class TreeName {
28
29     /**
30      * The treeName prefix. For example, the prefix for the treeName "a:foo"
31      * is "a".
32      */

33     final private String JavaDoc prefix;
34
35     /**
36      * The treeName name. For example, the name for the treeName "a:foo"
37      * is "foo".
38      */

39     final private String JavaDoc name;
40
41
42     /**
43      * The treeName rawName. For example, the rawName for the treeName "a:foo"
44      * is "a:foo".
45      */

46     final private String JavaDoc rawName;
47     
48     
49     //
50
// init
51
//
52

53     /** Creates new TreeName.
54      * @throws InvalidArgumentException
55      */

56     public TreeName (String JavaDoc prefix, String JavaDoc name) throws InvalidArgumentException {
57         checkPrefix (prefix);
58         checkName (name);
59         
60         this.prefix = prefix;
61         this.name = name;
62         this.rawName = getQualifiedName (prefix, name);;
63     }
64     
65     /** Creates new TreeName.
66      * @throws InvalidArgumentException
67      */

68     public TreeName (String JavaDoc rawName) throws InvalidArgumentException {
69         checkRawName (rawName);
70         
71         this.prefix = getPrefix (rawName);
72         this.name = getName (rawName);
73         this.rawName = rawName;
74     }
75     
76     // /** Creates new TreeName -- copy constructor. */
77
// public TreeName (TreeName name) {
78
// this.prefix = name.prefix;
79
// this.name = name.name;
80
// this.rawName = name.rawName;
81
// }
82

83     
84     //
85
// itself
86
//
87

88     /**
89      */

90     private static String JavaDoc getPrefix (String JavaDoc rawName) {
91         int i = rawName.indexOf (":"); // NOI18N
92

93         if (i < 0) {
94             return ""; // NOI18N
95
} else {
96             return rawName.substring (0, i);
97         }
98     }
99     
100     /**
101      */

102     private static String JavaDoc getName (String JavaDoc rawName) {
103         int i = rawName.indexOf (":"); // NOI18N
104

105         if (i < 0) {
106             return rawName;
107         } else {
108             return rawName.substring (i + 1);
109         }
110     }
111     
112     /**
113      */

114     private static String JavaDoc getQualifiedName (String JavaDoc prefix, String JavaDoc name) {
115         if ( "".equals (prefix) ) { // NOI18N
116
return name;
117         } else {
118             return (prefix + ":" + name); // NOI18N
119
}
120         
121     }
122     
123     /**
124      */

125     public String JavaDoc getPrefix () {
126         return prefix;
127     }
128     
129     /**
130      */

131     private void checkPrefix (String JavaDoc prefix) throws InvalidArgumentException {
132         if ( prefix == null ) {
133             throw createInvalidNullArgumentException ();
134         }
135     }
136     
137     /**
138      */

139     public String JavaDoc getName () {
140         return name;
141     }
142     
143     /**
144      */

145     private void checkName (String JavaDoc name) throws InvalidArgumentException {
146         if ( name == null ) {
147             throw createInvalidNullArgumentException ();
148         }
149     }
150     
151     /**
152      * Should not it be just getQName() ???
153      */

154     public String JavaDoc getQualifiedName () {
155         return rawName;
156     }
157     
158     /**
159      */

160     private void checkRawName (String JavaDoc rawName) throws InvalidArgumentException {
161         if ( rawName == null ) {
162             throw createInvalidNullArgumentException ();
163         }
164     }
165     
166     /**
167      */

168     private InvalidArgumentException createInvalidNullArgumentException () {
169         return new InvalidArgumentException
170         (Util.THIS.getString ("EXC_invalid_null_value"),
171         new NullPointerException JavaDoc ());
172     }
173     
174     /**
175      */

176     public boolean equals (Object JavaDoc obj) {
177         if ( obj instanceof TreeName ) {
178             return rawName.equals (((TreeName)obj).rawName);
179         }
180         return false;
181     }
182     
183     /**
184      */

185     public int hashCode () {
186         return rawName.hashCode ();
187     }
188     
189     /**
190      */

191     public String JavaDoc toString () {
192         return rawName;
193     }
194     
195 }
196
Popular Tags