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