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 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 24 34 public class RemovalAwareList extends ManageableArrayList implements IRemovalAwareCollection 35 { 36 private Vector allObjectsToBeRemoved = new Vector (); 37 38 41 public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException 42 { 43 allObjectsToBeRemoved.removeAll(this); 46 47 Iterator iter = allObjectsToBeRemoved.iterator(); 48 while (iter.hasNext()) 49 { 50 broker.delete(iter.next()); 51 } 52 allObjectsToBeRemoved.clear(); 53 } 54 55 58 public Object remove(int index) 59 { 60 Object toBeRemoved = super.remove(index); 61 registerForDeletion(toBeRemoved); 62 return toBeRemoved; 63 } 64 65 protected void registerForDeletion(Object toBeRemoved) 66 { 67 if (!allObjectsToBeRemoved.contains(toBeRemoved)) 69 { 70 this.allObjectsToBeRemoved.add(toBeRemoved); 71 } 72 } 73 74 77 public boolean remove(Object o) 78 { 79 boolean result = super.remove(o); 80 registerForDeletion(o); 81 return result; 82 } 83 84 87 public synchronized void removeAllElements() 88 { 89 for (int i = 0; i < this.size(); i++) 90 { 91 registerForDeletion(this.get(i)); 92 } 93 super.clear(); 94 } 95 96 97 100 public synchronized void removeElementAt(int index) 101 { 102 Object toBeDeleted = this.get(index); 103 registerForDeletion(toBeDeleted); 104 super.remove(index); 105 } 106 107 public synchronized void clear() { 108 removeAllElements(); 109 } 110 111 114 protected void removeRange(int fromIndex, int toIndex) 115 { 116 for (int i = fromIndex; i < toIndex; i++) 117 { 118 registerForDeletion(this.get(i)); 119 } 120 super.removeRange(fromIndex, toIndex); 121 } 122 123 public void resetDeleted() { 124 this.allObjectsToBeRemoved.clear(); 125 } 126 127 } 128 | Popular Tags |