Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 469547 - WorkingSetComparator violates Comparator contract
Summary: WorkingSetComparator violates Comparator contract
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-06 08:11 EDT by Timo Kinnunen CLA
Modified: 2019-09-07 14:27 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Timo Kinnunen CLA 2015-06-06 08:11:45 EDT
Using a debug aid function to check that Comparators implement their API contract correctly reveals that org.eclipse.jdt.internal.ui.workingsets.WorkingSetComparator takes a shortcut in its implementation, resulting in a violation.

The API contract states: "The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y." 

When x and y are the same the only value A for which Integer.signum(A) == -Integer.signum(A) is 0.

Details from the debug aid method:
message: compare(x, x) is not 0, it is -1.

x was Other Projects (org.eclipse.ui.internal.WorkingSet) at 0
this: org.eclipse.jdt.internal.ui.workingsets.WorkingSetConfigurationDialog$12$1
comparator: org.eclipse.jdt.internal.ui.workingsets.WorkingSetComparator
array:
	Other Projects (org.eclipse.ui.internal.WorkingSet)
	All (org.eclipse.ui.internal.WorkingSet)
	Evening (org.eclipse.ui.internal.WorkingSet)
	GWT (org.eclipse.ui.internal.WorkingSet)
	Java Working Set (org.eclipse.ui.internal.WorkingSet)
	Repositories (org.eclipse.ui.internal.WorkingSet)
	Resources Working Set (org.eclipse.ui.internal.WorkingSet)
	Reviews (org.eclipse.ui.internal.WorkingSet)


Examining the WorkingSetComparator code reveals two short-circuiting if-statements. To fix the implementation these if-statements could be replaced with the following code:

		if (fIsOtherWorkingSetOnTop) {
			int result =
				Boolean.compare(IWorkingSetIDs.OTHERS.equals(w2.getId()), IWorkingSetIDs.OTHERS.equals(w1.getId()));
			if(result != 0) {
				return result;
			}
		}
Comment 1 Noopur Gupta CLA 2016-03-01 06:01:42 EST
I am not able to reproduce the issue by just creating and sorting the working sets mentioned in comment #0.

Timo, could you please push a Gerrit patch for your change and confirm that the issue does not occur after the fix?
Comment 2 Noopur Gupta CLA 2017-05-10 05:55:05 EDT
Target can be reassigned when the patch is available.
Comment 3 Eclipse Genie CLA 2019-09-07 14:27:06 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.