-
-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(CtLineElementComparator): one can add two values to an annotation #3640
Conversation
I tried to write down the math why this is wrong in #3519. If we return -1, we have a sleeping but and wait till the sort algorithm finds this. |
* Returns 1 if o2 is after o1 in the file, or o2 has no valid position. | ||
*/ | ||
@Override | ||
public int compare(CtElement o1, CtElement o2) { | ||
if (!o1.getPosition().isValidPosition() && !o2.getPosition().isValidPosition()) { | ||
return 0; | ||
return o1.equals(o2) ? 0 : -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to respect the antisymetric property, this line could be replaced with
return o1.equals(o2) ? 0 : ((o1.hashCode() < o2.hashCode()) ? -1 : 1);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice fix. At first glance I see no problem with transitive and antisymmetric property. LGTM.
Thanks @nharrand |
fix #3639 .
I seems that the entry set of the values contained an CtAnnotationImpl are ordered based on their source position. The issue is that so far
CtLineElementComparator#compare()
returns 0 if both element haveNoSourcePosition
. But this is the case when both elements are constructed programmatically. Hence the entry set arbitrarily retains only one of them.My proposed solution is to make
CtLineElementComparator#compare()
returns 0 only if both elements have the same exact position or if both elements haveNoSourcePosition
and are equals. Otherwise we return -1 (i.e. order them aribtrarily). This way our entry set is pseudo order but does not delete unequals elements.