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 java.util.Map;
21 import java.util.HashMap;
22
23 import org.apache.jdo.model.jdo.JDOClass;
24 import org.apache.jdo.model.jdo.JDOField;
25
26 /***
27 * A helper class to manage unresolved relationship information.
28 * The class maps the mappedBy name to all JDOField instances using this name
29 * (which might denote fields from different classes) as the mapped by name.
30 * To ease the access the list of JDOField instances is organized as a map
31 * using the declaring JDOClass as key.
32 */
33 class UnresolvedRelationshipHelper extends HashMap
34 {
35 /***
36 * Stores an unresolved relationship entry. The specified JDOField uses
37 * the specified field name in its mappedBy clause. The specified
38 * mappedByName denotes the field on the owning side of the relationship.
39 * @param mappedByName the field name used in the mappedBy clause.
40 * @param jdoField the jdoField instance using the specified field name as
41 * its mappedBy name.
42 */
43 public synchronized void register(String mappedByName, JDOField jdoField) {
44 Map fieldMap = (Map) get(mappedByName);
45 if (fieldMap == null) {
46
47 fieldMap = new HashMap();
48 put(mappedByName, fieldMap);
49 }
50
51 fieldMap.put(jdoField.getDeclaringClass(), jdoField);
52 }
53
54 /***
55 * Look for a JDOField in the unresolved relationship entry having the
56 * specified mappedByName as its mappedBy name. The JDOField must be
57 * declared by the specified jdoClass instance. This allows the owning
58 * side to find the JDOField using the name of the owning side in its
59 * mappedBy clause.
60 * @param mappedByName the field name used as mappedBy name.
61 * @param jdoClass the declaring JDOClass of the field to be returned.
62 * @return a JDOField declared by the specified jdoClass using the
63 * specified mappedByName as its mappedBy name.
64 */
65 public synchronized JDOField resolve(String mappedByName, JDOClass jdoClass) {
66 JDOField field = null;
67 Map fieldMap = (Map) get(mappedByName);
68 if (fieldMap != null) {
69
70
71
72
73 field = (JDOField) fieldMap.remove(jdoClass);
74
75 if (fieldMap.isEmpty()) {
76 remove(mappedByName);
77 }
78 }
79 return field;
80 }
81
82 /***
83 * Removes the specified JDOField from this unresolved relationship
84 * helper.
85 * @param mappedByName the field name used in the mappedBy clause.
86 * @param jdoField the jdoField instance using the specified field name as
87 * its mappedBy name.
88 */
89 public void remove(String mappedByName, JDOField jdoField) {
90
91
92
93 resolve(mappedByName, jdoField.getDeclaringClass());
94 }
95
96 }