View Javadoc

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