Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RemovalAwareSet |
|
| 1.5;1.5 |
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 java.util.Iterator; | |
19 | import java.util.Vector; | |
20 | import org.apache.ojb.broker.PersistenceBroker; | |
21 | import org.apache.ojb.broker.PersistenceBrokerException; | |
22 | ||
23 | /** | |
24 | * This is a set implementation that tracks removal and addition of elements. | |
25 | * This tracking allow the PersistenceBroker to delete elements from | |
26 | * the database that have been removed from the collection before a | |
27 | * PB.store() orperation occurs. | |
28 | * This will allow to use the PB api in way pretty close to ODMG persistent | |
29 | * collections! | |
30 | * | |
31 | * @author Thomas Dudziak | |
32 | * @version $Id: RemovalAwareSet.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $ | |
33 | */ | |
34 | public class RemovalAwareSet extends ManageableHashSet 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 | protected void registerForDeletion(Object toBeRemoved) | |
56 | { | |
57 | //only add objects once to avoid double deletions | |
58 | if (!allObjectsToBeRemoved.contains(toBeRemoved)) | |
59 | { | |
60 | this.allObjectsToBeRemoved.add(toBeRemoved); | |
61 | } | |
62 | } | |
63 | ||
64 | /** | |
65 | * @see java.util.Collection#remove(Object) | |
66 | */ | |
67 | public boolean remove(Object o) | |
68 | { | |
69 | boolean result = super.remove(o); | |
70 | registerForDeletion(o); | |
71 | return result; | |
72 | } | |
73 | ||
74 | /** | |
75 | * @see java.util.Vector#removeAllElements() | |
76 | */ | |
77 | public synchronized void removeAllElements() | |
78 | { | |
79 | for (Iterator it = iterator(); it.hasNext();) | |
80 | { | |
81 | registerForDeletion(it.next()); | |
82 | } | |
83 | super.clear(); | |
84 | } | |
85 | ||
86 | public synchronized void clear() | |
87 | { | |
88 | removeAllElements(); | |
89 | } | |
90 | ||
91 | public void resetDeleted() | |
92 | { | |
93 | this.allObjectsToBeRemoved.clear(); | |
94 | } | |
95 | ||
96 | } |