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