Skip to content
Snippets Groups Projects
Commit b3ec6f03 authored by Simon Barner's avatar Simon Barner
Browse files

- 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
parent df89a3e6
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment