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 ar...@apache.org on 2006/09/26 13:37:14 UTC

svn commit: r449999 - in /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker: core/CollectionTypes.java util/IdentityHashSet.java util/collections/RemovalAwareVector.java

Author: arminw
Date: Tue Sep 26 04:37:14 2006
New Revision: 449999

URL: http://svn.apache.org/viewvc?view=rev&rev=449999
Log:
initial check in

Added:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionTypes.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/IdentityHashSet.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/collections/RemovalAwareVector.java

Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionTypes.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionTypes.java?view=auto&rev=449999
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionTypes.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionTypes.java Tue Sep 26 04:37:14 2006
@@ -0,0 +1,254 @@
+package org.apache.ojb.broker.core;
+
+/* Copyright 2002-2006 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.Set;
+import java.util.Vector;
+
+import org.apache.ojb.broker.PersistenceBrokerException;
+import org.apache.ojb.broker.ManageableCollection;
+import org.apache.ojb.broker.OJBRuntimeException;
+import org.apache.ojb.broker.metadata.CollectionDescriptor;
+import org.apache.ojb.broker.metadata.MetadataException;
+import org.apache.ojb.broker.util.collections.ManageableArrayList;
+import org.apache.ojb.broker.util.collections.ManageableHashSet;
+import org.apache.ojb.broker.util.collections.ManageableVector;
+import org.apache.ojb.broker.util.configuration.Configurable;
+import org.apache.ojb.broker.util.configuration.Configuration;
+import org.apache.ojb.broker.util.configuration.ConfigurationException;
+import org.apache.ojb.broker.util.ClassHelper;
+
+/**
+ * This class provide the default collection implementation classes for 1:n, m:n references
+ * and query results.
+ *
+ * @version $Id: $
+ */
+public class CollectionTypes implements Configurable
+{
+    private Class oneToManyArray;
+    private Class oneToManyCollection;
+    private Class oneToManyList;
+    private Class oneToManyVector;
+    private Class oneToManySet;
+
+    private Class manyToManyArray;
+    private Class manyToManyCollection;
+    private Class manyToManyList;
+    private Class manyToManyVector;
+    private Class manyToManySet;
+
+    private Class query;
+    private Class OQLQuery;
+
+    public void configure(Configuration pConfig) throws ConfigurationException
+    {
+        oneToManyArray = pConfig.getClass("CollectionTypes.OneToManyArray", ManageableArrayList.class);
+        oneToManyCollection = pConfig.getClass("CollectionTypes.OneToManyCollection", ManageableArrayList.class);
+        oneToManyList = pConfig.getClass("CollectionTypes.OneToManyList", ManageableArrayList.class);
+        oneToManyVector = pConfig.getClass("CollectionTypes.OneToManyVector", ManageableVector.class);
+        oneToManySet = pConfig.getClass("CollectionTypes.OneToManySet", ManageableHashSet.class);
+
+        manyToManyArray = pConfig.getClass("CollectionTypes.ManyToManyArray", ManageableArrayList.class);
+        manyToManyCollection = pConfig.getClass("CollectionTypes.ManyToManyCollection", ManageableArrayList.class);
+        manyToManyList = pConfig.getClass("CollectionTypes.ManyToManyList", ManageableArrayList.class);
+        manyToManyVector = pConfig.getClass("CollectionTypes.ManyToManyVector", ManageableVector.class);
+        manyToManySet = pConfig.getClass("CollectionTypes.ManyToManySet", ManageableHashSet.class);
+
+        query = pConfig.getClass("CollectionTypes.Query", ManageableArrayList.class);
+        OQLQuery = pConfig.getClass("CollectionTypes.OQLQuery", ManageableArrayList.class);
+    }
+
+    public ManageableCollection createCollectionClass(CollectionDescriptor cold)
+    {
+        Class col = getCollectionClass(cold);
+        try
+        {
+            return (ManageableCollection) ClassHelper.newInstance(col);
+        }
+        catch(Exception e)
+        {
+            throw new OJBRuntimeException("Cannot instantiate the collection class '"
+                    + col.getName() + "' of collection/list/set/array '" + cold.getAttributeName()
+                    + "' in class " + cold.getClassDescriptor().getClassNameOfObject(), e);
+        }
+    }
+
+    public Class getCollectionClass(CollectionDescriptor cds) throws PersistenceBrokerException
+    {
+        Class result = cds.getCollectionClass();
+        if(result == null)
+        {
+            Class fieldType = cds.getPersistentField().getType();
+            
+            if (ManageableCollection.class.isAssignableFrom(fieldType))
+            {
+                result = fieldType;
+            }
+            else if(fieldType.isAssignableFrom(Collection.class))
+            {
+                result = cds.isMtoNRelation() ? getManyToManyCollection() : getOneToManyCollection();
+            }
+            else if(fieldType.isAssignableFrom(List.class))
+            {
+                result = cds.isMtoNRelation() ? getManyToManyList() : getOneToManyList();
+            }
+            else if(fieldType.isAssignableFrom(Set.class))
+            {
+                result = cds.isMtoNRelation() ? getManyToManySet() : getOneToManySet();
+            }
+            else if(fieldType.isAssignableFrom(Vector.class))
+            {
+                result = cds.isMtoNRelation() ? getManyToManyVector() : getOneToManyVector();
+            }
+            else if(fieldType.isArray())
+            {
+                result = cds.isMtoNRelation() ? getManyToManyArray() : getOneToManyArray();
+            }
+            else
+            {
+                throw new MetadataException(
+                        "Cannot determine a collection type for collection/list/set/array field '"
+                        + cds.getAttributeName() + "' of type '" + fieldType + "' in persistence capable class '"
+                        + cds.getClassDescriptor().getClassNameOfObject() + "'");
+            }
+        }
+        return result;
+    }
+
+
+    public Class getOneToManyArray()
+    {
+        return oneToManyArray;
+    }
+
+    public void setOneToManyArray(Class oneToManyArray)
+    {
+        this.oneToManyArray = oneToManyArray;
+    }
+
+    public Class getOneToManyCollection()
+    {
+        return oneToManyCollection;
+    }
+
+    public void setOneToManyCollection(Class oneToManyCollection)
+    {
+        this.oneToManyCollection = oneToManyCollection;
+    }
+
+    public Class getOneToManyList()
+    {
+        return oneToManyList;
+    }
+
+    public void setOneToManyList(Class oneToManyList)
+    {
+        this.oneToManyList = oneToManyList;
+    }
+
+    public Class getOneToManyVector()
+    {
+        return oneToManyVector;
+    }
+
+    public void setOneToManyVector(Class oneToManyVector)
+    {
+        this.oneToManyVector = oneToManyVector;
+    }
+
+    public Class getOneToManySet()
+    {
+        return oneToManySet;
+    }
+
+    public void setOneToManySet(Class oneToManySet)
+    {
+        this.oneToManySet = oneToManySet;
+    }
+
+    public Class getManyToManyArray()
+    {
+        return manyToManyArray;
+    }
+
+    public void setManyToManyArray(Class manyToManyArray)
+    {
+        this.manyToManyArray = manyToManyArray;
+    }
+
+    public Class getManyToManyCollection()
+    {
+        return manyToManyCollection;
+    }
+
+    public void setManyToManyCollection(Class manyToManyCollection)
+    {
+        this.manyToManyCollection = manyToManyCollection;
+    }
+
+    public Class getManyToManyList()
+    {
+        return manyToManyList;
+    }
+
+    public void setManyToManyList(Class manyToManyList)
+    {
+        this.manyToManyList = manyToManyList;
+    }
+
+    public Class getManyToManyVector()
+    {
+        return manyToManyVector;
+    }
+
+    public void setManyToManyVector(Class manyToManyVector)
+    {
+        this.manyToManyVector = manyToManyVector;
+    }
+
+    public Class getManyToManySet()
+    {
+        return manyToManySet;
+    }
+
+    public void setManyToManySet(Class manyToManySet)
+    {
+        this.manyToManySet = manyToManySet;
+    }
+
+    public Class getOQLQuery()
+    {
+        return OQLQuery;
+    }
+
+    public void setOQLQuery(Class OQLQuery)
+    {
+        this.OQLQuery = OQLQuery;
+    }
+
+    public Class getQuery()
+    {
+        return query;
+    }
+
+    public void setQuery(Class query)
+    {
+        this.query = query;
+    }
+}

Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/IdentityHashSet.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/IdentityHashSet.java?view=auto&rev=449999
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/IdentityHashSet.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/IdentityHashSet.java Tue Sep 26 04:37:14 2006
@@ -0,0 +1,94 @@
+package org.apache.ojb.broker.util;
+
+/* Copyright 2002-2006 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.AbstractSet;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.commons.collections.map.IdentityMap;
+
+/**
+ * This is a object identity based {@link java.util.Set} implementation.
+ *
+ * @version $Id: $
+ */
+public class IdentityHashSet extends AbstractSet implements Set
+{
+    private static final Object DUMMY = new Object();
+    private org.apache.commons.collections.map.IdentityMap map;
+
+    public IdentityHashSet()
+    {
+        this(null);
+    }
+
+    public IdentityHashSet(Collection c)
+    {
+        map = new org.apache.commons.collections.map.IdentityMap();
+        if(c != null) addAll(c);
+    }
+
+    public Iterator iterator()
+    {
+        return map.keySet().iterator();
+    }
+
+    public int size()
+    {
+        return map.size();
+    }
+
+    public boolean isEmpty()
+    {
+        return map.isEmpty();
+    }
+
+    public boolean contains(Object o)
+    {
+        return map.containsKey(o);
+    }
+
+    public boolean add(Object o)
+    {
+        return map.put(o, DUMMY) == null;
+    }
+
+    public boolean remove(Object o)
+    {
+        return map.remove(o) == DUMMY;
+    }
+
+    public void clear()
+    {
+        map.clear();
+    }
+
+    public Object clone()
+    {
+        try
+        {
+            IdentityHashSet newSet = (IdentityHashSet) super.clone();
+            newSet.map = (IdentityMap) map.clone();
+            return newSet;
+        }
+        catch(CloneNotSupportedException e)
+        {
+            throw new RuntimeException("Unexpected error", e);
+        }
+    }
+}

Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/collections/RemovalAwareVector.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/collections/RemovalAwareVector.java?view=auto&rev=449999
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/collections/RemovalAwareVector.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/collections/RemovalAwareVector.java Tue Sep 26 04:37:14 2006
@@ -0,0 +1,102 @@
+package org.apache.ojb.broker.util.collections;
+
+/* Copyright 2002-2006 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.Collection;
+import java.util.HashSet;
+
+import org.apache.ojb.broker.PersistenceBroker;
+import org.apache.ojb.broker.PersistenceBrokerException;
+
+/**
+ * This class
+ *
+ * @version $Id: $
+ */
+public class RemovalAwareVector extends ManageableVector
+{
+    private Collection deletedObjects = new HashSet();
+
+    public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
+    {
+        // make sure deletedObjects does not contain
+        // any instances that got re-added to the list
+        deletedObjects.removeAll(this);
+
+        Iterator iter = deletedObjects.iterator();
+        while (iter.hasNext())
+        {
+            broker.delete(iter.next());
+        }
+        deletedObjects.clear();
+    }
+
+    public synchronized Object remove(int index)
+    {
+        deletedObjects.add(get(index));
+        return super.remove(index);
+    }
+
+    public synchronized boolean removeAll(Collection c)
+    {
+        Iterator e = iterator();
+        while(e.hasNext())
+        {
+            Object o = e.next();
+            if(c.contains(o))
+            {
+                deletedObjects.add(o);
+            }
+        }
+        return super.removeAll(c);
+    }
+
+    public synchronized void removeAllElements()
+    {
+        Iterator it = iterator();
+        while(it.hasNext())
+        {
+            deletedObjects.add(it.next());
+        }
+        super.removeAllElements();
+    }
+
+    public synchronized boolean removeElement(Object obj)
+    {
+        boolean result = super.removeElement(obj);
+        if(result) deletedObjects.add(obj);
+        return result;
+    }
+
+    public synchronized void removeElementAt(int index)
+    {
+        deletedObjects.add(get(index));
+        super.removeElementAt(index);
+    }
+
+    protected void removeRange(int fromIndex, int toIndex)
+    {
+        // not supported
+        throw new UnsupportedOperationException("Not supported");
+    }
+
+    public synchronized boolean retainAll(Collection c)
+    {
+        // not supported
+        throw new UnsupportedOperationException("Not supported");
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org