ref #7155, ref #7198, ref #7199 make Cdm.equals final
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 18 Jan 2018 09:04:44 +0000 (10:04 +0100)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 18 Jan 2018 09:04:44 +0000 (10:04 +0100)
 * remove sematically equal method from VersionableEntity
 * add javadoc to explain why it should be final
 * open issue: do we really need to compare created in equals ?

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java

index de671822effffaeac835f025c7ca116135de754d..acb5a836a2220082b7e4e1ea8f263ba5bdb3c9bf 100644 (file)
@@ -59,6 +59,8 @@ import eu.etaxonomy.cdm.hibernate.search.UuidBridge;
 import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
 import eu.etaxonomy.cdm.jaxb.UUIDAdapter;
 import eu.etaxonomy.cdm.model.NewEntityListener;
+import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
+import eu.etaxonomy.cdm.strategy.match.IMatchable;
 import eu.etaxonomy.cdm.strategy.match.Match;
 import eu.etaxonomy.cdm.strategy.match.MatchMode;
 
@@ -332,14 +334,26 @@ public abstract class CdmBase implements Serializable, ICdmBase, ISelfDescriptiv
 // ************* Object overrides *************************/
 
     /**
-     * Is true if UUID is the same for the passed Object and this one.
+     * Is <code>true</code> if UUID and created timestamp (is this really needed/make sense?)
+     * is the same for the passed Object and this one.
+     * This method is final as subclasses should not override it.
+     * The contract should be the same for all persistable entities.
+     * 2 instances are equal if they represent the same entity in a given
+     * database.
+     * <BR><BR>
+     *
+     * If one wants to compare 2 CdmBase entities content wise you may use e.g. a
+     * {@link IMatchStrategy match strategy} and make sure
+     * {@link IMatchable matching} is implemented for the respective CdmBase subclass.
+     * You may adapt your match strategy to your own needs.
+     *
      * @see java.lang.Object#equals(java.lang.Object)
      * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
      * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
      * for more information about equals and hashcode.
      */
     @Override
-    public boolean equals(Object obj) {
+    public final boolean equals(Object obj) {
         if (obj == this){
             return true;
         }
@@ -350,8 +364,13 @@ public abstract class CdmBase implements Serializable, ICdmBase, ISelfDescriptiv
             return false;
         }
         ICdmBase cdmObj = (ICdmBase)obj;
-        boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
-        boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
+        UUID objUuid = cdmObj.getUuid();
+        if (objUuid == null){
+            throw new NullPointerException("CdmBase is missing UUID");
+        }
+        boolean uuidEqual = objUuid.equals(this.getUuid());
+        //TODO is this still needed?
+        boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
         if (! uuidEqual || !createdEqual){
                 return false;
         }
@@ -359,6 +378,7 @@ public abstract class CdmBase implements Serializable, ICdmBase, ISelfDescriptiv
     }
 
 
+
     /** Overrides {@link java.lang.Object#hashCode()}
      *  See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
      * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
index bf8dbf1db85a1e58e4f905677400f45cbc64b633..fcf826d7e9469aa3ba47ae571de1328c0d0a2d2a 100644 (file)
@@ -11,7 +11,6 @@ package eu.etaxonomy.cdm.model.common;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.UUID;
 
 import javax.persistence.Basic;
 import javax.persistence.FetchType;
@@ -33,7 +32,6 @@ import org.hibernate.search.annotations.Field;
 import org.hibernate.search.annotations.FieldBridge;
 import org.joda.time.DateTime;
 
-import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
 import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
 import eu.etaxonomy.cdm.strategy.match.Match;
@@ -117,38 +115,6 @@ public abstract class VersionableEntity extends CdmBase implements IVersionableE
                this.updated = updated;
        }
 
-       /**
-        * Is true if UUID and created timestamp are the same for the passed Object and this one.
-        * @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
-        * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
-        * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
-        * for more information about equals and hashcode.
-        */
-       @Override
-       public boolean equals(Object obj) {
-               if (obj == this){
-                       return true;
-               }
-               if (obj == null){
-                       return false;
-               }
-               if (!CdmBase.class.isAssignableFrom(obj.getClass())){
-                       return false;
-               }
-               ICdmBase cdmObj = (ICdmBase)obj;
-               boolean uuidEqual;
-               UUID objUuid = cdmObj.getUuid();
-               if (objUuid == null){
-                       throw new NullPointerException("CdmBase is missing UUID");
-               }
-               uuidEqual = objUuid.equals(this.getUuid());
-               //TODO is this still needed?
-               boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
-               if (! uuidEqual || !createdEqual){
-                               return false;
-               }
-               return true;
-       }
 
 
 //********************** CLONE *****************************************/