1 package org.apache.ojb.broker.util.collections; 2 3 17 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.PersistenceBrokerException; 20 import org.apache.ojb.broker.metadata.ClassDescriptor; 21 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 25 35 public class RemovalAwareCollection extends ManageableVector implements IRemovalAwareCollection 36 { 37 private Vector allObjectsToBeRemoved = new Vector (); 38 39 42 public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException 43 { 44 allObjectsToBeRemoved.removeAll(this); 47 48 Iterator iter = allObjectsToBeRemoved.iterator(); 49 while (iter.hasNext()) 50 { 51 Object obj = iter.next(); 52 ClassDescriptor cld = broker.getClassDescriptor(obj.getClass()); 53 if (broker.serviceBrokerHelper().assertValidPkForDelete(cld, obj)) 54 { 55 broker.delete(obj); 56 } 57 } 58 allObjectsToBeRemoved.clear(); 59 } 60 61 64 public Object remove(int index) 65 { 66 Object toBeRemoved = super.remove(index); 67 registerForDeletion(toBeRemoved); 68 return toBeRemoved; 69 } 70 71 protected void registerForDeletion(Object toBeRemoved) 72 { 73 if (!allObjectsToBeRemoved.contains(toBeRemoved)) 75 { 76 this.allObjectsToBeRemoved.add(toBeRemoved); 77 } 78 } 79 80 83 public boolean remove(Object o) 84 { 85 boolean result = super.remove(o); 86 registerForDeletion(o); 87 return result; 88 } 89 90 93 public synchronized void clear() 94 { 95 removeAllElements(); 96 } 97 98 101 public synchronized void removeAllElements() 102 { 103 for (int i = 0; i < this.size(); i++) 104 { 105 registerForDeletion(this.get(i)); 106 } 107 super.removeAllElements(); 108 } 109 110 111 114 public synchronized void removeElementAt(int index) 115 { 116 Object toBeDeleted = this.get(index); 117 registerForDeletion(toBeDeleted); 118 super.removeElementAt(index); 119 } 120 121 124 protected void removeRange(int fromIndex, int toIndex) 125 { 126 for (int i = fromIndex; i < toIndex; i++) 127 { 128 registerForDeletion(this.get(i)); 129 } 130 super.removeRange(fromIndex, toIndex); 131 } 132 133 } 134 | Popular Tags |