| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RemovalAwareList |
|
| 1.4444444444444444;1.444 |
| 1 | package org.apache.ojb.broker.util.collections; | |
| 2 | ||
| 3 | /* Copyright 2004-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 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 | /** | |
| 25 | * This is a list that tracks removal and addition of elements. | |
| 26 | * This tracking allow the PersistenceBroker to delete elements from | |
| 27 | * the database that have been removed from the collection before a | |
| 28 | * PB.store() orperation occurs. | |
| 29 | * This will allow to use the PB api in way pretty close to ODMG persistent | |
| 30 | * collections! | |
| 31 | * @author Thomas Mahler, adapted to ManageableArrayList by Edson C. E. Richter | |
| 32 | * @version $Id: RemovalAwareList.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $ | |
| 33 | */ | |
| 34 | public class RemovalAwareList extends ManageableArrayList implements IRemovalAwareCollection | |
| 35 | { | |
| 36 | private Vector allObjectsToBeRemoved = new Vector(); | |
| 37 | ||
| 38 | /** | |
| 39 | * @see org.apache.ojb.broker.ManageableCollection#afterStore(PersistenceBroker broker) | |
| 40 | */ | |
| 41 | public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException | |
| 42 | { | |
| 43 | // make sure allObjectsToBeRemoved does not contain | |
| 44 | // any instances that got re-added to the list | |
| 45 | 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 | /** | |
| 56 | * @see java.util.List#remove(int) | |
| 57 | */ | |
| 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 | //only add objects once to avoid double deletions | |
| 68 | if (!allObjectsToBeRemoved.contains(toBeRemoved)) | |
| 69 | { | |
| 70 | this.allObjectsToBeRemoved.add(toBeRemoved); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * @see java.util.Collection#remove(Object) | |
| 76 | */ | |
| 77 | public boolean remove(Object o) | |
| 78 | { | |
| 79 | boolean result = super.remove(o); | |
| 80 | registerForDeletion(o); | |
| 81 | return result; | |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * @see java.util.Vector#removeAllElements() | |
| 86 | */ | |
| 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 | /** | |
| 98 | * @see java.util.Vector#removeElementAt(int) | |
| 99 | */ | |
| 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 | /** | |
| 112 | * @see java.util.AbstractList#removeRange(int, int) | |
| 113 | */ | |
| 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 | } |