From b3ec6f03ddad985cc09c0cbc2a14bfa02c7260ac Mon Sep 17 00:00:00 2001 From: Simon Barner <barner@fortiss.org> Date: Thu, 11 Dec 2014 13:39:11 +0000 Subject: [PATCH] - Fix HierarchicalNameComparator to obey the total ordering contract of java.lang.comparable. - This should fix the related crashes that contained multiple objects with the same name refs 1841 --- .../generic/HierarchicalNameComparator.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/generic/HierarchicalNameComparator.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/generic/HierarchicalNameComparator.java index 54892a51a..0b248d4bd 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/generic/HierarchicalNameComparator.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/generic/HierarchicalNameComparator.java @@ -22,6 +22,7 @@ import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerComparator; import org.fortiss.tooling.base.model.element.IModelElement; import org.fortiss.tooling.base.ui.annotation.AnnotationEntry; +import org.fortiss.tooling.kernel.model.IIdLabeled; import org.fortiss.tooling.kernel.model.INamedElement; /** @@ -30,6 +31,34 @@ import org.fortiss.tooling.kernel.model.INamedElement; */ public class HierarchicalNameComparator extends ViewerComparator { + /** + * Compare two {@link Object}s by + * <ul> + * <li>name (if the are {@link INamedElement}s)</li> + * <li>Id (if they are {@link IIdLabeled} elements)</li> + * <li>Hash code (otherwise)</li> + * </ul> + */ + private int compareFallback(Object e1, Object o2) { + // ... and compare them by name... + if(e1 instanceof INamedElement && o2 instanceof INamedElement) { + // Elements are at the same level -> compare by name + int rval = (((INamedElement)e1).getName().compareTo(((INamedElement)o2).getName())); + if(rval != 0) { + return rval; + } + } + + // int Integer.compare(int x, int y) requires JDK 1.7 which is not available + // on the build server + if(e1 instanceof IIdLabeled && o2 instanceof IIdLabeled) { + return new Integer(((IIdLabeled)e1).getId()).compareTo(((IIdLabeled)o2).getId()); + } + + return new Integer(e1.hashCode()).compareTo(o2.hashCode()); + + } + /** {@inheritDoc} */ @Override public int compare(Viewer viewer, Object o1, Object o2) { @@ -80,16 +109,10 @@ public class HierarchicalNameComparator extends ViewerComparator { e2 = e2.eContainer(); } while(true); - // ... and compare them by name... - if(e1 instanceof INamedElement && e2 instanceof INamedElement) { - // Elements are at the same level -> compare by name - return(((INamedElement)e1).getName().compareTo(((INamedElement)e2).getName())); - } + // ... and compare them + return compareFallback(e1, e2); } - // ... or hash code (as a fallback ordering) - // int Integer.compare(int x, int y) requires JDK 1.7 which is not available - // on the build server - return new Integer(o1.hashCode()).compareTo(o2.hashCode()); + return compareFallback(o1, o2); } } -- GitLab