View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE CASTOR PROJECT AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * The CASTOR PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Keith Visco. All Rights Reserved.
42   */
43  package org.exolab.javasource;
44  
45  /**
46   * Represents a class name.
47   *
48   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
49   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
50   */
51  public final class JTypeName {
52      //--------------------------------------------------------------------------
53  
54      /** Package name for this type. */
55      private String _package = null;
56      
57      /** Fully-qualified name for this type. */
58      private String _qName = null;
59      
60      /** Local (unqualfied by a packagee) name for this type. */
61      private String _localName = null;
62  
63      //--------------------------------------------------------------------------
64  
65      /**
66       * Creates a default JTypeName.
67       */
68      public JTypeName() {
69          super();
70      }
71  
72      /**
73       * Creates a new JTypeName with the given name.
74       *
75       * @param name The fully qualified class name.
76       */
77      public JTypeName(final String name) {
78          super();
79          
80          init(name);
81      }
82  
83      //--------------------------------------------------------------------------
84  
85      /**
86       * Returns the local name of this JTypeName.
87       *
88       * @return The local name of this JTypeName.
89       */
90      public String getLocalName() {
91          return _localName;
92      }
93  
94      /**
95       * Returns the package name of this JTypeName.
96       *
97       * @return The package name of this JTypeName.
98       */
99      public String getPackageName() {
100         return _package;
101     }
102 
103     /**
104      * Returns the qualified name of this JTypeName.
105      *
106      * @return The qualified name of this JTypeName.
107      */
108     public String getQualifiedName() {
109         if (_qName == null) {
110             if (_localName != null) {
111                 if (_package != null) {
112                     _qName = _package + "." + _localName;
113                 } else {
114                     _qName = _localName;
115                 }
116             } else {
117                 _qName = _package;
118             }
119         }
120         return _qName;
121     }
122 
123     /**
124      * Sets the local name for this JTypeName. Setting the local name will
125      * modify the existing local name and will reset the existing qualified
126      * name.
127      *
128      * @param localName The local name to set.
129      */
130     public void setLocalName(final String localName) {
131         _localName = localName;
132         _qName = null;
133     }
134 
135     /**
136      * Sets the package name of this JTypeName. Setting the package name will
137      * modify the existing package name and will reset the existing qualified
138      * name.
139      *
140      * @param packageName The package name to set.
141      */
142     public void setPackageName(final String packageName) {
143         _package = packageName;
144         _qName = null;
145     }
146 
147     /**
148      * Sets the qualified name of this JTypeName. Setting the qualified name
149      * will overwrite any previous values set via calls to {@link #setLocalName(String)}
150      * and {@link #setPackageName(String)}.
151      *
152      * @param qName The qualified name.
153      */
154     public void setQualifiedName(final String qName) {
155         init(qName);
156     }
157 
158     //--------------------------------------------------------------------------
159 
160     /**
161      * Parses the given name value and initializes the variables.
162      *
163      * @param name The name to initialize with.
164      */
165     private void init(final String name) {
166         if (name == null) {
167             _qName     = null;
168             _localName = null;
169             _package   = null;
170         } else {
171             _qName     = name;
172             _localName = JNaming.getLocalNameFromClassName(name);
173             _package   = JNaming.getPackageFromClassName(name);
174         }
175     }
176 
177     //--------------------------------------------------------------------------
178 
179     /**
180      * {@inheritDoc}
181      */
182     public boolean equals(final Object obj) {
183         if (obj instanceof JTypeName) {
184 
185             JTypeName jname = (JTypeName) obj;
186             String qn1 = jname.getQualifiedName();
187             String qn2 = getQualifiedName();
188             if (qn1 == qn2) { return true; }
189             if (qn1 == null) { return (qn2 == null); }
190             return qn1.equals(qn2);
191         }
192         return false;
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     public int hashCode() {
199         String qName = getQualifiedName();
200         if (qName != null) {
201             return qName.hashCode();
202         }
203         return 0;
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     public String toString() {
210         return getQualifiedName();
211     }
212 
213     //--------------------------------------------------------------------------
214 }