You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-dev@db.apache.org by Craig L Russell <Cr...@Sun.COM> on 2006/01/25 03:35:23 UTC

Negative VOTE Issue 154: Add two convenience methods to InstanceLifecycleEvent

Javadogs,

This proposal adds two new methods to InstanceLifecycleEvent. There  
is no impact on users or implementations unless the implementation  
supplies its own InstanceLifecycleEvent class (along with all other  
classes in the javax.jdo jar) or if implementations construct event  
instances with non-null target object parameters and the event is not  
postDetach or postAttach.

I'd like to adopt this proposal so it can be incorporated into the  
next build of the Apache JDO api jar and added to the next draft of  
the spec.

Please reply if you see any issues.

Thanks,

Craig

Here is the supporting detail.

Add convenience methods to get persistent instance and detached  
instance from InstanceLifecycleEvent
The source and target object differ as to whether they are the  
persistent or detached instance in class InstanceLifecycleEvent  
depending upon whether the event object is given in the preDetach,  
postDetach, preAttach, or postAttach callbacks. I propose adding two  
convenience methods that makes it obvious from the API which object  
the user is getting.
      /**
+     * Returns the persistent instance involved in the event.
+     *
+     * @return The persistent instance involved in the event, or  
null if there was none.
+     *
+     * @see "Section 12.15, Java Data Objects 2.0 Specification"
+     */
+    public Object getPersistentInstance() {
+        switch (getEventType()) {
+            case DETACH:
+                return target == null
+                        ? getSource()   // preDetach:  source is  
persistent instance
+                        : getTarget();  // postDetach:  target is  
persistent instance
+            case ATTACH:
+                return target == null
+                        ? null          // preAttach:  no persistent  
instance yet
+                        : getSource();  // postAttach:  source is  
persistent instance
+        }
+
+        // for all other events, source is persistent instance
+        return getSource();
+    }
+
+    /**
+     * Returns the detached instance involved in the event.
+     *
+     * @return The detached instance involved in the event, or null  
if there was none.
+     *
+     * @see "Section 12.15, Java Data Objects 2.0 Specification"
+     */
+    public Object getDetachedInstance() {
+        switch (getEventType()) {
+            case DETACH:
+                return target == null
+                        ? null          // preDetach:  no detached  
instance yet
+                        : getSource();  // postDetach:  source is  
detached instance
+            case ATTACH:
+                return target == null
+                        ? getSource()   // preAttach:  source is  
detached instance
+                        : getTarget();  // postAttach:  target is  
detached instance
+        }
+
+        // for all other events, there is no detached instance
+        return null;
+    }

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!