From 51a26cecb118546e04a322a8d7e825c04b4e5a5f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andreas=20M=C3=BCller?= Date: Thu, 18 Jan 2018 10:04:44 +0100 Subject: [PATCH] ref #7155, ref #7198, ref #7199 make Cdm.equals final * 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 ? --- .../etaxonomy/cdm/model/common/CdmBase.java | 28 ++++++++++++--- .../cdm/model/common/VersionableEntity.java | 34 ------------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java index de671822ef..acb5a836a2 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java @@ -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 true 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. + *

+ * + * 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} diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java index bf8dbf1db8..fcf826d7e9 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java @@ -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 *****************************************/ -- 2.34.1