You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2004/11/13 11:08:17 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/collections ManageableListImpl.java TrackingCollectionImpl.java ManageableSetImpl.java AbstractListWrapper.java AbstractCollectionWrapper.java ManageableSortedSetImpl.java TrackingListImpl.java ManageableMapImpl.java TrackingList.java TrackingCollection.java ManageableArrayList.java ManageableHashSet.java RemovalAwareList.java ManageableHashMap.java ManageableTreeSet.java RemovalAwareCollection.java
brj 2004/11/13 02:08:17
Modified: src/java/org/apache/ojb/broker/util/collections
ManageableArrayList.java ManageableHashSet.java
RemovalAwareList.java ManageableHashMap.java
ManageableTreeSet.java RemovalAwareCollection.java
Added: src/java/org/apache/ojb/broker/util/collections
ManageableListImpl.java TrackingCollectionImpl.java
ManageableSetImpl.java AbstractListWrapper.java
AbstractCollectionWrapper.java
ManageableSortedSetImpl.java TrackingListImpl.java
ManageableMapImpl.java TrackingList.java
TrackingCollection.java
Log:
new collections implementation using composition instead of inheritance.
the old RemovalAwareCollection will be replaced by TrackingList in the future.
Revision Changes Path
1.9 +8 -30 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableArrayList.java
Index: ManageableArrayList.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableArrayList.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ManageableArrayList.java 18 Oct 2004 18:52:07 -0000 1.8
+++ ManageableArrayList.java 13 Nov 2004 10:08:16 -0000 1.9
@@ -16,48 +16,26 @@
*/
import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.apache.ojb.broker.ManageableCollection;
/**
* is a utility class. provides an ArrayList that addionally implements
* the ManageableCollection interface. This class may be used
* as a type for collection attributes.
*
+ * @deprecated use ManageableListImpl
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @version $Id$
*/
-public class ManageableArrayList extends ArrayList implements ManageableCollection
+public class ManageableArrayList extends ManageableListImpl
{
private static final long serialVersionUID = -136869193213411304L;
-
- /**
- * add a single Object to the Collection. This method is used during reading Collection elements
- * from the database. Thus it is is save to cast anObject to the underlying element type of the
- * collection.
- */
- public void ojbAdd(Object anObject)
- {
- this.add(anObject);
- }
-
- /**
- * adds a Collection to this collection. Used in reading Extents from the Database.
- * Thus it is save to cast otherCollection to this.getClass().
- */
- public void ojbAddAll(ManageableCollection otherCollection)
- {
- this.addAll((ManageableArrayList) otherCollection);
- }
-
+
/**
- * returns an Iterator over all elements in the collection. Used during store and delete Operations.
- * If the implementor does not return an iterator over ALL elements, OJB cannot store and delete all elements properly.
- *
+ * Default Constructor. Wraps an ArrayList.
*/
- public Iterator ojbIterator()
+ public ManageableArrayList()
{
- return this.iterator();
+ super(new ArrayList());
}
+
}
1.9 +6 -26 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashSet.java
Index: ManageableHashSet.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashSet.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ManageableHashSet.java 18 Oct 2004 18:52:07 -0000 1.8
+++ ManageableHashSet.java 13 Nov 2004 10:08:16 -0000 1.9
@@ -15,46 +15,26 @@
* limitations under the License.
*/
-import org.apache.ojb.broker.ManageableCollection;
-import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.PersistenceBrokerException;
-
import java.util.HashSet;
-import java.util.Iterator;
/**
* is a utility class. provides a HashSet that addionally implements
* the ManageableCollection interface. This class may be used
* as a type for collection attributes.
*
+ * @deprecated use ManageableSetImpl
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @version $Id$
*/
-public class ManageableHashSet extends HashSet implements ManageableCollection
+public class ManageableHashSet extends ManageableSetImpl
{
private static final long serialVersionUID = 1355425172634396407L;
/**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAdd(java.lang.Object)
- */
- public void ojbAdd(Object anObject)
- {
- super.add(anObject);
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(org.apache.ojb.broker.ManageableCollection)
- */
- public void ojbAddAll(ManageableCollection otherCollection)
- {
- super.addAll((ManageableHashSet) otherCollection);
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
+ * Default Constructor. Wraps a HashSet.
*/
- public Iterator ojbIterator()
+ public ManageableHashSet()
{
- return super.iterator();
+ super(new HashSet());
}
}
1.7 +31 -111 db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareList.java
Index: RemovalAwareList.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareList.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- RemovalAwareList.java 18 Oct 2004 18:52:07 -0000 1.6
+++ RemovalAwareList.java 13 Nov 2004 10:08:16 -0000 1.7
@@ -1,5 +1,8 @@
+
package org.apache.ojb.broker.util.collections;
-/* Copyright 2004-2004 The Apache Software Foundation
+
+
+/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,32 +16,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.metadata.CollectionDescriptor;
import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
-import java.util.Iterator;
-import java.util.Vector;
-
/**
- * This is a list that tracks removal and addition of elements.
- * This tracking allow the PersistenceBroker to delete elements from
- * the database that have been removed from the collection before a
- * PB.store() orperation occurs.
- * This will allow to use the PB api in way pretty close to ODMG persistent
- * collections!
- * @author Thomas Mahler, adapted to ManageableArrayList by Edson C. E. Richter
+ * List that deletes removed Objects.
+ *
+ * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id$
*/
-public class RemovalAwareList extends ManageableArrayList implements IRemovalAwareCollection
+public class RemovalAwareList extends ManageableListImpl implements IRemovalAwareCollection
{
- private static final long serialVersionUID = -4899252835689606974L;
-
- private Vector allObjectsToBeRemoved = new Vector();
+ private static final long serialVersionUID = 3258135743229670457L;
/**
- * @see org.apache.ojb.broker.IRemovalAwareCollection#afterStore(PersistenceBroker, CollectionDescriptor)
+ * Default Constructor.
+ */
+ public RemovalAwareList()
+ {
+ super(new TrackingListImpl(new ArrayList()));
+ }
+
+ /**
+ * @see org.apache.ojb.broker.util.collections.IRemovalAwareCollection#afterStore(org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.metadata.CollectionDescriptor)
*/
public void afterStore(PersistenceBroker broker, CollectionDescriptor cod) throws PersistenceBrokerException
{
@@ -46,108 +52,22 @@
{
return;
}
-
+
+ TrackingCollection tc = (TrackingCollection) getCollection();
+
// make sure allObjectsToBeRemoved does not contain
// any instances that got re-added to the list
- allObjectsToBeRemoved.removeAll(this);
+ tc.getDeletedObjects().removeAll(this);
- Iterator iter = allObjectsToBeRemoved.iterator();
+ Iterator iter = tc.getDeletedObjects().iterator();
while (iter.hasNext())
{
// @todo: once a PB instance is available as a callback parameter we should use it!
broker.delete(iter.next());
}
- allObjectsToBeRemoved.clear();
- }
-
- /**
- * @see java.util.List#remove(int)
- */
- public Object remove(int index)
- {
- Object toBeRemoved = super.remove(index);
- registerForDeletion(toBeRemoved);
- return toBeRemoved;
- }
-
- protected void registerForDeletion(Object toBeRemoved)
- {
- //only add objects once to avoid double deletions
- if (!allObjectsToBeRemoved.contains(toBeRemoved))
- {
- this.allObjectsToBeRemoved.add(toBeRemoved);
- }
- }
-
- /**
- * @see java.util.Collection#remove(Object)
- */
- public boolean remove(Object o)
- {
- boolean result = super.remove(o);
- registerForDeletion(o);
- return result;
- }
-
- /**
- * @see java.util.Vector#removeAllElements()
- */
- public synchronized void removeAllElements()
- {
- for (int i = 0; i < this.size(); i++)
- {
- registerForDeletion(this.get(i));
- }
- super.clear();
- }
-
-
- protected void removeFromDeletion(Object toBeReadded)
- {
- if (allObjectsToBeRemoved.contains(toBeReadded))
- {
- this.allObjectsToBeRemoved.remove(toBeReadded);
- }
- }
-
- /**
- * @see java.util.Collection#add(Object)
- */
- public boolean add(Object o)
- {
- boolean result = super.add(o);
- removeFromDeletion(o);
- return result;
- }
-
- /**
- * @see java.util.Vector#removeElementAt(int)
- */
- public synchronized void removeElementAt(int index)
- {
- Object toBeDeleted = this.get(index);
- registerForDeletion(toBeDeleted);
- super.remove(index);
- }
-
- public synchronized void clear() {
- removeAllElements();
- }
-
- /**
- * @see java.util.AbstractList#removeRange(int, int)
- */
- protected void removeRange(int fromIndex, int toIndex)
- {
- for (int i = fromIndex; i < toIndex; i++)
- {
- registerForDeletion(this.get(i));
- }
- super.removeRange(fromIndex, toIndex);
+ tc.clearDeletedObjects();
+ tc.clearNewObjects();
+
}
- public void resetDeleted() {
- this.allObjectsToBeRemoved.clear();
- }
-
}
1.8 +12 -38 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashMap.java
Index: ManageableHashMap.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashMap.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ManageableHashMap.java 18 Oct 2004 18:52:07 -0000 1.7
+++ ManageableHashMap.java 13 Nov 2004 10:08:16 -0000 1.8
@@ -14,51 +14,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import java.util.HashMap;
-import java.util.Iterator;
-
-import org.apache.ojb.broker.ManageableCollection;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
-import org.apache.ojb.broker.metadata.ClassDescriptor;
+import java.util.HashMap;
/**
- * Creates a Map where the primary key is the map key, and the object is the map value
+ * is a utility class. provides an HashMap that addionally implements
+ * the ManageableCollection interface. This class may be used
+ * as a type for collection attributes.
+ *
+ * @deprecated use ManageableMapImpl
*/
-public class ManageableHashMap extends HashMap implements ManageableCollection
+public class ManageableHashMap extends ManageableMapImpl
{
private static final long serialVersionUID = -2978724878910447122L;
/**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAdd(java.lang.Object)
+ * Default Constructor. Wraps a HashMap.
*/
- public void ojbAdd(Object anObject)
- {
- if (anObject != null)
- {
- ClassDescriptor cd = PersistenceBrokerFactory.defaultPersistenceBroker().getDescriptorRepository().getDescriptorFor(anObject.getClass());
- Object key = cd.getPrimaryKey().getPersistentField().get(anObject);
- put(key,anObject);
- }
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(org.apache.ojb.broker.ManageableCollection)
- */
- public void ojbAddAll(ManageableCollection otherCollection)
- {
- Iterator it = otherCollection.ojbIterator();
- while (it.hasNext())
- {
- ojbAdd(it.next());
- }
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
- */
- public Iterator ojbIterator()
- {
- return values().iterator();
- }
+ public ManageableHashMap()
+ {
+ super(new HashMap());
+ }
}
1.3 +24 -42 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableTreeSet.java
Index: ManageableTreeSet.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableTreeSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ManageableTreeSet.java 18 Oct 2004 18:52:07 -0000 1.2
+++ ManageableTreeSet.java 13 Nov 2004 10:08:16 -0000 1.3
@@ -1,57 +1,39 @@
package org.apache.ojb.broker.util.collections;
-/* Copyright 2002-2004 The Apache Software Foundation
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+/* Copyright 2003-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
-import java.util.Iterator;
import java.util.TreeSet;
-import org.apache.ojb.broker.ManageableCollection;
-
/**
* is a utility class. provides a SortedSet that addionally implements
* the ManageableCollection interface. This class may be used
* as a type for collection attributes.
*
+* @deprecated use ManageableSortedSetImpl
* @author <a href="mailto:dvid@atlas.cz">David Zejda<a>
*/
-public class ManageableTreeSet extends TreeSet implements ManageableCollection
+public class ManageableTreeSet extends ManageableSortedSetImpl
{
private static final long serialVersionUID = 567887377088890892L;
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAdd(java.lang.Object)
- */
- public void ojbAdd(Object anObject)
- {
- super.add(anObject);
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(org.apache.ojb.broker.ManageableCollection)
- */
- public void ojbAddAll(ManageableCollection otherCollection)
- {
- super.addAll((ManageableTreeSet) otherCollection);
- }
-
- /**
- * @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
- */
- public Iterator ojbIterator()
- {
- return super.iterator();
- }
+
+ /**
+ * Default Constructor. Wraps a TreeSet.
+ */
+ public ManageableTreeSet()
+ {
+ super(new TreeSet());
+ }
}
1.10 +27 -101 db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareCollection.java
Index: RemovalAwareCollection.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareCollection.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- RemovalAwareCollection.java 18 Oct 2004 18:52:07 -0000 1.9
+++ RemovalAwareCollection.java 13 Nov 2004 10:08:16 -0000 1.10
@@ -15,32 +15,35 @@
* limitations under the License.
*/
+import java.util.ArrayList;
+import java.util.Iterator;
+
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.metadata.CollectionDescriptor;
import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
-import java.util.Iterator;
-import java.util.Vector;
-
/**
- * This is a collection that tracks removal and addition of elements.
- * This tracking allow the PersistenceBroker to delete elements from
- * the database that have been removed from the collection before a
- * PB.store() orperation occurs.
- * This will allow to use the PB api in way pretty close to ODMG persistent
- * collections!
- * @author Thomas Mahler
+ * Collection that deletes removed objects.
+ * To be compatible with the old version the Collection is actually a List.
+ *
+ * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id$
*/
-public class RemovalAwareCollection extends ManageableVector implements IRemovalAwareCollection
+public class RemovalAwareCollection extends ManageableListImpl implements IRemovalAwareCollection
{
- private static final long serialVersionUID = 4592512000565875203L;
-
- private Vector allObjectsToBeRemoved = new Vector();
+ private static final long serialVersionUID = 3258135743229670457L;
/**
- * @see org.apache.ojb.broker.IRemovalAwareCollection#afterStore(PersistenceBroker, CollectionDescriptor)
+ * @param collection
+ */
+ public RemovalAwareCollection()
+ {
+ super(new TrackingListImpl(new ArrayList()));
+ }
+
+ /**
+ * @see org.apache.ojb.broker.util.collections.IRemovalAwareCollection#afterStore(org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.metadata.CollectionDescriptor)
*/
public void afterStore(PersistenceBroker broker, CollectionDescriptor cod) throws PersistenceBrokerException
{
@@ -49,99 +52,22 @@
return;
}
+ TrackingCollection tc = (TrackingCollection) getCollection();
+
// make sure allObjectsToBeRemoved does not contain
// any instances that got re-added to the list
- allObjectsToBeRemoved.removeAll(this);
+ tc.getDeletedObjects().removeAll(this);
- Iterator iter = allObjectsToBeRemoved.iterator();
+ Iterator iter = tc.getDeletedObjects().iterator();
while (iter.hasNext())
{
// @todo: once a PB instance is available as a callback parameter we should use it!
broker.delete(iter.next());
}
- allObjectsToBeRemoved.clear();
- }
-
- /**
- * @see java.util.List#remove(int)
- */
- public Object remove(int index)
- {
- Object toBeRemoved = super.remove(index);
- registerForDeletion(toBeRemoved);
- return toBeRemoved;
- }
-
- protected void registerForDeletion(Object toBeRemoved)
- {
- //only add objects once to avoid double deletions
- if (!allObjectsToBeRemoved.contains(toBeRemoved))
- {
- this.allObjectsToBeRemoved.add(toBeRemoved);
- }
- }
-
- /**
- * @see java.util.Collection#remove(Object)
- */
- public boolean remove(Object o)
- {
- boolean result = super.remove(o);
- registerForDeletion(o);
- return result;
- }
-
- protected void removeFromDeletion(Object toBeReadded)
- {
- if (allObjectsToBeRemoved.contains(toBeReadded))
- {
- this.allObjectsToBeRemoved.remove(toBeReadded);
- }
- }
-
- /**
- * @see java.util.Collection#add(Object)
- */
- public boolean add(Object o)
- {
- boolean result = super.add(o);
- removeFromDeletion(o);
- return result;
- }
-
- /**
- * @see java.util.Vector#removeAllElements()
- */
- public synchronized void removeAllElements()
- {
- for (int i = 0; i < this.size(); i++)
- {
- registerForDeletion(this.get(i));
- }
- super.removeAllElements();
- }
-
-
- /**
- * @see java.util.Vector#removeElementAt(int)
- */
- public synchronized void removeElementAt(int index)
- {
- Object toBeDeleted = this.get(index);
- registerForDeletion(toBeDeleted);
- super.removeElementAt(index);
- }
-
- /**
- * @see java.util.AbstractList#removeRange(int, int)
- */
- protected void removeRange(int fromIndex, int toIndex)
- {
- for (int i = fromIndex; i < toIndex; i++)
- {
- registerForDeletion(this.get(i));
- }
- super.removeRange(fromIndex, toIndex);
+ tc.clearDeletedObjects();
+ tc.clearNewObjects();
+
+
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableListImpl.java
Index: ManageableListImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.ojb.broker.ManageableCollection;
/**
* Manageable Wrapper for List.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: ManageableListImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class ManageableListImpl extends AbstractListWrapper implements List, ManageableCollection
{
private static final long serialVersionUID = 3256726190730392115L;
/**
* Default Constructor. Wraps an ArrayList.
*/
public ManageableListImpl()
{
this(new ArrayList());
}
/**
* @param list the wrapped List
*/
public ManageableListImpl(List list)
{
super(list);
}
/**
* @param collection the wrapped Collection
*/
protected ManageableListImpl(Collection collection)
{
super(collection);
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbAdd(java.lang.Object)
*/
public void ojbAdd(Object anObject)
{
add(anObject);
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(org.apache.ojb.broker.ManageableCollection)
*/
public void ojbAddAll(ManageableCollection otherCollection)
{
Iterator it = otherCollection.ojbIterator();
while (it.hasNext())
{
ojbAdd(it.next());
}
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
*/
public Iterator ojbIterator()
{
return iterator();
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/TrackingCollectionImpl.java
Index: TrackingCollectionImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* A Wrapper to track modifications on a Collection.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: TrackingCollectionImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class TrackingCollectionImpl extends AbstractCollectionWrapper implements TrackingCollection
{
private static final long serialVersionUID = 3256726190730392115L;
private Collection m_newObjects = new ArrayList();
private Collection m_deletedObjects = new ArrayList();
/**
* @param collection
*/
public TrackingCollectionImpl(Collection collection)
{
super(collection);
}
/**
* @see java.util.Collection#add(java.lang.Object)
*/
public boolean add(Object o)
{
boolean result = super.add(o);
if (result)
{
registerNew(o);
}
return result;
}
/**
* @see java.util.Collection#addAll(java.util.Collection)
*/
public boolean addAll(Collection c)
{
boolean result = super.addAll(c);
if (result)
{
registerNew(c);
}
return result;
}
/**
* @see java.util.Collection#clear()
*/
public void clear()
{
registerDelete(getCollection());
super.clear();
}
/**
* @see java.util.Collection#remove(java.lang.Object)
*/
public boolean remove(Object o)
{
boolean result = super.remove(o);
if (result)
{
registerDelete(o);
}
return result;
}
/**
* @see java.util.Collection#removeAll(java.util.Collection)
*/
public boolean removeAll(Collection c)
{
boolean result = super.removeAll(c);
if (result)
{
registerDelete(c);
}
return result;
}
/**
* @see java.util.Collection#retainAll(java.util.Collection)
*/
public boolean retainAll(Collection c)
{
boolean modified = false;
Iterator iter = iterator();
while (iter.hasNext())
{
Object obj = iter.next();
if (!c.contains(obj))
{
iter.remove();
registerDelete(obj);
modified = true;
}
}
return modified;
}
/**
* @return Returns the deletedObjects.
*/
public Collection getDeletedObjects()
{
return m_deletedObjects;
}
public void clearDeletedObjects()
{
m_deletedObjects.clear();
}
/**
* @return Returns the newObjects.
*/
public Collection getNewObjects()
{
return m_newObjects;
}
public void clearNewObjects()
{
m_newObjects.clear();
}
/**
* Register a deleted Object.
* @param obj
*/
protected void registerDelete(Object obj)
{
removeFromNew(obj);
m_deletedObjects.add(obj);
}
/**
* Register a Collection of deleted Objects.
* @param coll
*/
protected void registerDelete(Collection coll)
{
removeFromNew(coll);
m_deletedObjects.addAll(coll);
}
/**
* Register a new Object.
* @param obj
*/
protected void registerNew(Object obj)
{
removeFromDeletion(obj);
m_newObjects.add(obj);
}
/**
* Register a Collection of new Objects.
* @param coll
*/
protected void registerNew(Collection coll)
{
removeFromDeletion(coll);
m_newObjects.addAll(coll);
}
private void removeFromDeletion(Object toBeReadded)
{
m_deletedObjects.remove(toBeReadded);
}
private void removeFromDeletion(Collection coll)
{
m_deletedObjects.removeAll(coll);
}
private void removeFromNew(Object toBeDeleted)
{
m_newObjects.remove(toBeDeleted);
}
private void removeFromNew(Collection coll)
{
m_newObjects.removeAll(coll);
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableSetImpl.java
Index: ManageableSetImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.HashSet;
import java.util.Set;
/**
* Manageable Wrapper for List.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: ManageableSetImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class ManageableSetImpl extends ManageableListImpl implements Set
{
private static final long serialVersionUID = 3257847666854474551L;
/**
* Default Constructor. Wraps a HashSet.
*/
public ManageableSetImpl()
{
this(new HashSet());
}
/**
* @param set the wrapped Set
*/
public ManageableSetImpl(Set set)
{
super(set);
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/AbstractListWrapper.java
Index: AbstractListWrapper.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
/**
* Wrapper for List.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: AbstractListWrapper.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public abstract class AbstractListWrapper extends AbstractCollectionWrapper implements List
{
/**
* Default Constructor. Wraps an ArrayList.
*/
public AbstractListWrapper()
{
this(new ArrayList());
}
/**
* @param list the wrapped List
*/
public AbstractListWrapper(List list)
{
super(list);
}
/**
* @param collection the wrapped Collection
*/
protected AbstractListWrapper(Collection collection)
{
super(collection);
}
/**
* @return Returns the list.
*/
protected List getList()
{
return (List) getCollection();
}
/**
* @see java.util.List#add(int, java.lang.Object)
*/
public void add(int index, Object element)
{
getList().add(index, element);
}
/**
* @see java.util.List#addAll(int, java.util.Collection)
*/
public boolean addAll(int index, Collection c)
{
return getList().addAll(index, c);
}
/**
* @see java.util.List#get(int)
*/
public Object get(int index)
{
return getList().get(index);
}
/**
* @see java.util.List#indexOf(java.lang.Object)
*/
public int indexOf(Object o)
{
return getList().indexOf(o);
}
/**
* @see java.util.List#lastIndexOf(java.lang.Object)
*/
public int lastIndexOf(Object o)
{
return getList().lastIndexOf(o);
}
/**
* @see java.util.List#listIterator()
*/
public ListIterator listIterator()
{
return getList().listIterator();
}
/**
* @see java.util.List#listIterator(int)
*/
public ListIterator listIterator(int index)
{
return getList().listIterator(index);
}
/**
* @see java.util.List#remove(int)
*/
public Object remove(int index)
{
return getList().remove(index);
}
/**
* @see java.util.List#set(int, java.lang.Object)
*/
public Object set(int index, Object element)
{
return getList().set(index, element);
}
/**
* @see java.util.List#subList(int, int)
*/
public List subList(int fromIndex, int toIndex)
{
return getList().subList(fromIndex, toIndex);
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/AbstractCollectionWrapper.java
Index: AbstractCollectionWrapper.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
/**
* Wrapper for Collection.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: AbstractCollectionWrapper.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public abstract class AbstractCollectionWrapper implements Collection, Serializable
{
private Collection m_collection;
/**
* @param collection the wrapped Collection
*/
public AbstractCollectionWrapper(Collection collection)
{
super();
if (collection == null)
{
throw new IllegalArgumentException("Collection must not be null");
}
setCollection(collection);
}
/**
* @return Returns the collection.
*/
protected Collection getCollection()
{
return m_collection;
}
/**
* @param collection The collection to set.
*/
protected void setCollection(Collection collection)
{
m_collection = collection;
}
/**
* @see java.util.Collection#size()
*/
public int size()
{
return getCollection().size();
}
/**
* @see java.util.Collection#clear()
*/
public void clear()
{
getCollection().clear();
}
/**
* @see java.util.Collection#isEmpty()
*/
public boolean isEmpty()
{
return getCollection().isEmpty();
}
/**
* @see java.util.Collection#toArray()
*/
public Object[] toArray()
{
return getCollection().toArray();
}
/**
* @see java.util.Collection#add(java.lang.Object)
*/
public boolean add(Object o)
{
return getCollection().add(o);
}
/**
* @see java.util.Collection#contains(java.lang.Object)
*/
public boolean contains(Object o)
{
return getCollection().contains(o);
}
/**
* @see java.util.Collection#remove(java.lang.Object)
*/
public boolean remove(Object o)
{
return getCollection().remove(o);
}
/**
* @see java.util.Collection#addAll(java.util.Collection)
*/
public boolean addAll(Collection c)
{
return getCollection().addAll(c);
}
/**
* @see java.util.Collection#containsAll(java.util.Collection)
*/
public boolean containsAll(Collection c)
{
return getCollection().containsAll(c);
}
/**
* @see java.util.Collection#removeAll(java.util.Collection)
*/
public boolean removeAll(Collection c)
{
return getCollection().removeAll(c);
}
/**
* @see java.util.Collection#retainAll(java.util.Collection)
*/
public boolean retainAll(Collection c)
{
return getCollection().retainAll(c);
}
/**
* @see java.util.Collection#iterator()
*/
public Iterator iterator()
{
return getCollection().iterator();
}
/**
* @see java.util.Collection#toArray(java.lang.Object[])
*/
public Object[] toArray(Object[] a)
{
return getCollection().toArray(a);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
return getCollection().equals(obj);
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
return getCollection().hashCode();
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return getCollection().toString();
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableSortedSetImpl.java
Index: ManageableSortedSetImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Manageable Wrapper for SortedSet.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: ManageableSortedSetImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class ManageableSortedSetImpl extends ManageableSetImpl implements SortedSet
{
private static final long serialVersionUID = 3688501091377492532L;
/**
* Default Constructor. Wraps a HashSet.
*/
public ManageableSortedSetImpl()
{
this(new TreeSet());
}
/**
* @param set the wrapped SortedSet
*/
public ManageableSortedSetImpl(SortedSet set)
{
super(set);
}
/**
* @return Returns the sortedSet.
*/
private SortedSet getSortedSet()
{
return (SortedSet) getCollection();
}
/**
* @see java.util.SortedSet#first()
*/
public Object first()
{
return getSortedSet().first();
}
/**
* @see java.util.SortedSet#last()
*/
public Object last()
{
return getSortedSet().last();
}
/**
* @see java.util.SortedSet#comparator()
*/
public Comparator comparator()
{
return getSortedSet().comparator();
}
/**
* @see java.util.SortedSet#headSet(java.lang.Object)
*/
public SortedSet headSet(Object toElement)
{
return getSortedSet().headSet(toElement);
}
/**
* @see java.util.SortedSet#tailSet(java.lang.Object)
*/
public SortedSet tailSet(Object fromElement)
{
return getSortedSet().tailSet(fromElement);
}
/**
* @see java.util.SortedSet#subSet(java.lang.Object, java.lang.Object)
*/
public SortedSet subSet(Object fromElement, Object toElement)
{
return getSortedSet().subSet(fromElement, toElement);
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/TrackingListImpl.java
Index: TrackingListImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
/**
* A Wrapper to track modifications on a List.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: TrackingListImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class TrackingListImpl extends TrackingCollectionImpl implements TrackingList
{
private static final long serialVersionUID = 3256721779866022455L;
/**
* @param collection a List
*/
public TrackingListImpl(List collection)
{
super(collection);
}
protected List getList()
{
return (List) getCollection();
}
/**
* @see java.util.List#add(int, java.lang.Object)
*/
public void add(int index, Object element)
{
registerNew(element);
getList().add(index, element);
}
/**
* @see java.util.List#addAll(int, java.util.Collection)
*/
public boolean addAll(int index, Collection c)
{
registerNew(c);
return getList().addAll(index, c);
}
/**
* @see java.util.List#get(int)
*/
public Object get(int index)
{
return getList().get(index);
}
/**
* @see java.util.List#indexOf(java.lang.Object)
*/
public int indexOf(Object o)
{
return getList().indexOf(o);
}
/**
* @see java.util.List#lastIndexOf(java.lang.Object)
*/
public int lastIndexOf(Object o)
{
return getList().lastIndexOf(o);
}
/**
* @see java.util.List#listIterator()
*/
public ListIterator listIterator()
{
return getList().listIterator();
}
/**
* @see java.util.List#listIterator(int)
*/
public ListIterator listIterator(int index)
{
return getList().listIterator(index);
}
/**
* @see java.util.List#remove(int)
*/
public Object remove(int index)
{
Object obj = get(index);
registerDelete(obj);
return getList().remove(index);
}
/**
* @see java.util.List#set(int, java.lang.Object)
*/
public Object set(int index, Object element)
{
Object obj = get(index);
registerDelete(obj);
registerNew(element);
return getList().set(index, element);
}
/**
* @see java.util.List#subList(int, int)
*/
public List subList(int fromIndex, int toIndex)
{
return getList().subList(fromIndex, toIndex);
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableMapImpl.java
Index: ManageableMapImpl.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.metadata.ClassDescriptor;
/**
* Manageable Wrapper for Map.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: ManageableMapImpl.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public class ManageableMapImpl implements Map, ManageableCollection, Serializable
{
private static final long serialVersionUID = 3256444711508915512L;
private Map m_map;
/**
* @param map the wrapped Map
*/
public ManageableMapImpl(Map map)
{
super();
if (map == null)
{
throw new IllegalArgumentException("Map must not be null");
}
setMap(map);
}
/**
* Default Constructor. Wraps a HashMap.
*/
public ManageableMapImpl()
{
this(new HashMap());
}
/**
* @return Returns the map.
*/
protected Map getMap()
{
return m_map;
}
/**
* @param map The map to set.
*/
protected void setMap(Map map)
{
m_map = map;
}
/**
* @see java.util.Map#size()
*/
public int size()
{
return getMap().size();
}
/**
* @see java.util.Map#clear()
*/
public void clear()
{
getMap().clear();
}
/**
* @see java.util.Map#isEmpty()
*/
public boolean isEmpty()
{
return getMap().isEmpty();
}
/**
* @see java.util.Map#containsKey(java.lang.Object)
*/
public boolean containsKey(Object key)
{
return getMap().containsKey(key);
}
/**
* @see java.util.Map#containsValue(java.lang.Object)
*/
public boolean containsValue(Object value)
{
return getMap().containsValue(value);
}
/**
* @see java.util.Map#values()
*/
public Collection values()
{
return getMap().values();
}
/**
* @see java.util.Map#putAll(java.util.Map)
*/
public void putAll(Map t)
{
getMap().putAll(t);
}
/**
* @see java.util.Map#entrySet()
*/
public Set entrySet()
{
return getMap().entrySet();
}
/**
* @see java.util.Map#keySet()
*/
public Set keySet()
{
return getMap().keySet();
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public Object get(Object key)
{
return getMap().get(key);
}
/**
* @see java.util.Map#remove(java.lang.Object)
*/
public Object remove(Object key)
{
return getMap().remove(key);
}
/**
* @see java.util.Map#put(java.lang.Object, java.lang.Object)
*/
public Object put(Object key, Object value)
{
return getMap().put(key, value);
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbAdd(java.lang.Object)
*/
public void ojbAdd(Object anObject)
{
if (anObject != null)
{
ClassDescriptor cd = PersistenceBrokerFactory.defaultPersistenceBroker().getDescriptorRepository().getDescriptorFor(anObject.getClass());
Object key = cd.getPrimaryKey().getPersistentField().get(anObject);
put(key,anObject);
}
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(org.apache.ojb.broker.ManageableCollection)
*/
public void ojbAddAll(ManageableCollection otherCollection)
{
Iterator it = otherCollection.ojbIterator();
while (it.hasNext())
{
ojbAdd(it.next());
}
}
/**
* @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
*/
public Iterator ojbIterator()
{
return values().iterator();
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
return getMap().equals(obj);
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
return getMap().hashCode();
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return getMap().toString();
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/TrackingList.java
Index: TrackingList.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.List;
/**
* Interface for a List tracking modifications.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: TrackingList.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public interface TrackingList extends TrackingCollection, List
{
}
1.1 db-ojb/src/java/org/apache/ojb/broker/util/collections/TrackingCollection.java
Index: TrackingCollection.java
===================================================================
package org.apache.ojb.broker.util.collections;
/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Collection;
/**
* Interface for a Collection tracking modifications.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: TrackingCollection.java,v 1.1 2004/11/13 10:08:16 brj Exp $
*/
public interface TrackingCollection extends Collection
{
/**
* @return the Collection of deleted objects.
*/
Collection getDeletedObjects();
/**
* @return the Collection of new objects.
*/
Collection getNewObjects();
/**
* Clears the deleted object collection.
*/
void clearDeletedObjects();
/**
* Clears the new object collection.
*/
void clearNewObjects();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org