Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2013-09-05 10:39:40 -0400
committerChristian W. Damus2013-09-06 14:39:00 -0400
commitb753888ec881037b4778a42e4f92808aa82ac9d1 (patch)
tree9cffd18ac5f6c4aef96791d49e159bf18bf77109 /plugins/views/org.eclipse.papyrus.views.search
parent3fd3ba388766504112b893d40122ee66bb83a795 (diff)
downloadorg.eclipse.papyrus-b753888ec881037b4778a42e4f92808aa82ac9d1.tar.gz
org.eclipse.papyrus-b753888ec881037b4778a42e4f92808aa82ac9d1.tar.xz
org.eclipse.papyrus-b753888ec881037b4778a42e4f92808aa82ac9d1.zip
415371: Fix problems with equals() and hashCode()
https://bugs.eclipse.org/bugs/show_bug.cgi?id=415371 Updated the equals() method to avoid resolving the source object, especially if it is an EObject, because it may no longer be available (e.g., after a CDO repository connection is closed). There is still a problem because equality with an EObject is not symmetric, but I did not remove that because I don't know whether it is needed. Also added the corresponding hashCode() method that was missing.
Diffstat (limited to 'plugins/views/org.eclipse.papyrus.views.search')
-rw-r--r--plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/results/AbstractResultEntry.java56
1 files changed, 41 insertions, 15 deletions
diff --git a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/results/AbstractResultEntry.java b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/results/AbstractResultEntry.java
index f2f26d7d687..1bb3f4a45e4 100644
--- a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/results/AbstractResultEntry.java
+++ b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/results/AbstractResultEntry.java
@@ -9,7 +9,8 @@
*
* Contributors:
* CEA LIST - Initial API and implementation
- * Christian W. Damus (CEA LIST) - Replace workspace IResource dependency with URI for CDO compatibility
+ * Christian W. Damus (CEA LIST) - Replace workspace IResource dependency with URI for CDO compatibility
+ * Christian W. Damus (CEA LIST) - Fix equals() to avoid resolving source objects and add missing hashCode()
*
*****************************************************************************/
package org.eclipse.papyrus.views.search.results;
@@ -21,7 +22,6 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.services.ServiceException;
@@ -145,7 +145,15 @@ public abstract class AbstractResultEntry extends Match {
*/
@Override
public boolean equals(Object obj) {
- if(obj instanceof AbstractResultEntry) {
+ if(obj == this) {
+ return true;
+ }
+ if(obj == null) {
+ return false;
+ }
+ if(!(obj instanceof AbstractResultEntry)) {
+ // support comparison against EObjects
+ // FIXME: This violates the symmetry contract of Object::equals!
if(obj instanceof EObject && this.getSource() instanceof EObject) {
if(EcoreUtil.equals((EObject)this.getSource(), (EObject)obj)) {
if(((AbstractResultEntry)obj).getOffset() == this.getOffset()) {
@@ -154,22 +162,40 @@ public abstract class AbstractResultEntry extends Match {
}
}
}
- } else {
- if(((AbstractResultEntry)obj).getSource() != null) {
- if(((AbstractResultEntry)obj).getSource().equals(this.getSource())) {
- if(((AbstractResultEntry)obj).getOffset() == this.getOffset()) {
- if(((AbstractResultEntry)obj).getLength() == this.getLength()) {
- return true;
- }
- }
-
- }
- }
}
return false;
+ }
+
+ AbstractResultEntry other = (AbstractResultEntry)obj;
+ // don't attempt to resolve the source object by URI in case it is no longer available.
+ // Note that, in the degenerate (and invalid) case in which all of the uri variants of
+ // both result entries are null, they are not considered equal
+ boolean sameSource = ((uriSource != null) && uriSource.equals(other.uriSource)) //
+ || ((uriResource != null) && uriResource.equals(other.uriResource)) //
+ || ((uriEResource != null) && uriEResource.equals(other.uriEResource));
+
+ return sameSource && (getOffset() == other.getOffset()) && (getLength() == other.getLength());
+ }
+
+ @Override
+ public int hashCode() {
+ int result = 0;
+
+ if (uriSource != null) {
+ result = result ^ uriSource.hashCode();
+ }
+ if (uriResource != null) {
+ result = result ^ uriResource.hashCode();
+ }
+ if (uriEResource != null) {
+ result = result ^ uriEResource.hashCode();
}
- return super.equals(obj);
+
+ result = result ^ (getOffset() * 17);
+ result = result ^ (getLength() * 37);
+
+ return super.hashCode();
}
/**

Back to the top