cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r448453 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/intercept/ test/java/org/apache/cayenne/intercept/
Date Thu, 21 Sep 2006 04:07:17 GMT
Author: aadamchik
Date: Wed Sep 20 21:07:16 2006
New Revision: 448453

URL: http://svn.apache.org/viewvc?view=rev&rev=448453
Log:
CAY-660 - refactoring callback processing

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/CallbackMap.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptorTst.java

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/CallbackMap.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/CallbackMap.java?view=auto&rev=448453
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/CallbackMap.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/CallbackMap.java
Wed Sep 20 21:07:16 2006
@@ -0,0 +1,85 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ ****************************************************************/
+package org.apache.cayenne.intercept;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.collections.Closure;
+
+/**
+ * A helper class that stores a bunch of callbacks by entity class.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class CallbackMap {
+
+    private Map callbacks;
+
+    CallbackMap() {
+        this.callbacks = new HashMap();
+    }
+    
+    boolean isEmpty() {
+        return callbacks.isEmpty();
+    }
+
+    void addCallback(Class entityClass, String methodName) {
+        addCallback(entityClass, new EntityCallback(entityClass, methodName));
+    }
+
+    void addCallback(Class objectClass, String methodName, Class entityClass) {
+        addCallback(entityClass, new ListenerCallback(
+                objectClass,
+                methodName,
+                entityClass));
+    }
+
+    private void addCallback(Class entityClass, Closure callback) {
+        Collection entityCallbacks = (Collection) callbacks.get(entityClass.getName());
+
+        if (entityCallbacks == null) {
+            entityCallbacks = new ArrayList(3);
+            callbacks.put(entityClass.getName(), entityCallbacks);
+        }
+
+        entityCallbacks.add(callback);
+    }
+
+    /**
+     * Utility method applying callbacks to a collection.
+     */
+    void applyCallbacks(Object object) {
+        Collection callbacks = getCallbacks(object.getClass());
+        if (callbacks != null) {
+            Iterator it = (Iterator) callbacks.iterator();
+            while (it.hasNext()) {
+                ((Closure) it.next()).execute(object);
+            }
+        }
+    }
+
+    Collection getCallbacks(Class entityClass) {
+        return (Collection) callbacks.get(entityClass.getName());
+    }
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java?view=diff&rev=448453&r1=448452&r2=448453
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
Wed Sep 20 21:07:16 2006
@@ -18,11 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.intercept;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.Map;
 
 import org.apache.cayenne.DeleteDenyException;
 import org.apache.cayenne.PersistenceState;
@@ -31,7 +28,6 @@
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.property.ClassDescriptor;
-import org.apache.commons.collections.Closure;
 
 /**
  * Implements JPA-compliant "PrePersist", "PreRemove" callbacks for the ObjectContext
@@ -46,12 +42,8 @@
  */
 public class ObjectContextCallbackInterceptor extends ObjectContextDecorator {
 
-    static final int PRE_PERSIST_INDEX = 0;
-    static final int PRE_REMOVE_INDEX = 1;
-
-    private static final int CALLBACK_SIZE = 2;
-
-    protected Map callbacks = new HashMap();
+    CallbackMap prePersist = new CallbackMap();
+    CallbackMap preRemove = new CallbackMap();
 
     /**
      * Creates a new object, applying "PrePersist" callbacks to it.
@@ -59,7 +51,7 @@
     public Persistent newObject(Class persistentClass) {
         Persistent object = super.newObject(persistentClass);
 
-        applyCallbacks(getCallbacks(persistentClass, PRE_PERSIST_INDEX), object);
+        prePersist.applyCallbacks(object);
 
         return object;
     }
@@ -74,28 +66,18 @@
     }
 
     public void addPrePersistCallback(Class entityClass, String methodName) {
-        addCallback(
-                entityClass,
-                new EntityCallback(entityClass, methodName),
-                PRE_PERSIST_INDEX);
+        prePersist.addCallback(entityClass, methodName);
     }
 
     public void addPrePersistCallback(
             Class objectClass,
             String methodName,
             Class entityClass) {
-
-        addCallback(entityClass, new ListenerCallback(
-                objectClass,
-                methodName,
-                entityClass), PRE_PERSIST_INDEX);
+        prePersist.addCallback(objectClass, methodName, entityClass);
     }
 
     public void addPreRemoveCallback(Class entityClass, String methodName) {
-        addCallback(
-                entityClass,
-                new EntityCallback(entityClass, methodName),
-                PRE_REMOVE_INDEX);
+        preRemove.addCallback(entityClass, methodName);
     }
 
     public void addPreRemoveCallback(
@@ -103,10 +85,7 @@
             String methodName,
             Class entityClass) {
 
-        addCallback(entityClass, new ListenerCallback(
-                objectClass,
-                methodName,
-                entityClass), PRE_REMOVE_INDEX);
+        preRemove.addCallback(objectClass, methodName, entityClass);
     }
 
     /**
@@ -116,7 +95,7 @@
     void applyPreRemoveCallbacks(Persistent object) {
 
         if (object.getPersistenceState() != PersistenceState.NEW) {
-            applyCallbacks(getCallbacks(object.getClass(), PRE_REMOVE_INDEX), object);
+            preRemove.applyCallbacks(object);
         }
 
         ObjEntity entity = getEntityResolver().lookupObjEntity(object);
@@ -147,42 +126,5 @@
                 }
             }
         }
-    }
-
-    /**
-     * Utility method applying callbacks to a collection.
-     */
-    void applyCallbacks(Collection callbacks, Object object) {
-        if (callbacks != null) {
-            Iterator it = (Iterator) callbacks.iterator();
-            while (it.hasNext()) {
-                ((Closure) it.next()).execute(object);
-            }
-        }
-    }
-
-    Collection getCallbacks(Class entityClass, int callbackType) {
-
-        Collection[] entityCallbacks = (Collection[]) callbacks
-                .get(entityClass.getName());
-
-        return (entityCallbacks != null) ? entityCallbacks[callbackType] : null;
-    }
-
-    void addCallback(Class entityClass, Closure callback, int callbackType) {
-
-        Collection[] entityCallbacks = (Collection[]) callbacks
-                .get(entityClass.getName());
-
-        if (entityCallbacks == null) {
-            entityCallbacks = new Collection[CALLBACK_SIZE];
-            callbacks.put(entityClass.getName(), entityCallbacks);
-        }
-
-        if (entityCallbacks[callbackType] == null) {
-            entityCallbacks[callbackType] = new ArrayList(3);
-        }
-
-        entityCallbacks[callbackType].add(callback);
     }
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptorTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptorTst.java?view=diff&rev=448453&r1=448452&r2=448453
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptorTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptorTst.java
Wed Sep 20 21:07:16 2006
@@ -55,9 +55,7 @@
         assertNotNull(a3);
         assertTrue(a3.isPrePersisted());
 
-        Collection prepersist = i.getCallbacks(
-                Artist.class,
-                ObjectContextCallbackInterceptor.PRE_PERSIST_INDEX);
+        Collection prepersist = i.prePersist.getCallbacks(Artist.class);
         assertNotNull(prepersist);
         assertEquals(2, prepersist.size());
 
@@ -107,9 +105,7 @@
         assertFalse(a3.isPrePersisted());
         assertTrue(a3.isPreRemoved());
 
-        Collection preremove = i.getCallbacks(
-                Artist.class,
-                ObjectContextCallbackInterceptor.PRE_REMOVE_INDEX);
+        Collection preremove = i.preRemove.getCallbacks(Artist.class);
         assertNotNull(preremove);
         assertEquals(2, preremove.size());
 



Mime
View raw message