Project

General

Profile

Revision 51a26cec

ID51a26cecb118546e04a322a8d7e825c04b4e5a5f
Parent 6ef55556
Child 82de6131

Added by Andreas Müller almost 4 years ago

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 ?

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java
59 59
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
60 60
import eu.etaxonomy.cdm.jaxb.UUIDAdapter;
61 61
import eu.etaxonomy.cdm.model.NewEntityListener;
62
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
63
import eu.etaxonomy.cdm.strategy.match.IMatchable;
62 64
import eu.etaxonomy.cdm.strategy.match.Match;
63 65
import eu.etaxonomy.cdm.strategy.match.MatchMode;
64 66

  
......
332 334
// ************* Object overrides *************************/
333 335

  
334 336
    /**
335
     * Is true if UUID is the same for the passed Object and this one.
337
     * Is <code>true</code> if UUID and created timestamp (is this really needed/make sense?)
338
     * is the same for the passed Object and this one.
339
     * This method is final as subclasses should not override it.
340
     * The contract should be the same for all persistable entities.
341
     * 2 instances are equal if they represent the same entity in a given
342
     * database.
343
     * <BR><BR>
344
     *
345
     * If one wants to compare 2 CdmBase entities content wise you may use e.g. a
346
     * {@link IMatchStrategy match strategy} and make sure
347
     * {@link IMatchable matching} is implemented for the respective CdmBase subclass.
348
     * You may adapt your match strategy to your own needs.
349
     *
336 350
     * @see java.lang.Object#equals(java.lang.Object)
337 351
     * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
338 352
     * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
339 353
     * for more information about equals and hashcode.
340 354
     */
341 355
    @Override
342
    public boolean equals(Object obj) {
356
    public final boolean equals(Object obj) {
343 357
        if (obj == this){
344 358
            return true;
345 359
        }
......
350 364
            return false;
351 365
        }
352 366
        ICdmBase cdmObj = (ICdmBase)obj;
353
        boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
354
        boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
367
        UUID objUuid = cdmObj.getUuid();
368
        if (objUuid == null){
369
            throw new NullPointerException("CdmBase is missing UUID");
370
        }
371
        boolean uuidEqual = objUuid.equals(this.getUuid());
372
        //TODO is this still needed?
373
        boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
355 374
        if (! uuidEqual || !createdEqual){
356 375
                return false;
357 376
        }
......
359 378
    }
360 379

  
361 380

  
381

  
362 382
    /** Overrides {@link java.lang.Object#hashCode()}
363 383
     *  See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
364 384
     * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VersionableEntity.java
11 11

  
12 12
import java.util.HashMap;
13 13
import java.util.Map;
14
import java.util.UUID;
15 14

  
16 15
import javax.persistence.Basic;
17 16
import javax.persistence.FetchType;
......
33 32
import org.hibernate.search.annotations.FieldBridge;
34 33
import org.joda.time.DateTime;
35 34

  
36
import eu.etaxonomy.cdm.common.CdmUtils;
37 35
import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
38 36
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
39 37
import eu.etaxonomy.cdm.strategy.match.Match;
......
117 115
		this.updated = updated;
118 116
	}
119 117

  
120
	/**
121
	 * Is true if UUID and created timestamp are the same for the passed Object and this one.
122
	 * @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
123
	 * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
124
	 * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
125
	 * for more information about equals and hashcode.
126
	 */
127
	@Override
128
	public boolean equals(Object obj) {
129
		if (obj == this){
130
			return true;
131
		}
132
		if (obj == null){
133
			return false;
134
		}
135
		if (!CdmBase.class.isAssignableFrom(obj.getClass())){
136
			return false;
137
		}
138
		ICdmBase cdmObj = (ICdmBase)obj;
139
		boolean uuidEqual;
140
		UUID objUuid = cdmObj.getUuid();
141
		if (objUuid == null){
142
			throw new NullPointerException("CdmBase is missing UUID");
143
		}
144
		uuidEqual = objUuid.equals(this.getUuid());
145
		//TODO is this still needed?
146
		boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
147
		if (! uuidEqual || !createdEqual){
148
				return false;
149
		}
150
		return true;
151
	}
152 118

  
153 119

  
154 120
//********************** CLONE *****************************************/

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)