View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.model.jdo;
18  
19  import org.apache.jdo.model.jdo.JDOClass;
20  import org.apache.jdo.model.jdo.JDOMember;
21  
22  /***
23   * This is the super interface for named JDO metadata elements, 
24   * such as JDOClass and JDOField.
25   *
26   * @author Michael Bouschen
27   */
28  public class JDOMemberImpl
29      extends JDOElementImpl
30      implements JDOMember
31  {
32      /*** Property name.*/
33      private String name;
34  
35      /*** Relationship JDOClass<->JDOMember. */
36      private JDOClass declaringClass;
37  
38      /*** Constructor. */
39      protected JDOMemberImpl(String name, JDOClass declaringClass)
40      {
41          this.name = name;
42          this.declaringClass = declaringClass;
43      }
44  
45      /***
46       * Returns the name of this JDOMember.
47       * @return the name
48       */
49      public String getName()
50      {
51          return name;
52      }
53  
54      /*** 
55       * Get the declaring class of this JDOMember.
56       * @return the class that owns this JDOMember, or <code>null</code>
57       * if the element is not attached to any class
58       */
59      public JDOClass getDeclaringClass()
60      {
61          return declaringClass;
62      }
63  
64      //================= redefinition of java.lang.Object methods ================
65      
66      /*** 
67       * Overrides Object's <code>toString</code> method to return the name
68       * of this persistence element.
69       * @return a string representation of the object
70       */
71      public String toString () 
72      { 
73          return getName(); 
74      }
75      
76      /*** 
77       * Overrides Object's <code>equals</code> method by comparing the name 
78       * of this member with the name of the argument obj. The method returns 
79       * <code>false</code> if obj does not have the same dynamic type as this 
80       * member.
81       * @return <code>true</code> if this object is the same as the obj argument;
82       * <code>false</code> otherwise.
83       * @param obj the reference object with which to compare.
84       */
85      public boolean equals(Object obj)
86      {
87          if (obj == null)
88              return false;
89          if (obj == this)
90              return true;
91          
92          // check for the right class and then do the name check by 
93          // calling compareTo.
94          return (getClass() == obj.getClass()) && (compareTo(obj) == 0);
95      }
96      
97      /*** Overrides Object's <code>hashCode</code> method to return the hashCode 
98       * of this name.
99       * @return a hash code value for this object.
100      */
101     public int hashCode()
102     {
103         return (getName()==null) ? 0 : getName().hashCode();
104     }
105     
106     //================= implementation of Comparable ================
107     
108     /*** 
109      * Compares this object with the specified object for order. Returns a 
110      * negative integer, zero, or a positive integer as this object is less than, 
111      * equal to, or greater than the specified object. The specified object must 
112      * be a an instance of JDOMember, if not a ClassCastException is thrown.
113      * The order of JDOMember instances is defined by the order of their names.
114      * JDOMember instances without name are considered to be less than any named 
115      * member.
116      * @param o the Object to be compared.
117      * @return a negative integer, zero, or a positive integer as this object is
118      * less than, equal to, or greater than the specified object.
119      * @exception ClassCastException - if the specified object is null or is not 
120      * an instance of JDOMember
121      */
122     public int compareTo(Object o)
123     {
124         // null is not allowed
125         if (o == null)
126             throw new ClassCastException();
127         if (o == this)
128             return 0;
129         
130         String thisName = getName();
131         // the following throws a ClassCastException if o is not a JDOMember
132         String otherName = ((JDOMember)o).getName();
133         // if this does not have a name it should compare less than any named
134         if (thisName == null) {
135             return (otherName == null) ? 0 : -1;
136         }
137         
138         // if this is named and o does not have a name it should compare greater
139         if (otherName == null) {
140             return 1;
141         }
142         
143         // now we know that this and o are named JDOMembers => compare the names
144         int ret = thisName.compareTo(otherName);
145         // If both names are equal, both objects might have different types.
146         // If so order both objects by their type names 
147         // (necessary to be consistent with equals)
148         if ((ret == 0) && (getClass() != o.getClass()))
149             ret = getClass().getName().compareTo(o.getClass().getName());
150         return ret;
151     }
152 }