-
Notifications
You must be signed in to change notification settings - Fork 321
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
Replace usage of Maps.newHashMapWithExpectedSize() with standard Java #2976
Replace usage of Maps.newHashMapWithExpectedSize() with standard Java #2976
Conversation
org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/preferences/OptionsConfigurationBlock.java
Outdated
Show resolved
Hide resolved
...se.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/XtextGeneratorTemplates.xtend
Outdated
Show resolved
Hide resolved
I think @szarnekow should review this, but personally, I'm not a big fan of such strange expressions for computing the size ;) another note: |
Is it about the expression or specifying the size at all? If it is the latter, I can also remove it. If it is the former I'm fine to restrict this PR to the change of
That's right. But I have only used it in two places where the map is passed as |
org.eclipse.xtext/src/org/eclipse/xtext/resource/impl/EObjectDescriptionLookUp.java
Outdated
Show resolved
Hide resolved
4394639
to
85ca11f
Compare
I have now reverted the changes that require computation of the capacity. That can be replaced once the Java-19 |
Is there anything left here to land this? |
i guess a rebase would be helpful. |
85ca11f
to
e911c27
Compare
Rebased it and looked into the currently open issues and answered where I can think I can help something. :) |
@@ -48,8 +47,7 @@ protected boolean createEObjectDescriptions(IQualifiedNameProvider qualifiedName | |||
try { | |||
QualifiedName qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject); | |||
if (qualifiedName != null) { | |||
Map<String, String> userData = Maps.newHashMapWithExpectedSize(1); | |||
userData.put(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI)); | |||
Map<String, String> userData = Map.of(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI)); |
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.
I'm reluctant to accept this change, since the map will now throw an NPE when queried with null as a key. Though that's a valid implementation of the Map interface, it doesn't follow the principle of least surprise, which would just be containsKey(null) == false
and get(null) == null
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.
In both cases the map is passed to org.eclipse.xtext.resource.EObjectDescription
and only queried in
https://github.com/eclipse/xtext/blob/2bce8b6c62b1da7c57d81cff6e699eedb19d90a9/org.eclipse.xtext/src/org/eclipse/xtext/resource/EObjectDescription.java#L89-L94
The doc of that method already defines that the name argument must not be null:
https://github.com/eclipse/xtext/blob/2bce8b6c62b1da7c57d81cff6e699eedb19d90a9/org.eclipse.xtext/src/org/eclipse/xtext/resource/IEObjectDescription.java#L49-L54
It was indeed not enforced before, but the new behavior would be 100% compliant with the specification of the method.
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.
Understandable. Yet I prefer some simple guidelines in a code base, one of which would be to avoid the overly strict immutable collections. I've seen to many NPE when doing containsKey, indexOf or contains checks on these with a null in code paths that used to work great until they were refactored to the immutable collections.
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.
Personally I think it is good that these immutable collections are strict with null elements since one is forced to be more careful in using null, which on the long run and in general is an improvement for a code base, even though it can be annoying in the beginning.
But since you prefer not to use it here I replaced these two usages of Map.of() with HashMap
.
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.
Curious: Why didn't you use Collections.singletonMap instead?
(No need to change this)
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.
Curious: Why didn't you use Collections.singletonMap instead?
I'm not sure anymore. I think I just forgot it during the discussion and initially I probably assumed that Map.of()
is more elegant while meeting the spec. of the methods.
(No need to change this)
Even if there is no need, I think that's the better solution and change it accordingly.
2d4ab0a
to
b455f1c
Compare
@HannesWell two things I don't understand about test results:
|
Of course, for the second item, we could change our design and also build PRs for committers, cc @cdietrich |
68bd58c
to
83ae503
Compare
@LorenzoBettini Does your comment on the other MR mean that the check result |
The "Build / Test Results" failing cannot be trusted: it fails even in the presence of flakes. We can only trust "Build / build..." jobs failing. Unfortunately, the test results action cannot handle flaky tests currently. |
83ae503
to
9ebd37b
Compare
Can this be considered for Xtext M3? |
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.
I'd be ok with merging für M3
In Java-19 there is even static factory method
HashMap.newHashMap(int)
that can be used as a drop-in replacement once Java-19 or later is required.For now I calculate the necessary capacity explicitly using the formula:
expected-size *4/3+1
.The change in XtextGeneratorTemplates also removes the usage of
Maps.newHashMapWithExpectedSize()
of many generated Activators, but I haven't changed them manually. I assume they should be regenerated, shouldn't they?Part of #2975