View Javadoc

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