KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > collections > ManageableHashMap


1 package org.apache.ojb.broker.util.collections;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.ojb.broker.ManageableCollection;
22 import org.apache.ojb.broker.PersistenceBroker;
23 import org.apache.ojb.broker.PersistenceBrokerException;
24 import org.apache.ojb.broker.metadata.ClassDescriptor;
25 import org.apache.ojb.broker.metadata.FieldDescriptor;
26 import org.apache.ojb.broker.metadata.MetadataException;
27 import org.apache.ojb.broker.metadata.MetadataManager;
28
29
30 /**
31  * Creates a Map where the primary key is the map key, and the object
32  * is the map value.
33  * <br/>
34  * <strong>Note:</strong> This implementation is limited in use, only objects with
35  * single primary key field are allowed (composed PK's are illegal).
36  */

37 public class ManageableHashMap extends HashMap JavaDoc implements ManageableCollection
38 {
39     public void ojbAdd(Object JavaDoc anObject)
40     {
41         if (anObject != null)
42         {
43             ClassDescriptor cd = MetadataManager.getInstance().getRepository().getDescriptorFor(anObject.getClass());
44             FieldDescriptor[] fields = cd.getPkFields();
45             if(fields.length > 1 || fields.length == 0)
46             {
47                 throw new MetadataException("ManageableHashMap can only be used for persistence capable objects with" +
48                         " exactly one primiary key field defined in metadata, for " + anObject.getClass() + " the" +
49                         " PK field count is " + fields.length);
50             }
51             else
52             {
53                 Object JavaDoc key = fields[0].getPersistentField().get(anObject);
54                 put(key,anObject);
55             }
56         }
57     }
58
59     public void ojbAddAll(ManageableCollection otherCollection)
60     {
61         Iterator JavaDoc it = otherCollection.ojbIterator();
62         while (it.hasNext())
63         {
64             ojbAdd(it.next());
65         }
66     }
67
68     public Iterator JavaDoc ojbIterator()
69     {
70         return values().iterator();
71     }
72
73     public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
74     {
75         //do nothing
76
}
77 }
78
Popular Tags