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