01: package org.apache.ojb.broker.util.collections;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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.2.2 2005/12/21 22:28:15 tomdz Exp $
33: */
34: public class RemovalAwareSet extends ManageableHashSet implements
35: IRemovalAwareCollection {
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)
42: throws PersistenceBrokerException {
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: broker.delete(iter.next());
50: }
51: allObjectsToBeRemoved.clear();
52: }
53:
54: protected void registerForDeletion(Object toBeRemoved) {
55: //only add objects once to avoid double deletions
56: if (!allObjectsToBeRemoved.contains(toBeRemoved)) {
57: this .allObjectsToBeRemoved.add(toBeRemoved);
58: }
59: }
60:
61: /**
62: * @see java.util.Collection#remove(Object)
63: */
64: public boolean remove(Object o) {
65: boolean result = super .remove(o);
66: registerForDeletion(o);
67: return result;
68: }
69:
70: /**
71: * @see java.util.Vector#removeAllElements()
72: */
73: public synchronized void removeAllElements() {
74: for (Iterator it = iterator(); it.hasNext();) {
75: registerForDeletion(it.next());
76: }
77: super .clear();
78: }
79:
80: public synchronized void clear() {
81: removeAllElements();
82: }
83:
84: public void resetDeleted() {
85: this.allObjectsToBeRemoved.clear();
86: }
87:
88: }
|