1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
93
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
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
125 if (o == null)
126 throw new ClassCastException();
127 if (o == this)
128 return 0;
129
130 String thisName = getName();
131
132 String otherName = ((JDOMember)o).getName();
133
134 if (thisName == null) {
135 return (otherName == null) ? 0 : -1;
136 }
137
138
139 if (otherName == null) {
140 return 1;
141 }
142
143
144 int ret = thisName.compareTo(otherName);
145
146
147
148 if ((ret == 0) && (getClass() != o.getClass()))
149 ret = getClass().getName().compareTo(o.getClass().getName());
150 return ret;
151 }
152 }