KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 /**
26  * This is a collection that tracks removal and addition of elements.
27  * This tracking allow the PersistenceBroker to delete elements from
28  * the database that have been removed from the collection before a
29  * PB.store() orperation occurs.
30  * This will allow to use the PB api in way pretty close to ODMG persistent
31  * collections!
32  * @author Thomas Mahler
33  * @version $Id: RemovalAwareCollection.java,v 1.7.2.3 2005/12/21 22:28:15 tomdz Exp $
34  */

35 public class RemovalAwareCollection extends ManageableVector implements IRemovalAwareCollection
36 {
37     private Vector JavaDoc allObjectsToBeRemoved = new Vector JavaDoc();
38
39     /**
40      * @see org.apache.ojb.broker.ManageableCollection#afterStore(PersistenceBroker broker)
41      */

42     public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
43     {
44         // make sure allObjectsToBeRemoved does not contain
45
// any instances that got re-added to the list
46
allObjectsToBeRemoved.removeAll(this);
47
48         Iterator JavaDoc iter = allObjectsToBeRemoved.iterator();
49         while (iter.hasNext())
50         {
51             Object JavaDoc 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     /**
62      * @see java.util.List#remove(int)
63      */

64     public Object JavaDoc remove(int index)
65     {
66         Object JavaDoc toBeRemoved = super.remove(index);
67         registerForDeletion(toBeRemoved);
68         return toBeRemoved;
69     }
70
71     protected void registerForDeletion(Object JavaDoc toBeRemoved)
72     {
73         //only add objects once to avoid double deletions
74
if (!allObjectsToBeRemoved.contains(toBeRemoved))
75         {
76             this.allObjectsToBeRemoved.add(toBeRemoved);
77         }
78     }
79
80     /**
81      * @see java.util.Collection#remove(Object)
82      */

83     public boolean remove(Object JavaDoc o)
84     {
85         boolean result = super.remove(o);
86         registerForDeletion(o);
87         return result;
88     }
89
90     /**
91      * @see java.util.Vector#clear()
92      */

93     public synchronized void clear()
94     {
95         removeAllElements();
96     }
97
98     /**
99      * @see java.util.Vector#removeAllElements()
100      */

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     /**
112      * @see java.util.Vector#removeElementAt(int)
113      */

114     public synchronized void removeElementAt(int index)
115     {
116         Object JavaDoc toBeDeleted = this.get(index);
117         registerForDeletion(toBeDeleted);
118         super.removeElementAt(index);
119     }
120
121     /**
122      * @see java.util.AbstractList#removeRange(int, int)
123      */

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