View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
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              // new entry for field name
46              fieldMap = new HashMap();
47              put(mappedByName, fieldMap);
48          }
49          // store jdoField
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              // Get JDOField instance for specified JDOClass instance and 
69              // remove it directly since it is resolved.
70              // Please note, remove returns the removed entry, so we do not
71              // need an extra get for the instance to be returned. 
72              field = (JDOField) fieldMap.remove(jdoClass);
73              // remove the map if it was the last entry
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          // We can delegate to resolve here, because it will remove the
90          // resolved entry from the helper and we simply ignore the returned
91          // value from resolve. 
92          resolve(mappedByName, jdoField.getDeclaringClass());
93      }
94  
95  }