1 package org.apache.ojb.broker.util.collections; 2 3 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 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 37 public class ManageableHashMap extends HashMap implements ManageableCollection 38 { 39 public void ojbAdd(Object 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 key = fields[0].getPersistentField().get(anObject); 54 put(key,anObject); 55 } 56 } 57 } 58 59 public void ojbAddAll(ManageableCollection otherCollection) 60 { 61 Iterator it = otherCollection.ojbIterator(); 62 while (it.hasNext()) 63 { 64 ojbAdd(it.next()); 65 } 66 } 67 68 public Iterator ojbIterator() 69 { 70 return values().iterator(); 71 } 72 73 public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException 74 { 75 } 77 } 78 | Popular Tags |