I would like to put an equals() method in BaseObject, and make
BaseObject and a couple of its methods abstract (nothing should ever
be instantiating a BaseObject).  Specifically, the getPrimaryKey() and
setPrimaryKey() which deal with an Object instance would become
abstract.  As I discussed with John McNally yesterday, this obviates
the need to store duplicate primary key information in multiple
locations within a BaseObject extension class.  John's recent Torque
changes now supply implementations for both of these methods, so
Torque generated code requires zero modification.  I am willing to
port the existing non-Torque-gen'd BaseObject subclasses that make up
Turbine (yeah, yeah I know it's rather trivial).

Here's an example patch:

Index: BaseObject.java
===================================================================
RCS file: 
/products/cvs/turbine/turbine/src/java/org/apache/turbine/om/BaseObject.java,v
retrieving revision 1.10
diff -u -u -r1.10 BaseObject.java
--- BaseObject.java     2001/01/31 22:27:53     1.10
+++ BaseObject.java     2001/02/02 18:52:33
@@ -68,7 +68,7 @@
  * @author <a href="mailto:[EMAIL PROTECTED]">John D. McNally</a>
  * @version $Id: BaseObject.java,v 1.10 2001/01/31 22:27:53 mcnally Exp $
  */
-public class BaseObject implements Serializable
+public abstract class BaseObject implements Serializable
 {
     public static final int NEW_ID = -1;
     
@@ -78,11 +78,6 @@
     private boolean is_new = true;
 
     /**
-     * The unique id for the object which can be used for persistence.
-     */
-    private Object primaryKey = null;
-
-    /**
      * A flag which can be set to indicate that an object has been
      * modified, since it was last retrieved from the persistence
      * mechanism.
@@ -95,10 +90,7 @@
      *
      * @return the object primaryKey as an Object
      */
-    public Object getPrimaryKey()
-    {
-        return primaryKey;
-    }
+    public abstract Object getPrimaryKey();
 
     /**
      * Attempts to return the object primaryKey as an int.
@@ -214,7 +206,7 @@
      */
     public void setPrimaryKey(int primaryKey) throws Exception
     {
-        this.primaryKey = new Integer(primaryKey);
+        setPrimaryKey(new Integer(primaryKey));
     }
 
     /**
@@ -226,7 +218,7 @@
      */
     public void setPrimaryKey(long primaryKey) throws Exception
     {
-        this.primaryKey = new Long(primaryKey);
+        setPrimaryKey(new Long(primaryKey));
     }
 
     /**
@@ -236,10 +228,7 @@
      * @exception Exception, This method will not throw any exceptions
      * but this allows for children to override the method more easily 
      */
-    public void setPrimaryKey(Object primaryKey) throws Exception
-    {
-        this.primaryKey = primaryKey;
-    }
+    public abstract void setPrimaryKey(Object primaryKey) throws Exception;
 
     /**
      * Sets the modified state for the object.
@@ -279,6 +268,45 @@
     public Object getByName(String field)
     {
         throw new Error("BaseObject.getByName: method must be overridden if called");
+    }
+
+    /**
+     * Compares this with another <code>BaseObject</code> instance.  Returns
+     * <code>false</code> if <code>obj</code> is not an instance of
+     * <code>BaseObject</code>, and calls <code>equals(BaseObject)</code>
+     * otherwise.
+     *
+     * @param obj The object to compare to.
+     * @return    Whether equal to the object specified.
+     */
+    public boolean equals(Object obj)
+    {
+        if (obj instanceof BaseObject)
+        {
+            return equals((BaseObject)obj);
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    /**
+     * Compares the primary key of this instance with that of another.
+     *
+     * @param bo The object to compare to.
+     * @return   Whether the primary keys are equal.
+     */
+    public boolean equals(BaseObject bo)
+    {
+        if (this == bo)
+        {
+            return true;
+        }
+        else
+        {
+            return getPrimaryKey().equals(bo.getPrimaryKey());
+        }
     }
 
     /*

-- 

Daniel Rall <[EMAIL PROTECTED]>


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to